Làm thế nào để bạn chuyển một giá trị trong python?

Data binning, bucketing, hoặc rời rạc binning, là một kỹ thuật rất hữu ích cho cả tiền xử lý và hiểu hoặc trực quan hóa dữ liệu phức tạp, đặc biệt là trong quá trình phân khúc khách hàng. Nó được áp dụng cho các biến liên tục, chẳng hạn như doanh thu, đơn đặt hàng hoặc lần truy cập gần đây và nhóm nhiều dãy số lại với nhau thành một số lượng nhỏ các "thùng" hoặc "nhóm" riêng biệt. Thực tế, nó cho phép bạn tạo các biến phân loại từ các biến liên tục

Hai ứng dụng thực sự phổ biến của data binning trong thương mại điện tử là phân loại khoảng không quảng cáo ABC và phân đoạn RFM. Phân loại hàng tồn kho ABC sử dụng tỷ lệ phần trăm doanh thu tích lũy do mỗi SKU sản phẩm tạo ra để phân bổ nó vào loại A, B hoặc C, dựa trên đóng góp của nó cho doanh nghiệp, do đó giúp người quản lý thu mua kiểm soát tốt hơn danh mục sản phẩm rộng

Phân đoạn RFM lấy các biến RFM mạnh mẽ - lần truy cập gần đây, tần suất và giá trị tiền tệ - và phân loại chúng thành các nhóm từ 1 đến 5, dẫn đến 125 điểm từ 111 đến 555. Những điều này có thể cho các nhà tiếp thị thấy giá trị gần đúng của từng khách hàng đối với doanh nghiệp và liệu họ có nguy cơ mất hiệu lực hay mới mua hay không, điều mà họ không thể thực hiện chỉ từ các giá trị thô

Việc tạo thùng thống kê có thể được thực hiện nhanh chóng và dễ dàng trong Python, sử dụng cả chức năng Pandas, scikit-learning và tùy chỉnh. Ở đây, chúng tôi sẽ sử dụng nhiều kỹ thuật tạo thùng khác nhau để hiểu rõ hơn về tập dữ liệu thương mại điện tử điển hình

Tải tập dữ liệu của bạn

Trong dự án này, chúng tôi sẽ sử dụng tập dữ liệu Bán lẻ trực tuyến của Kho lưu trữ máy học UCI. Đó là tập dữ liệu giao dịch thông thường, vì vậy bạn sẽ dễ dàng tạo một phiên bản tương tự từ dữ liệu của chính mình nếu muốn. Tải lên Pandas và Numpy và nhập tập dữ liệu

import pandas as pd
import numpy as np
import jenkspy
import warnings
warnings.filterwarnings['ignore']

df = pd.read_csv['uci_online_retail.csv']

Pandas có thể khá chậm khi mở các tệp Excel, vì vậy khi tôi sử dụng tập dữ liệu này thường xuyên, tôi đã lưu lại nó dưới dạng tệp CSV để nó mở nhanh hơn. Tôi cũng đã loại bỏ một cột thừa và đổi tên các cột trong khung dữ liệu để chúng gọn gàng hơn một chút và phù hợp hơn với phong cách đặt tên biến thông thường của tôi

df.drop[['Unnamed: 0', 'Country'], axis=1, inplace=True] 
df.rename[columns={'InvoiceNo': 'order_id',
                   'StockCode': 'variant_id', 
                   'Description': 'name',
                   'InvoiceDate': 'date_created', 
                   'UnitPrice': 'item_price', 
                   'Quantity': 'quantity',
                   'CustomerID': 'customer_id'}, inplace=True]
df.head[]

order_idvariant_idnamequantitydate_createditem_pricecustomer_id053636585123GIỮ ĐÈN T TRÁI TIM TREO TREO62010-12-01 08. 26. 002. 5517850. 0153636571053ĐÈN KIM LOẠI TRẮNG62010-12-01 08. 26. 003. 3917850. 0253636584406BCREAM CUPID HEARTS MANG ÁO 82010-12-01 08. 26. 002. 7517850. 0353636584029GDÂY CHUYỀN CỜ ĐOÀN CHAI NƯỚC NÓNG62010-12-01 08. 26. 003. 3917850. 0453636584029ERED WOOOLLY HOTTIE TRÁI TIM TRẮNG. 62010-12-01 08. 26. 003. 3917850. 0

Kỹ sư một số tính năng bổ sung

