【发布时间】:2021-01-28 06:31:07
【问题描述】:
我想使用 Information Gain 度量(scikit-learn 中的 Mutual Info)识别 Dataframe 的 10 个最佳特征,并将它们显示在表格中(根据 Information Gain 获得的分数升序排列)。
在此示例中,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()
使用 Mutual Info 分数实现此目的的正确语法是什么?
【问题讨论】:
-
您尝试过什么,您的尝试出了什么问题? sklearn user guide on clusterin 有语法和使用示例
-
在我的例子中,labels_true 和 labels_pred 是我的 train_features 和 test_features 吗?
-
在这些示例中显示 0101 时会让人感到困惑
-
labels_true 和 labels_pred 是实际的 y 值,是模型预测的 y 值。因此,在您的情况下,
labels_true将是test_labels,labels_pred将是调用model.predict(test_features)返回的标签(当然,假设您已经先将模型拟合到输入特征和标签) -
@G.Anderson 哦,所以我需要先训练一个模型?像决策树分类器或随机森林?我以为那部分是在后面。在我看来,这更像是:我使用信息增益度量选择数据帧的 10 个最佳特征,然后使用这些特征来训练模型。所以我很困惑
标签: python pandas dataframe scikit-learn feature-selection