使用 NMF 的替代式分群
在這個練習中,你將分析商品購買資料,並使用非負矩陣分解(NMF)演算法找出有意義的族群。它特別適合處理常見於電商或零售情境中的稀疏「顧客 × 商品」矩陣。最後,你會擷取 components,並在下一個練習中進一步探索。
我們已將 pandas 載入為 pd,numpy 載入為 np。此外,原始的顧客 × 商品購買資料集已載入為 wholesale。
本練習屬於課程
Python 的行銷機器學習
練習說明
- 從
sklearn.decomposition匯入非負矩陣分解的函式。 - 以 4 個 components 初始化
NMF實例。 - 在
wholesale銷售資料上訓練(fit)模型。 - 將擷取到的 components 以
pandasDataFrame 形式儲存。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Import the non-negative matrix factorization module
from sklearn.decomposition import ___
# Initialize NMF instance with 4 components
nmf = ___(4)
# Fit the model on the wholesale sales data
nmf.___(wholesale)
# Extract the components
components = pd.DataFrame(data=nmf.___, columns=wholesale.columns)