低方差特征
在上一个练习中,您确定在归一化后的 head_df 中,0.001 是筛除低方差特征的合适阈值。现在请使用 VarianceThreshold 特征选择器移除这些特征。
本练习是课程的一部分
Python 中的降维
练习说明
- 使用阈值 0.001 创建方差阈值选择器。
- 通过用各列的均值进行除法来归一化
head_dfDataFrame,并拟合该选择器。 - 使用
.get_support()从选择器创建布尔掩码。 - 将该掩码作为参数传给
.loc[]方法,创建一个降维后的 DataFrame。
交互式实操练习
通过完成这段示例代码来试试这个练习。
from sklearn.feature_selection import VarianceThreshold
# Create a VarianceThreshold feature selector
sel = ____(threshold=____)
# Fit the selector to normalized head_df
sel.fit(____ / ____)
# Create a boolean mask
mask = sel.____
# Apply the mask to create a reduced DataFrame
reduced_df = head_df.loc[____, ____]
print(f"Dimensionality reduced from {head_df.shape[1]} to {reduced_df.shape[1]}.")