特征筛选3——卡方检验筛选特征(单变量筛选)

sklearn文档:https://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.chi2.html

卡方检验只适用分类任务,用来检验特征与y是否相互独立,具体的描述可以参考:https://www.jianshu.com/p/b670b2a23187

工具类是:

def chi2_selection(x_data, y_data):  # todo chi2 只能是分类方法
    # https://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.chi2.html
    from sklearn.feature_selection import chi2
    minmax_x = MinMaxScaler().fit_transform(x_data)
    chi2_value, p_value = chi2(minmax_x, y_data)
    chi2_df = pd.DataFrame(data=[x_data.columns, chi2_value]).T
    chi2_df.columns = ['feature', 'value']
    chi2_df.sort_values(by='value', ascending=False, inplace=True)
    return chi2_df

得到的结果值越大,证明特征与结果越相关,即特征越重要

示例

import pandas as pd
from sklearn.datasets import make_regression, make_classification
from sklearn.preprocessing import MinMaxScaler


def chi2_selection(x_data, y_data):
    # https://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.chi2.html
    from sklearn.feature_selection import chi2
    minmax_x = MinMaxScaler().fit_transform(x_data)
    chi2_value, p_value = chi2(minmax_x, y_data)
    chi2_df = pd.DataFrame(data=[x_data.columns, chi2_value]).T
    chi2_df.columns = ['feature', 'value']
    chi2_df.sort_values(by='value', ascending=False, inplace=True)
    return chi2_df


if __name__ == '__main__':
    value_x, value_y = make_classification(n_samples=1000, n_classes=4, n_features=10, n_informative=8)
    df_x = pd.DataFrame(value_x, columns=['f_1', 'f_2', 'f_3', 'f_4', 'f_5', 'f_6', "f_7", "f_8", "f_9", "f_10"])
    df_y = pd.Series(value_y)
    # 下面是筛选单变量特征
    feature_df = chi2_selection(df_x, df_y)
    for col_index, value in feature_df.iterrows():
        print(value[0], ":", value[1])

你可能感兴趣的:(机器学习,特征工程,sklearn,python,人工智能)