定義目標變數
在這裡,你要建立一個 pandas 樞紐分析表,將客戶作為列、發票月份作為欄,值為發票筆數。你會把最後一個月份的值作為目標變數。其餘變數可作為模型中的所謂延遲特徵(lagged features)。本題不會使用它們,但強烈建議你之後試試看加入這些變數,是否能讓模型效能優於接下來練習中會看到的結果。
pandas 與 numpy 函式庫已分別以 pd 與 np 載入。online 資料集也已為你匯入。
本練習屬於課程
Python 的行銷機器學習
練習說明
- 使用
pivot_table()函式建立樞紐分析表,計算發票筆數。 - 將 2011 年 11 月的銷售資料欄名儲存為清單。
- 將目標值儲存為
Y。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Build a pivot table counting invoices for each customer monthly
cust_month_tx = pd.___(data=online, values='___',
index=['___'], columns=['___'],
aggfunc=pd.Series.nunique, fill_value=0)
# Store November 2011 data column name as a list
target = ['2011-___']
# Store target value as `Y`
Y = cust_month_tx[___]