開始使用免費開始

使用 StandardScaler() 進行置中與縮放

我們已經載入同一個名為 data 的資料集。你現在的目標是使用 sklearn 函式庫中的 StandardScaler 將它進行置中與縮放。

pandasnumpyseabornmatplotlib.pyplot 分別已以 pdnpsnsplt 載入。我們也已經匯入 StandardScaler

你可以在主控台自由探索這個資料集。

本練習屬於課程

Python 的客群分群

檢視課程

練習說明

  • StandardScaler 初始化為 scaler,並對 data 執行 fit。
  • 使用 scalerdata 進行縮放與置中轉換。
  • data_normalized 建立一個 pandas DataFrame,並沿用 data 的索引與欄名。
  • 列印摘要統計,確認平均數為 0、標準差為 1,並將結果四捨五入到小數點後 2 位。

動手互動練習

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

# Initialize a scaler
scaler = ____()

# Fit the scaler
____.____(data)

# Scale and center the data
data_normalized = ____.____(data)

# Create a pandas DataFrame
data_normalized = pd.DataFrame(____, index=data.index, columns=data.columns)

# Print summary statistics
print(data_normalized.____().round(____))
編輯並執行程式碼