Vì tập dữ liệu Bán lẻ trực tuyến không chứa dòng doanh thu và trường ngày giờ không được lưu trữ chính xác, trước tiên, chúng tôi sẽ sử dụng hàm Pandas

df['date_created'] = pd.to_datetime[df['date_created']]
df['line_revenue'] = df['quantity'] * df['item_price']
df.head[]
9 để đặt trường thành loại chính xác, sau đó tạo một cột mới chứa
df_customers = pd.DataFrame[df['customer_id'].unique[]]
df_customers.columns = ['customer_id']
df_customers.head[]
0,

df['date_created'] = pd.to_datetime[df['date_created']]
df['line_revenue'] = df['quantity'] * df['item_price']
df.head[]

order_idvariant_idnamequantitydate_createditem_pricecustomer_idline_revenue053636585123GIỮ ĐÈN TREO TIM TRÁI TIM62010-12-01 08. 26. 002. 5517850. 015. 30153636571053ĐÈN KIM LOẠI TRẮNG62010-12-01 08. 26. 003. 3917850. 020. 34253636584406BCREAM CUPID HEARTS MANG ÁO 82010-12-01 08. 26. 002. 7517850. 022. 00353636584029GDÂY CHUYỀN CỜ ĐOÀN CHAI NƯỚC NÓNG62010-12-01 08. 26. 003. 3917850. 020. 34453636584029ERED WOOOLLY HOTTIE TRÁI TIM TRẮNG. 62010-12-01 08. 26. 003. 3917850. 020. 34

Tạo tập dữ liệu cấp khách hàng

Vì chúng tôi sẽ tạo dữ liệu cấp độ khách hàng, chúng tôi cần lấy tập dữ liệu giao dịch của mình và biến nó thành cấp độ khách hàng. Bước đầu tiên trong quy trình này là tạo một khung dữ liệu mới dựa trên các khách hàng duy nhất trong dữ liệu

df_customers = pd.DataFrame[df['customer_id'].unique[]]
df_customers.columns = ['customer_id']
df_customers.head[]

khách hàng_id017850. 0113047. 0212583. 0313748. 0415100. 0

Tiếp theo, chúng tôi sẽ tính toán

df_customers = pd.DataFrame[df['customer_id'].unique[]]
df_customers.columns = ['customer_id']
df_customers.head[]
3 cho mỗi khách hàng bằng cách sử dụng ngày đặt hàng gần đây nhất của họ, sau đó chúng tôi sẽ tính toán
df_customers = pd.DataFrame[df['customer_id'].unique[]]
df_customers.columns = ['customer_id']
df_customers.head[]
4 của họ hoặc số ngày kể từ lần đặt hàng cuối cùng của họ. Vì tập dữ liệu này là từ năm 2011 nên các lần truy cập gần đây đều rất cao

df.drop[['Unnamed: 0', 'Country'], axis=1, inplace=True] 
df.rename[columns={'InvoiceNo': 'order_id',
                   'StockCode': 'variant_id', 
                   'Description': 'name',
                   'InvoiceDate': 'date_created', 
                   'UnitPrice': 'item_price', 
                   'Quantity': 'quantity',
                   'CustomerID': 'customer_id'}, inplace=True]
df.head[]
0

khách hàng_idrecency_daterecency017850. 02011-02-10 14. 38. 003546113047. 02011-11-08 12. 10. 003275212583. 02011-12-07 08. 07. 003246313748. 02011-09-05 09. 45. 003339415100. 02011-01-13 17. 09. 003574

Để kiểm tra số lượng đơn hàng đã đặt, chúng ta có thể tính toán số lượng giá trị

df_customers = pd.DataFrame[df['customer_id'].unique[]]
df_customers.columns = ['customer_id']
df_customers.head[]
5 duy nhất bằng cách sử dụng hàm
df_customers = pd.DataFrame[df['customer_id'].unique[]]
df_customers.columns = ['customer_id']
df_customers.head[]
6, sau đó chúng ta có thể gán giá trị cho khung dữ liệu khách hàng

df.drop[['Unnamed: 0', 'Country'], axis=1, inplace=True] 
df.rename[columns={'InvoiceNo': 'order_id',
                   'StockCode': 'variant_id', 
                   'Description': 'name',
                   'InvoiceDate': 'date_created', 
                   'UnitPrice': 'item_price', 
                   'Quantity': 'quantity',
                   'CustomerID': 'customer_id'}, inplace=True]
df.head[]
3

