始める無料で始める

Lasso モデルの結果

Lasso モデルを学習できたので、テストセットで予測性能($R^2$)をスコア化し、係数が 0 に縮まったために無視された特徴量がいくつあるか数えます。

X_testy_test のデータセットはあらかじめ読み込まれています。

Lasso() モデルと StandardScaler() はそれぞれ lascaler としてインスタンス化され、どちらも学習データにフィット済みです。

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

Pythonで学ぶ次元削減

コースを見る

演習の手順

  • 事前にフィット済みのスケーラーでテストセットを変換します。
  • スケーリング後のテストデータで \(R^2\) を計算します。
  • 係数が 0 に等しいときに True となるリストを作成します。
  • 係数が 0 の特徴量の総数を計算します。

実践的なインタラクティブ演習

このサンプルコードを完成させて、この演習に挑戦してみましょう。

# Transform the test set with the pre-fitted scaler
X_test_std = scaler.____

# Calculate the coefficient of determination (R squared) on X_test_std
r_squared = la.____(____, ____)
print(f"The model can predict {r_squared:.1%} of the variance in the test set.")

# Create a list that has True values when coefficients equal 0
zero_coef = la.____ == ____

# Calculate how many features have a zero coefficient
n_ignored = sum(____)
print(f"The model has ignored {n_ignored} out of {len(la.coef_)} features.")
コードを編集して実行