Chuẩn bị dữ liệu cho dự đoán mua hàng

Machine Learning cho Marketing với Python

Karolis Urbonas

Head of Analytics & Science, Amazon

Hồi quy - dự đoán biến liên tục

  • Hồi quy - học có giám sát
  • Biến mục tiêu - liên tục hoặc đếm
  • Phiên bản đơn giản nhất - hồi quy tuyến tính
  • Dữ liệu đếm (vd. số ngày hoạt động) đôi khi dự đoán tốt hơn bằng hồi quy Poisson hoặc Nhị thức âm
Machine Learning cho Marketing với Python

Đặc trưng RFM: Recency, Frequency, Monetary

  • RFM - nền tảng của nhiều phương pháp tạo đặc trưng
  • Recency - thời gian từ lần giao dịch gần nhất
  • Frequency - số lần mua trong giai đoạn quan sát
  • Giá trị Monetary - tổng chi tiêu trong giai đoạn quan sát
Machine Learning cho Marketing với Python

Khám phá phân bố doanh số theo tháng

# Explore monthly distribution of observations
online.groupby(['InvoiceMonth']).size()
InvoiceMonth
2010-12    4893
2011-01    3580
2011-02    3648
2011-03    4764
2011-04    4148
2011-05    5018
2011-06    4669
2011-07    4610
2011-08    4744
2011-09    7189
2011-10    8808
2011-11    9513
dtype: int64
Machine Learning cho Marketing với Python

Tách dữ liệu đặc trưng

# Exclude target variable
online_X = online[online['InvoiceMonth']!='2011-11']

# Define snapshot date NOW = dt.datetime(2011,11,1)
# Build the features features = online_X.groupby('CustomerID').agg({ 'InvoiceDate': lambda x: (NOW - x.max()).days, 'InvoiceNo': pd.Series.nunique, 'TotalSum': np.sum, 'Quantity': ['mean', 'sum'] }).reset_index()
features.columns = ['CustomerID', 'recency', 'frequency', 'monetary', 'quantity_avg', 'quantity_total']
Machine Learning cho Marketing với Python

Xem lại đặc trưng

print(features.head())

Phần đầu bảng đặc trưng

Machine Learning cho Marketing với Python

Tính biến mục tiêu

# Build pivot table with monthly transactions per customer
cust_month_tx = pd.pivot_table(data=online, index=['CustomerID'], 
                               values='InvoiceNo',
                               columns=['InvoiceMonth'],
                               aggfunc=pd.Series.nunique, fill_value=0)
print(cust_month_tx.head())

Bảng xoay giao dịch theo tháng của khách hàng

Machine Learning cho Marketing với Python

Hoàn tất chuẩn bị dữ liệu và tách train/test

# Store identifier and target variable column names
custid = ['CustomerID']
target = ['2011-11']

# Extract target variable Y = cust_month_tx[target]
# Extract feature column names cols = [col for col in features.columns if col not in custid]
# Store features X = features[cols]
Machine Learning cho Marketing với Python

Chia dữ liệu thành train và test

# Randomly split 25% of the data to testing
from sklearn.model_selection import train_test_split
train_X, test_X, train_Y, test_Y = train_test_split(X, Y, 
                                                    test_size=0.25, 
                                                    random_state=99)

# Print shapes of the datasets print(train_X.shape, train_Y.shape, test_X.shape, test_Y.shape)
(2529, 5) (2529, 1) (843, 5) (843, 1)
Machine Learning cho Marketing với Python

Hãy luyện tập!

Machine Learning cho Marketing với Python

Preparing Video For Download...