khách hàng_idrecency_daterecencyfrequency017850. 02011-02-10 14. 38. 00354635113047. 02011-11-08 12. 10. 00327518212583. 02011-12-07 08. 07. 00324618313748. 02011-09-05 09. 45. 0033395415100. 02011-01-13 17. 09. 0035746

Để tính giá trị

df_customers = pd.DataFrame[df['customer_id'].unique[]]
df_customers.columns = ['customer_id']
df_customers.head[]
7 cho mỗi khách hàng, chúng tôi chỉ cần
df_customers = pd.DataFrame[df['customer_id'].unique[]]
df_customers.columns = ['customer_id']
df_customers.head[]
8 giá trị trong cột
df_customers = pd.DataFrame[df['customer_id'].unique[]]
df_customers.columns = ['customer_id']
df_customers.head[]
0 mà chúng tôi đã tạo trước đó, sau đó gán giá trị trở lại khung dữ liệu

df.drop[['Unnamed: 0', 'Country'], axis=1, inplace=True] 
df.rename[columns={'InvoiceNo': 'order_id',
                   'StockCode': 'variant_id', 
                   'Description': 'name',
                   'InvoiceDate': 'date_created', 
                   'UnitPrice': 'item_price', 
                   'Quantity': 'quantity',
                   'CustomerID': 'customer_id'}, inplace=True]
df.head[]
7

customer_idrecency_daterecencyfrequencymonetary017850. 02011-02-10 14. 38. 003546355288. 63113047. 02011-11-08 12. 10. 003275183079. 10212583. 02011-12-07 08. 07. 003246187187. 34313748. 02011-09-05 09. 45. 0033395948. 25415100. 02011-01-13 17. 09. 0035746635. 10

Cuối cùng, để tính toán số liệu

df.drop[['Unnamed: 0', 'Country'], axis=1, inplace=True] 
df.rename[columns={'InvoiceNo': 'order_id',
                   'StockCode': 'variant_id', 
                   'Description': 'name',
                   'InvoiceDate': 'date_created', 
                   'UnitPrice': 'item_price', 
                   'Quantity': 'quantity',
                   'CustomerID': 'customer_id'}, inplace=True]
df.head[]
00, chúng ta có thể đếm số lượng sản phẩm duy nhất được mua bằng cách sử dụng
df_customers = pd.DataFrame[df['customer_id'].unique[]]
df_customers.columns = ['customer_id']
df_customers.head[]
6 trên cột
df.drop[['Unnamed: 0', 'Country'], axis=1, inplace=True] 
df.rename[columns={'InvoiceNo': 'order_id',
                   'StockCode': 'variant_id', 
                   'Description': 'name',
                   'InvoiceDate': 'date_created', 
                   'UnitPrice': 'item_price', 
                   'Quantity': 'quantity',
                   'CustomerID': 'customer_id'}, inplace=True]
df.head[]
02. Điều này sẽ cung cấp cho chúng tôi nhiều loại giá trị khác nhau, từ những khách hàng đã mua một SKU duy nhất trong đời cho đến những người đã mua hơn một trăm. Bây giờ chúng tôi đã có một bộ dữ liệu cấp độ khách hàng đẹp mà chúng tôi có thể sử dụng để tạo thùng

df['date_created'] = pd.to_datetime[df['date_created']]
df['line_revenue'] = df['quantity'] * df['item_price']
df.head[]
1

khách hàng_idrecency_daterecencytần suất tiền tệ giống017850. 02011-02-10 14. 38. 003546355288. 6324113047. 02011-11-08 12. 10. 003275183079. 10106212583. 02011-12-07 08. 07. 003246187187. 34115313748. 02011-09-05 09. 45. 0033395948. 2524415100. 02011-01-13 17. 09. 0035746635. 101

Sự rời rạc dựa trên lượng tử

Phương pháp tạo thùng đầu tiên mà chúng tôi sẽ áp dụng được gọi là hàm rời rạc hóa dựa trên lượng tử hoặc qcut. Phương pháp này tạo các nhóm bằng nhau [hoặc xấp xỉ bằng nhau] dựa trên thứ hạng hoặc ranh giới lượng tử cụ thể của chúng. Đó là kỹ thuật tiêu chuẩn để tạo nhóm ngũ phân vị RFM

Đầu tiên, chúng tôi sẽ chuyển dữ liệu trong cột

