開始使用免費開始

正規化變數

現在進入資料前處理的最後一步。你將把未偏態的資料集 wholesale_boxcox 轉換到相同尺度,也就是讓所有欄位的平均數為 0、標準差為 1。你會使用 sklearn.preprocessing 模組中的 StandardScaler

你在前一題已轉換完成的未偏態資料集 wholesale_coxbox 已以 pandas DataFrame 匯入。此外,StandardScaler() 實例已初始化為 scaler

本練習屬於課程

Python 的行銷機器學習

檢視課程

練習說明

  • 在經過 Box-Cox 轉換的資料集上,對已初始化的 scaler 進行 fit。
  • 轉換並將縮放後的資料集儲存為 wholesale_scaled
  • 以縮放後的資料集建立一個 pandas DataFrame。
  • 列印所有欄位的平均數與標準差。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Fit the initialized `scaler` instance on the Box-Cox transformed dataset
scaler.___(wholesale_boxcox)

# Transform and store the scaled dataset as `wholesale_scaled`
wholesale_scaled = scaler.___(wholesale_boxcox)

# Create a `pandas` DataFrame from the scaled dataset
wholesale_scaled_df = pd.DataFrame(data=___,
                                       index=wholesale_boxcox.___,
                                       columns=wholesale_boxcox.columns)

# Print the mean and standard deviation for all columns
print(wholesale_scaled_df.agg(['___','std']).round())
編輯並執行程式碼