调整正则化强度
您当前的 Lasso 模型的 \(R^2\) 分数为 84.7%。当模型的正则化过强时,会产生较高偏差,从而削弱其预测能力。
让我们通过调整 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.")