df.drop[['Unnamed: 0', 'Country'], axis=1, inplace=True] 
df.rename[columns={'InvoiceNo': 'order_id',
                   'StockCode': 'variant_id', 
                   'Description': 'name',
                   'InvoiceDate': 'date_created', 
                   'UnitPrice': 'item_price', 
                   'Quantity': 'quantity',
                   'CustomerID': 'customer_id'}, inplace=True]
df.head[]
00. Chúng tôi sẽ chia dữ liệu thành năm ngăn bằng nhau dựa trên số lượng SKU duy nhất mà mỗi khách hàng đã mua và chúng tôi sẽ gán cho họ nhãn thích hợp từ danh sách của chúng tôi

Đối với mục đích trình diễn, tôi đã xác định cụ thể các ranh giới ngăn, vì vậy các ngăn này là 0-0. 2 trong Rất thấp, 0. 2-0. 4 ở Thấp, 0. 4-0. 6 ở Trung bình, 0. 6-0. 8 ở mức cao và 0. 8-1 trong Rất cao. Tuy nhiên, bạn không cần cung cấp những thứ này nếu bạn sắp xếp các thùng đều nhau. Bạn chỉ cần cung cấp số lượng nhãn và

df.drop[['Unnamed: 0', 'Country'], axis=1, inplace=True] 
df.rename[columns={'InvoiceNo': 'order_id',
                   'StockCode': 'variant_id', 
                   'Description': 'name',
                   'InvoiceDate': 'date_created', 
                   'UnitPrice': 'item_price', 
                   'Quantity': 'quantity',
                   'CustomerID': 'customer_id'}, inplace=True]
df.head[]
04 sẽ phân phối chúng đồng đều mà không cần xác định ranh giới

df['date_created'] = pd.to_datetime[df['date_created']]
df['line_revenue'] = df['quantity'] * df['item_price']
df.head[]
4

df['date_created'] = pd.to_datetime[df['date_created']]
df['line_revenue'] = df['quantity'] * df['item_price']
df.head[]
5

df.drop[['Unnamed: 0', 'Country'], axis=1, inplace=True] 
df.rename[columns={'InvoiceNo': 'order_id',
                   'StockCode': 'variant_id', 
                   'Description': 'name',
                   'InvoiceDate': 'date_created', 
                   'UnitPrice': 'item_price', 
                   'Quantity': 'quantity',
                   'CustomerID': 'customer_id'}, inplace=True]
df.head[]
0

Nếu bạn xem nội dung của các thùng bằng cách sử dụng tổng hợp và tính giá trị tối thiểu, tối đa và trung bình cho cột đa dạng, bạn sẽ cảm nhận được cách dữ liệu được phân phối. Kích thước thùng xấp xỉ bằng nhau [nhưng không hoàn toàn bằng nhau] và tăng đúng kích thước như đã xác định

df.drop[['Unnamed: 0', 'Country'], axis=1, inplace=True] 
df.rename[columns={'InvoiceNo': 'order_id',
                   'StockCode': 'variant_id', 
                   'Description': 'name',
                   'InvoiceDate': 'date_created', 
                   'UnitPrice': 'item_price', 
                   'Quantity': 'quantity',
                   'CustomerID': 'customer_id'}, inplace=True]
df.head[]
1

min_varietymax_varietyavg_varietyvariety_binRất thấp1126. 472254Thấp132619. 279775Trung bình274836. 109175Cao499267. 258621Rất cao931794178. 728111

Tiếp theo, chúng tôi sẽ tạo một số thùng tercile có nhãn A, B và C, sẽ phân khúc khách hàng dựa trên tổng giá trị tiền tệ mà họ đã tạo ra cho doanh nghiệp. Vì chúng tôi muốn các ranh giới chẵn, chúng tôi chỉ cần chuyển 3 cho đối số q và dữ liệu sẽ được chia đều. Chúng tôi nhận được ba thùng với 1458, 1457 và 1457 khách hàng trong mỗi thùng

df.drop[['Unnamed: 0', 'Country'], axis=1, inplace=True] 
df.rename[columns={'InvoiceNo': 'order_id',
                   'StockCode': 'variant_id', 
                   'Description': 'name',
                   'InvoiceDate': 'date_created', 
                   'UnitPrice': 'item_price', 
                   'Quantity': 'quantity',
                   'CustomerID': 'customer_id'}, inplace=True]
df.head[]
2

