Lasso 模型结果
您已经训练好了 Lasso 模型。现在请在测试集上计算其预测能力($R^2$),并统计有多少特征因系数被压缩到 0 而被忽略。
X_test 和 y_test 数据集已为您预加载。
Lasso() 模型和 StandardScaler() 分别已实例化为 la 和 scaler,并都已在训练数据上完成拟合。
本练习是课程的一部分
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.")