消除變數偏態
現在你要使用 Box-Cox 轉換來處理 wholesale 欄位,接著以兩兩變數關係圖來檢視結果,確認分配的偏態已降低、更接近常態。這是關鍵步驟,可確保 K-means 演算法能收斂,並找出同質的觀測群組(亦即叢集或區隔)。
已從 scipy 載入 stats 模組,且 wholesale 資料集已匯入為 pandas 的 DataFrame。
本練習屬於課程
Python 的行銷機器學習
練習說明
- 定義一個可套用在
pandasDataFrame 的自訂 Box-Cox 轉換函式。 - 將該函式套用到
wholesale資料集。 - 繪製轉換後變數之間的兩兩關係圖。
- 顯示圖表。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Define custom Box Cox transformation function
def boxcox_df(x):
x_boxcox, _ = stats.___(x)
return x_boxcox
# Apply the function to the `wholesale` dataset
wholesale_boxcox = ___.___(boxcox_df, axis=0)
# Plot the pairwise relationships between the transformed variables
sns.___(___, diag_kind='kde')
# Display the chart
plt.___()