調整正規化強度
你目前的 Lasso 模型有 84.7% 的 \(R^2\) 分數。當模型的正規化過強時,可能會有高偏差,進而降低預測能力。
讓我們透過調整 alpha 參數,在預測力與模型簡潔度之間取得更好的平衡。
本練習屬於課程
Python 的降維
練習說明
- 從
1、0.5、0.1、0.01這些選項中,找到能讓 \(R^2\) 高於 98% 的「最大」alpha值。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Find the highest alpha value with R-squared above 98%
la = Lasso(____, random_state=0)
# Fits the model and calculates performance stats
la.fit(X_train_std, y_train)
r_squared = la.score(X_test_std, y_test)
n_ignored_features = sum(la.coef_ == 0)
# Print peformance stats
print(f"The model can predict {r_squared:.1%} of the variance in the test set.")
print(f"{n_ignored_features} out of {len(la.coef_)} features were ignored.")