df.drop[['Unnamed: 0', 'Country'], axis=1, inplace=True] 
df.rename[columns={'InvoiceNo': 'order_id',
                   'StockCode': 'variant_id', 
                   'Description': 'name',
                   'InvoiceDate': 'date_created', 
                   'UnitPrice': 'item_price', 
                   'Quantity': 'quantity',
                   'CustomerID': 'customer_id'}, inplace=True]
df.head[]
3

Sử dụng cùng một phương pháp tổng hợp, chúng ta có thể kiểm tra lại các giá trị tối thiểu, tối đa và trung bình trong mỗi ngăn để hiểu cách phân khúc khách hàng. Có sự khác biệt rõ ràng giữa các khách hàng trong các thùng, vì vậy nhãn ABC hoạt động tốt trong việc giúp mọi người hiểu được sự đóng góp của từng khách hàng đối với doanh nghiệp trong nháy mắt

df.drop[['Unnamed: 0', 'Country'], axis=1, inplace=True] 
df.rename[columns={'InvoiceNo': 'order_id',
                   'StockCode': 'variant_id', 
                   'Description': 'name',
                   'InvoiceDate': 'date_created', 
                   'UnitPrice': 'item_price', 
                   'Quantity': 'quantity',
                   'CustomerID': 'customer_id'}, inplace=True]
df.head[]
4

min_monetarymax_monetaryavg_monetarymonetary_binA1141. 34279489. 024815. 372952B374. 701141. 24685. 161594C-4287. 63374. 57196. 013018

Để kiểm tra cả hai thùng cùng nhau, chúng tôi có thể sử dụng

df.drop[['Unnamed: 0', 'Country'], axis=1, inplace=True] 
df.rename[columns={'InvoiceNo': 'order_id',
                   'StockCode': 'variant_id', 
                   'Description': 'name',
                   'InvoiceDate': 'date_created', 
                   'UnitPrice': 'item_price', 
                   'Quantity': 'quantity',
                   'CustomerID': 'customer_id'}, inplace=True]
df.head[]
05 với đối số
df.drop[['Unnamed: 0', 'Country'], axis=1, inplace=True] 
df.rename[columns={'InvoiceNo': 'order_id',
                   'StockCode': 'variant_id', 
                   'Description': 'name',
                   'InvoiceDate': 'date_created', 
                   'UnitPrice': 'item_price', 
                   'Quantity': 'quantity',
                   'CustomerID': 'customer_id'}, inplace=True]
df.head[]
06 tùy chọn của nó, sau đó sẽ cung cấp cho chúng tôi số lượng, giá trị duy nhất, giá trị hàng đầu và tần số cho từng bộ dữ liệu được phân loại

df.drop[['Unnamed: 0', 'Country'], axis=1, inplace=True] 
df.rename[columns={'InvoiceNo': 'order_id',
                   'StockCode': 'variant_id', 
                   'Description': 'name',
                   'InvoiceDate': 'date_created', 
                   'UnitPrice': 'item_price', 
                   'Quantity': 'quantity',
                   'CustomerID': 'customer_id'}, inplace=True]
df.head[]
5

đa dạng_binmonetary_bincount43724372unique53topLowCfreq8901458

Cắt

Trong khi tạo thùng thông qua phân tách dựa trên lượng tử với

df.drop[['Unnamed: 0', 'Country'], axis=1, inplace=True] 
df.rename[columns={'InvoiceNo': 'order_id',
                   'StockCode': 'variant_id', 
                   'Description': 'name',
                   'InvoiceDate': 'date_created', 
                   'UnitPrice': 'item_price', 
                   'Quantity': 'quantity',
                   'CustomerID': 'customer_id'}, inplace=True]
df.head[]
04 tạo ra các thùng có kích thước bằng nhau [hoặc càng gần bằng nhau càng tốt], đôi khi bạn có thể muốn sử dụng một cách tiếp cận khác và có các thùng có kích thước không bằng nhau. Bạn có thể làm điều này trong Pandas bằng hàm
df.drop[['Unnamed: 0', 'Country'], axis=1, inplace=True] 
df.rename[columns={'InvoiceNo': 'order_id',
                   'StockCode': 'variant_id', 
                   'Description': 'name',
                   'InvoiceDate': 'date_created', 
                   'UnitPrice': 'item_price', 
                   'Quantity': 'quantity',
                   'CustomerID': 'customer_id'}, inplace=True]
df.head[]
08

