NMF 分群的平均值
最後,你將以視覺化方式探索由 NMF 建立的 3 群解的平均值,並與 K-means 的結果做比較。你會取出特徵矩陣 W,並以此取得「硬指定」的分群:對每位顧客,在矩陣中選取其對應列裡數值最高的欄(分群)作為指派。
我們已將 pandas 函式庫載入為 pd,並將 seaborn 載入為 sns。原始的 wholesale 資料集已匯入,且已經訓練完成的 3 群 NMF 物件為 nmf。components 資料集已載入為 pandas 的 DataFrame。
本練習屬於課程
Python 的行銷機器學習
練習說明
- 以轉換後的數值作為資料,並以 components 的索引作為欄名稱,建立 W 矩陣。
- 指派分群:對每位顧客選取該列中數值最大的欄名作為分群值。
- 針對每個分群計算各欄位的平均值。
- 以 heatmap 繪製這些平均值。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Create the W matrix
W = pd.DataFrame(data=nmf.___(wholesale), columns=components.index)
W.index = wholesale.index
# Assign the column name where the corresponding value is the largest
wholesale_nmf3 = wholesale.___(segment = W.idxmax(axis=1))
# Calculate the average column values per each segment
nmf3_averages = wholesale_nmf3.___('___').mean().round(0)
# Plot the average values as heatmap
sns.___(___.T, cmap='YlGnBu')
# Display the chart
plt.show()