如何使用scikit-learn中的信息增益度量来选择DataFrame中的最佳特征

5
我想使用信息增益度量(scikit-learn中的相互信息)来识别DataFrame的前10个最佳特征,并将它们按照信息增益度量得分的升序顺序显示在表格中。
在这个例子中,features是一个DataFrame,其中包含所有能够告诉我们餐厅是否会关闭的有趣训练数据。
# Initialization of data and labels
x = features.copy () # "x" contains all training data
y = x ["closed"] # "y" contains the labels of the records in "x"

# Elimination of the class column (closed) of features
x = x.drop ('closed', axis = 1)

# this is x.columns, sorry for the mix french and english
features_columns = ['moyenne_etoiles', 'ville', 'zone', 'nb_restaurants_zone',
       'zone_categories_intersection', 'ville_categories_intersection',
       'nb_restaurant_meme_annee', 'ecart_type_etoiles', 'tendance_etoiles',
       'nb_avis', 'nb_avis_favorables', 'nb_avis_defavorables',
       'ratio_avis_favorables', 'ratio_avis_defavorables',
       'nb_avis_favorables_mention', 'nb_avis_defavorables_mention',
       'nb_avis_favorables_elites', 'nb_avis_defavorables_elites',
       'nb_conseils', 'nb_conseils_compliment', 'nb_conseils_elites',
       'nb_checkin', 'moyenne_checkin', 'annual_std', 'chaine',
       'nb_heures_ouverture_semaine', 'ouvert_samedi', 'ouvert_dimanche',
       'ouvert_lundi', 'ouvert_vendredi', 'emporter', 'livraison',
       'bon_pour_groupes', 'bon_pour_enfants', 'reservation', 'prix',
       'terrasse']

# normalization
std_scale = preprocessing.StandardScaler().fit(features[features_columns])
normalized_data = std_scale.transform(features[features_columns])
labels = np.array(features['closed'])

# split the data 
train_features, test_features, train_labels, test_labels = train_test_split(normalized_data, labels, test_size = 0.2, random_state = 42)

labels_true = ?
labels_pred = ?

# I dont really know how to use this function to achieve what i want
from sklearn.feature_selection import mutual_info_classif
from sklearn.datasets import make_classification



# Get the mutual information coefficients and convert them to a data frame
coeff_df =pd.DataFrame(features,
                         columns=['Coefficient'], index=x.columns)

coeff_df.head()



什么是使用互信息分数实现这一目标的正确语法?

你尝试了什么?你的尝试出了什么问题?sklearn聚类用户指南提供了语法和用法示例。 - G. Anderson
在这个例子中,labels_true和labels_pred是否就是我案例中的train_features和test_features? - Lynn
当他们在示例中显示0101时,这很令人困惑。 - Lynn
@G.Anderson 噢,所以我需要先训练一个模型吗?比如决策树分类器或随机森林?我以为这部分是在之后进行的。在我的想法中,更像是:我使用信息增益度量选择数据框中的前10个最佳特征,然后使用这些特征来训练模型。所以我有点困惑。 - Lynn
是的,这就是你必须做的。可以这样想:如果你没有在这些特征上训练模型,那么你如何选择重要的特征呢?你怎么知道哪个是重要的?一个特征只有在模型中才能成为重要的因素,而重要性取决于该特征对模型预测能力的影响程度。 - G. Anderson
显示剩余6条评论
1个回答

4

经过调整的互信息得分(adjusted_mutual_info_score)将真实标签与分类器预测的标签进行比较。这两个标签数组必须具有相同的形状(nsamples,)。

如果您想要实现的目标是获取估计的每个特征与目标之间的相互信息,请使用Scikit-Learn的mutual_info_classif。将特征数组和相应的标签传递给mutual_info_classif,即可返回估计的每个特征与目标之间的相互信息。

import numpy as np
import pandas as pd

from sklearn.feature_selection import mutual_info_classif
from sklearn.datasets import make_classification

# Generate a sample data frame
X, y = make_classification(n_samples=1000, n_features=4,
                           n_informative=2, n_redundant=2,
                           random_state=0, shuffle=False)
feature_columns = ['A', 'B', 'C', 'D']
features = pd.DataFrame(X, columns=feature_columns)

# Get the mutual information coefficients and convert them to a data frame
coeff_df =pd.DataFrame(mutual_info_classif(X, y).reshape(-1, 1),
                         columns=['Coefficient'], index=feature_columns)

输出

features.head(3)
Out[43]: 
          A         B         C         D
0 -1.668532 -1.299013  0.799353 -1.559985
1 -2.972883 -1.088783  1.953804 -1.891656
2 -0.596141 -1.370070 -0.105818 -1.213570

# Displaying only the top two features. Adjust the number as required.
coeff_df.sort_values(by='Coefficient', ascending=False)[:2]

Out[44]: 
   Coefficient
B     0.523911
D     0.366884

那么在这里,make_classification函数是做什么的?n_informative和random_state参数又有什么作用呢?X和y又代表什么意思呢?如果我理解正确的话,X是所有的训练数据,而y则是“X”中记录的标签?那么如何按照信息增益得分的升序,在表格中显示出前10个最佳特征呢? - Lynn
它告诉我int()参数必须是字符串、类似字节的对象或数字,而不是'DataFrame'。 - Lynn
我必须使用make_classification吗? - Lynn
当然不是,这只是为了举例。我正在重新整理答案,希望它会变得更清晰。 - KRKirov
我更新了我的帖子以展示我尝试过的内容,但是在coeff_df中得到了很多NaN。 - Lynn
显示剩余4条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接