Điều này đôi khi có thể rất hữu ích khi phân phối thống kê của dữ liệu rất sai lệch. Ví dụ: có thể bạn đang phân tích dữ liệu khách hàng trong 20 năm và hầu hết khách hàng đã mất hiệu lực, với doanh thu hiện tại đến từ một phần nhỏ hơn những người đang hoạt động

Sử dụng

df.drop[['Unnamed: 0', 'Country'], axis=1, inplace=True] 
df.rename[columns={'InvoiceNo': 'order_id',
                   'StockCode': 'variant_id', 
                   'Description': 'name',
                   'InvoiceDate': 'date_created', 
                   'UnitPrice': 'item_price', 
                   'Quantity': 'quantity',
                   'CustomerID': 'customer_id'}, inplace=True]
df.head[]
04 có thể có nghĩa là dữ liệu được đánh dấu của bạn chứa hỗn hợp khách hàng đã mất hiệu lực và khách hàng đang hoạt động, nhưng sử dụng
df.drop[['Unnamed: 0', 'Country'], axis=1, inplace=True] 
df.rename[columns={'InvoiceNo': 'order_id',
                   'StockCode': 'variant_id', 
                   'Description': 'name',
                   'InvoiceDate': 'date_created', 
                   'UnitPrice': 'item_price', 
                   'Quantity': 'quantity',
                   'CustomerID': 'customer_id'}, inplace=True]
df.head[]
08 có thể đảm bảo chúng được phân tách tốt hơn dựa trên giới hạn bạn xác định. Mặc dù phân đoạn RFM thường được thực hiện bằng cách sử dụng phân tách dựa trên lượng tử, phương pháp thay thế này có thể thực tế hơn trên các bộ dữ liệu bị sai lệch nặng

df.drop[['Unnamed: 0', 'Country'], axis=1, inplace=True] 
df.rename[columns={'InvoiceNo': 'order_id',
                   'StockCode': 'variant_id', 
                   'Description': 'name',
                   'InvoiceDate': 'date_created', 
                   'UnitPrice': 'item_price', 
                   'Quantity': 'quantity',
                   'CustomerID': 'customer_id'}, inplace=True]
df.head[]
6

df.drop[['Unnamed: 0', 'Country'], axis=1, inplace=True] 
df.rename[columns={'InvoiceNo': 'order_id',
                   'StockCode': 'variant_id', 
                   'Description': 'name',
                   'InvoiceDate': 'date_created', 
                   'UnitPrice': 'item_price', 
                   'Quantity': 'quantity',
                   'CustomerID': 'customer_id'}, inplace=True]
df.head[]
7

Như bạn có thể thấy từ đoạn mã trên, việc chạy hàm

df.drop[['Unnamed: 0', 'Country'], axis=1, inplace=True] 
df.rename[columns={'InvoiceNo': 'order_id',
                   'StockCode': 'variant_id', 
                   'Description': 'name',
                   'InvoiceDate': 'date_created', 
                   'UnitPrice': 'item_price', 
                   'Quantity': 'quantity',
                   'CustomerID': 'customer_id'}, inplace=True]
df.head[]
08 chỉ với số lượng tham số cơ bản sẽ tạo ra các thùng có kích thước rất không bằng nhau, thể hiện sự thật của dữ liệu cơ bản

K-có nghĩa là cụm

Một cách tiếp cận phổ biến khác để tạo dữ liệu phân loại từ các biến liên tục và các biến khác là sử dụng các kỹ thuật học không giám sát, chẳng hạn như phân cụm. Thuật toán phân cụm K-means có lẽ được sử dụng rộng rãi nhất cho mục đích này và có thể được áp dụng nhanh chóng và dễ dàng cho một hoặc nhiều cột trong khung dữ liệu để trả về cụm được tạo động

df.drop[['Unnamed: 0', 'Country'], axis=1, inplace=True] 
df.rename[columns={'InvoiceNo': 'order_id',
                   'StockCode': 'variant_id', 
                   'Description': 'name',
                   'InvoiceDate': 'date_created', 
                   'UnitPrice': 'item_price', 
                   'Quantity': 'quantity',
                   'CustomerID': 'customer_id'}, inplace=True]
df.head[]
8

df.drop[['Unnamed: 0', 'Country'], axis=1, inplace=True] 
df.rename[columns={'InvoiceNo': 'order_id',
                   'StockCode': 'variant_id', 
                   'Description': 'name',
                   'InvoiceDate': 'date_created', 
                   'UnitPrice': 'item_price', 
                   'Quantity': 'quantity',
                   'CustomerID': 'customer_id'}, inplace=True]
