始める無料で始める

変数を正規化する

いよいよデータ準備の最終ステップです。歪度を補正したデータセット wholesale_boxcox を同一スケールに変換し、すべての列の平均を 0、標準偏差を 1 にそろえます。sklearn.preprocessing モジュールの StandardScaler を使います。

前の演習で変換した、歪度を補正済みの wholesale_coxbox データセットは、pandas の DataFrame としてインポート済みです。また、StandardScaler() のインスタンスは scaler として初期化されています。

この演習はコースの一部です

Pythonで学ぶマーケティングのための機械学習

コースを見る

演習の手順

  • 初期化済みの scaler インスタンスを、Box-Cox 変換後のデータセットに対して 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())
コードを編集して実行