구매 예측을 위한 데이터 준비

Python으로 배우는 마케팅용 Machine Learning

Karolis Urbonas

Head of Analytics & Science, Amazon

회귀: 연속형 변수 예측

  • 회귀: 지도학습의 한 유형
  • 타깃 변수: 연속형 또는 카운트형 변수
  • 가장 단순한 형태: 선형 회귀
  • 카운트 데이터(예: 활성 일수)는 포아송/음이항 회귀가 더 적합할 수 있음
Python으로 배우는 마케팅용 Machine Learning

RFM 피처: Recency, Frequency, Monetary

  • RFM: 많은 피처 엔지니어링의 기반 접근법
  • 최신성(Recency): 마지막 거래 이후 경과 시간
  • 빈도(Frequency): 관측 기간 내 구매 횟수
  • 금액(Monetary): 관측 기간 총 지출액
Python으로 배우는 마케팅용 Machine Learning

월별 판매 분포 탐색

# 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
Python으로 배우는 마케팅용 Machine Learning

피처 데이터 분리

# 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']
Python으로 배우는 마케팅용 Machine Learning

피처 검토

print(features.head())

피처 헤더

Python으로 배우는 마케팅용 Machine Learning

타깃 변수 계산

# 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())

고객별 월간 피벗 테이블

Python으로 배우는 마케팅용 Machine Learning

데이터 준비 완료 및 학습/테스트 분할

# 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]
Python으로 배우는 마케팅용 Machine Learning

학습/테스트 데이터 분할

# 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)
Python으로 배우는 마케팅용 Machine Learning

데이터 준비 실습을 해봅시다!

Python으로 배우는 마케팅용 Machine Learning

Preparing Video For Download...