df.head[]
9

đa dạngclusterclass_kmeans0240C11060C21150C3240C410C

df['date_created'] = pd.to_datetime[df['date_created']]
df['line_revenue'] = df['quantity'] * df['item_price']
df.head[]
0

df['date_created'] = pd.to_datetime[df['date_created']]
df['line_revenue'] = df['quantity'] * df['item_price']
df.head[]
1

Giảm giá trị duy nhất

Một ứng dụng tuyệt vời khác của data binning là khi nó được áp dụng để giảm các giá trị duy nhất trong một cột gồm các biến phân loại. Điều này thực sự có thể giúp mô hình của bạn nhìn thấy gỗ từ cây. Ví dụ: cột

df.drop[['Unnamed: 0', 'Country'], axis=1, inplace=True] 
df.rename[columns={'InvoiceNo': 'order_id',
                   'StockCode': 'variant_id', 
                   'Description': 'name',
                   'InvoiceDate': 'date_created', 
                   'UnitPrice': 'item_price', 
                   'Quantity': 'quantity',
                   'CustomerID': 'customer_id'}, inplace=True]
df.head[]
32 của chúng tôi trong bộ dữ liệu giao dịch ban đầu chứa 4223 giá trị duy nhất mà chúng tôi có thể thấy bằng cách chạy
df.drop[['Unnamed: 0', 'Country'], axis=1, inplace=True] 
df.rename[columns={'InvoiceNo': 'order_id',
                   'StockCode': 'variant_id', 
                   'Description': 'name',
                   'InvoiceDate': 'date_created', 
                   'UnitPrice': 'item_price', 
                   'Quantity': 'quantity',
                   'CustomerID': 'customer_id'}, inplace=True]
df.head[]
33. Nó có số lượng rất cao, vì vậy sẽ không phù hợp với mã hóa một lần dưới dạng biến phân loại

Tuy nhiên, giả sử chúng ta muốn sử dụng nó như một biến phân loại [đó là một ý tưởng ngu ngốc, nhưng hy vọng bạn sẽ hiểu được bức tranh]. Để làm điều này, chúng tôi cần giảm đáng kể số lượng của nó, điều mà chúng tôi có thể thực hiện bằng cách tạo dữ liệu dựa trên ngưỡng xuất hiện. Đây là một chức năng làm điều đó cho chúng tôi

df['date_created'] = pd.to_datetime[df['date_created']]
df['line_revenue'] = df['quantity'] * df['item_price']
df.head[]
2

Bây giờ, nếu chúng tôi chạy tính năng trên cột tên và đặt ngưỡng thành 2000, sau đó kiểm tra lại

df.drop[['Unnamed: 0', 'Country'], axis=1, inplace=True] 
df.rename[columns={'InvoiceNo': 'order_id',
                   'StockCode': 'variant_id', 
                   'Description': 'name',
                   'InvoiceDate': 'date_created', 
                   'UnitPrice': 'item_price', 
                   'Quantity': 'quantity',
                   'CustomerID': 'customer_id'}, inplace=True]
df.head[]
34 bằng cách sử dụng hàm Pandas value_counts[], chúng tôi sẽ thấy rằng chúng tôi có ba sản phẩm bán chạy nhất với tên lớp riêng và mọi thứ khác

df['date_created'] = pd.to_datetime[df['date_created']]
df['line_revenue'] = df['quantity'] * df['item_price']
df.head[]
3

order_idvariant_idnamequantitydate_createditem_pricecustomer_idline_revenuereduce_name053636585123GIỮ ĐÈN TREO TIM TRÁI TIM62010-12-01 08. 26. 002. 5517850. 015. 30ĐÈN ĐÈN T TREO TREO TRÁI TIM153636571053ĐÈN KIM LOẠI TRẮNG62010-12-01 08. 26. 003. 3917850. 020. 34Khác253636584406BCREAM CUPID TRÁI TIM MANG ÁO 82010-12-01 08. 26. 002. 7517850. 022. 00Khác353636584029GDÂY CHUYỀN CỜ CÔNG ĐOÀN CHAI NƯỚC NÓNG62010-12-01 08. 26. 003. 3917850. 020. 34Khác453636584029ERED TRÁI TIM TRẮNG LEN NỔI BẬT. 62010-12-01 08. 26. 003. 3917850. 020. 34Khác

