如何使用 sklearn(卡方或方差分析)去除冗余特征
How to use sklearn ( chi-square or ANOVA) to removes redundant features
在特征选择步骤下,我们要识别相关特征并删除冗余特征。
根据我的理解,冗余特征是依赖特征。 (所以我们只想把特征之间的独立特征留给他们自己)
我的问题是关于使用 sklearn
和方差分析/卡方检验删除冗余特征。
根据我所读(和看到的示例),我们正在使用 SelectKBest
或 SelectPercentile
来保留取决于目标的最佳功能 (y
)
但是我们可以将这些方法与 chi2, f_classif
一起使用以删除依赖的特征吗?
换句话说,我想用 sklearn 方法去除多余的特征。我们该怎么做?
您可以使用 SelectKBest
来使用提供的函数(例如卡方)对特征进行评分,并获得 N 个得分最高的特征。例如,为了保留前 10 个功能,您可以使用以下内容:
from sklearn.feature_selection import SelectKBest, chi2, f_classif
# chi-square
top_10_features = SelectKBest(chi2, k=10).fit_transform(X, y)
# or ANOVA
top_10_features = SelectKBest(f_classif, k=10).fit_transform(X, y)
但是,通常有许多方法和技术可用于减少特征。您通常需要根据您的数据、您正在训练的模型和您想要预测的输出来决定使用哪些方法。例如,即使你最终有 20 个特征,你也需要检查每对特征之间的相关性是什么,并在它们高度相关的情况下删除一个。
以下函数将为您提供相关性最高的特征。您可以使用此输出进一步减少当前变量列表:
def get_feature_correlation(df, top_n=None, corr_method='spearman',
remove_duplicates=True, remove_self_correlations=True):
"""
Compute the feature correlation and sort feature pairs based on their correlation
:param df: The dataframe with the predictor variables
:type df: pandas.core.frame.DataFrame
:param top_n: Top N feature pairs to be reported (if None, all of the pairs will be returned)
:param corr_method: Correlation compuation method
:type corr_method: str
:param remove_duplicates: Indicates whether duplicate features must be removed
:type remove_duplicates: bool
:param remove_self_correlations: Indicates whether self correlations will be removed
:type remove_self_correlations: bool
:return: pandas.core.frame.DataFrame
"""
corr_matrix_abs = df.corr(method=corr_method).abs()
corr_matrix_abs_us = corr_matrix_abs.unstack()
sorted_correlated_features = corr_matrix_abs_us \
.sort_values(kind="quicksort", ascending=False) \
.reset_index()
# Remove comparisons of the same feature
if remove_self_correlations:
sorted_correlated_features = sorted_correlated_features[
(sorted_correlated_features.level_0 != sorted_correlated_features.level_1)
]
# Remove duplicates
if remove_duplicates:
sorted_correlated_features = sorted_correlated_features.iloc[:-2:2]
# Create meaningful names for the columns
sorted_correlated_features.columns = ['Feature 1', 'Feature 2', 'Correlation (abs)']
if top_n:
return sorted_correlated_features[:top_n]
return sorted_correlated_features
其他选项可能是:
- 缺失值的百分比
- 与目标变量的相关性
- 包括一些随机变量,看看它们是否进入后续的简化变量列表
- 随着时间的推移功能稳定性
- 等等
正如我所提到的,这实际上取决于您要实现的目标。
在特征选择步骤下,我们要识别相关特征并删除冗余特征。
根据我的理解,冗余特征是依赖特征。 (所以我们只想把特征之间的独立特征留给他们自己)
我的问题是关于使用 sklearn
和方差分析/卡方检验删除冗余特征。
根据我所读(和看到的示例),我们正在使用 SelectKBest
或 SelectPercentile
来保留取决于目标的最佳功能 (y
)
但是我们可以将这些方法与 chi2, f_classif
一起使用以删除依赖的特征吗?
换句话说,我想用 sklearn 方法去除多余的特征。我们该怎么做?
您可以使用 SelectKBest
来使用提供的函数(例如卡方)对特征进行评分,并获得 N 个得分最高的特征。例如,为了保留前 10 个功能,您可以使用以下内容:
from sklearn.feature_selection import SelectKBest, chi2, f_classif
# chi-square
top_10_features = SelectKBest(chi2, k=10).fit_transform(X, y)
# or ANOVA
top_10_features = SelectKBest(f_classif, k=10).fit_transform(X, y)
但是,通常有许多方法和技术可用于减少特征。您通常需要根据您的数据、您正在训练的模型和您想要预测的输出来决定使用哪些方法。例如,即使你最终有 20 个特征,你也需要检查每对特征之间的相关性是什么,并在它们高度相关的情况下删除一个。
以下函数将为您提供相关性最高的特征。您可以使用此输出进一步减少当前变量列表:
def get_feature_correlation(df, top_n=None, corr_method='spearman',
remove_duplicates=True, remove_self_correlations=True):
"""
Compute the feature correlation and sort feature pairs based on their correlation
:param df: The dataframe with the predictor variables
:type df: pandas.core.frame.DataFrame
:param top_n: Top N feature pairs to be reported (if None, all of the pairs will be returned)
:param corr_method: Correlation compuation method
:type corr_method: str
:param remove_duplicates: Indicates whether duplicate features must be removed
:type remove_duplicates: bool
:param remove_self_correlations: Indicates whether self correlations will be removed
:type remove_self_correlations: bool
:return: pandas.core.frame.DataFrame
"""
corr_matrix_abs = df.corr(method=corr_method).abs()
corr_matrix_abs_us = corr_matrix_abs.unstack()
sorted_correlated_features = corr_matrix_abs_us \
.sort_values(kind="quicksort", ascending=False) \
.reset_index()
# Remove comparisons of the same feature
if remove_self_correlations:
sorted_correlated_features = sorted_correlated_features[
(sorted_correlated_features.level_0 != sorted_correlated_features.level_1)
]
# Remove duplicates
if remove_duplicates:
sorted_correlated_features = sorted_correlated_features.iloc[:-2:2]
# Create meaningful names for the columns
sorted_correlated_features.columns = ['Feature 1', 'Feature 2', 'Correlation (abs)']
if top_n:
return sorted_correlated_features[:top_n]
return sorted_correlated_features
其他选项可能是:
- 缺失值的百分比
- 与目标变量的相关性
- 包括一些随机变量,看看它们是否进入后续的简化变量列表
- 随着时间的推移功能稳定性
- 等等
正如我所提到的,这实际上取决于您要实现的目标。