含缺失值或低方差的特征

Python 中的降维

Jeroen Boeye

Head of Machine Learning, Faktion

创建特征选择器

print(ansur_df.shape)
(6068, 94)
from sklearn.feature_selection import VarianceThreshold

sel = VarianceThreshold(threshold=1)

sel.fit(ansur_df) mask = sel.get_support() print(mask)
array([ True,  True, ..., False,  True])
Python 中的降维

应用特征选择器

print(ansur_df.shape)
(6068, 94)
reduced_df = ansur_df.loc[:, mask]
print(reduced_df.shape)
(6068, 93)
Python 中的降维

方差选择器注意事项

buttock_df.boxplot()

特征箱线图

Python 中的降维

标准化方差

from sklearn.feature_selection import VarianceThreshold

sel = VarianceThreshold(threshold=0.005)

sel.fit(ansur_df / ansur_df.mean())

mask = sel.get_support() reduced_df = ansur_df.loc[:, mask] print(reduced_df.shape)
(6068, 45)
Python 中的降维

缺失值选择器

宝可梦示例

Python 中的降维

缺失值选择器

宝可梦示例 NaN

Python 中的降维

识别缺失值

pokemon_df.isna()

宝可梦示例缺失值布尔

Python 中的降维

统计缺失值数量

pokemon_df.isna().sum()
Name         0
Type 1       0
Type 2     386
Total        0
HP           0
Attack       0
Defense      0
dtype: int64
Python 中的降维

统计缺失值占比

pokemon_df.isna().sum() / len(pokemon_df)
Name       0.00
Type 1     0.00
Type 2     0.48
Total      0.00
HP         0.00
Attack     0.00
Defense    0.00
dtype: float64
Python 中的降维

应用缺失值阈值

# 缺失值少于 30% = True
mask = pokemon_df.isna().sum() / len(pokemon_df) < 0.3
print(mask)
Name        True
Type 1      True
Type 2     False
Total       True
HP          True
Attack      True
Defense     True
dtype: bool
Python 中的降维

应用缺失值阈值

reduced_df = pokemon_df.loc[:, mask]

reduced_df.head()

应用掩码后的宝可梦示例

Python 中的降维

Vamos praticar!

Python 中的降维

Preparing Video For Download...