df['date_created'] = pd.to_datetime[df['date_created']]
df['line_revenue'] = df['quantity'] * df['item_price']
df.head[]
4

df['date_created'] = pd.to_datetime[df['date_created']]
df['line_revenue'] = df['quantity'] * df['item_price']
df.head[]
5

Xẻ tự nhiên Fisher-Jenks

Cách tiếp cận cuối cùng mà chúng ta có thể áp dụng được gọi là thuật toán Fisher-Jenks. Đây là một cái mới đối với tôi, cho đến khi Chris Moffitt tại Practical Business Python trình bày về nó, nhưng nó trông thực sự tiện dụng. Thuật toán Fisher-Jenks, hoặc phương pháp tối ưu hóa Jenks, được sử dụng để xác định các phân tách tự nhiên trong bộ dữ liệu một chiều

Nó dường như cho kết quả gần giống nhất với phân cụm K-means, nhưng đáng để xem xét nếu có một điểm ngắt tự nhiên cụ thể mà bạn muốn xác định trong một chuỗi. Giống như phân cụm K-means, bạn cần xác định số lần ngắt. Nếu chúng ta chạy cái này trên cột

df.drop[['Unnamed: 0', 'Country'], axis=1, inplace=True] 
df.rename[columns={'InvoiceNo': 'order_id',
                   'StockCode': 'variant_id', 
                   'Description': 'name',
                   'InvoiceDate': 'date_created', 
                   'UnitPrice': 'item_price', 
                   'Quantity': 'quantity',
                   'CustomerID': 'customer_id'}, inplace=True]
df.head[]
00, thuật toán Fisher-Jenks sẽ tìm thấy các điểm ngắt tự nhiên sau

df['date_created'] = pd.to_datetime[df['date_created']]
df['line_revenue'] = df['quantity'] * df['item_price']
df.head[]
6

df['date_created'] = pd.to_datetime[df['date_created']]
df['line_revenue'] = df['quantity'] * df['item_price']
df.head[]
7

Bây giờ chúng ta đã biết vị trí tự nhiên của các điểm ngắt, chúng ta có thể sử dụng các vị trí này làm giới hạn cho hàm

df.drop[['Unnamed: 0', 'Country'], axis=1, inplace=True] 
df.rename[columns={'InvoiceNo': 'order_id',
                   'StockCode': 'variant_id', 
                   'Description': 'name',
                   'InvoiceDate': 'date_created', 
                   'UnitPrice': 'item_price', 
                   'Quantity': 'quantity',
                   'CustomerID': 'customer_id'}, inplace=True]
df.head[]
08, hàm này sau đó sẽ chuyển dữ liệu theo các điểm ngắt tự nhiên được tìm thấy, thay vì các ranh giới được xác định trước của chúng ta. Nếu bạn thấy rằng bạn đã có các giá trị
df.drop[['Unnamed: 0', 'Country'], axis=1, inplace=True] 
df.rename[columns={'InvoiceNo': 'order_id',
                   'StockCode': 'variant_id', 
                   'Description': 'name',
                   'InvoiceDate': 'date_created', 
                   'UnitPrice': 'item_price', 
                   'Quantity': 'quantity',
                   'CustomerID': 'customer_id'}, inplace=True]
df.head[]
38, bạn có thể chuyển vào đối số
df.drop[['Unnamed: 0', 'Country'], axis=1, inplace=True] 
df.rename[columns={'InvoiceNo': 'order_id',
                   'StockCode': 'variant_id', 
                   'Description': 'name',
                   'InvoiceDate': 'date_created', 
                   'UnitPrice': 'item_price', 
                   'Quantity': 'quantity',
                   'CustomerID': 'customer_id'}, inplace=True]
df.head[]
39 tùy chọn

Xô trong Python là gì?

buckets là thư viện python có thể được sử dụng để quản lý dữ liệu mà nó sẽ sắp xếp để giữ dữ liệu gần đây/có liên quan nhất và hủy ưu tiên dữ liệu cũ hơn. A bucket in this instance is a place holder for data. And with Buckets you can manage multiple bucket lists. A bucket list is a fixed size list of buckets.

Binning trong gấu trúc là gì?

Binning còn được gọi là chia nhóm hoặc rời rạc hóa là một kỹ thuật xử lý trước dữ liệu phổ biến được sử dụng để nhóm các khoảng dữ liệu liên tục thành “thùng” hoặc “nhóm”. In this article we will discuss 4 methods for binning numerical values using python Pandas library.

Chủ Đề