低變異特徵
在前一個練習中,你確認在正規化後,使用 0.001 作為門檻能過濾掉 head_df 中的低變異特徵。現在請使用 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]}.")