Python 3.x 如何从特征列表和权重系数列表中选择前10个特征(逻辑回归)?

Python 3.x 如何从特征列表和权重系数列表中选择前10个特征(逻辑回归)?,python-3.x,machine-learning,logistic-regression,feature-selection,Python 3.x,Machine Learning,Logistic Regression,Feature Selection,我想在我的逻辑回归模型中选择前5个特征。我现在有两个数组,一个包含所有的功能名称,另一个列表包含model.coef u其中model=LogisticRegression()中的系数 本文件如下: [ 2.07587361e-04 5.59531750e-04 0.00000000e+00 0.00000000e+00 -5.16353886e-02 ...... 1.66633057e-02] #this also has 108 elements 当我尝试对系数值进行排序时,

我想在我的逻辑回归模型中选择前5个特征。我现在有两个数组,一个包含所有的功能名称,另一个列表包含model.coef u其中model=LogisticRegression()中的系数

本文件如下:

[ 2.07587361e-04  5.59531750e-04  0.00000000e+00  0.00000000e+00
-5.16353886e-02 ......  1.66633057e-02]   #this also has 108 elements
当我尝试对系数值进行排序时,会得到不同的值

sorted_index = np.argsort(coefficents[0])
print(sorted_index)
[ 22  91  42  15  52  31  16  32  86 .... 17 106]   #this has 108 values

如何从这两个数组中获得正确的前5个重要功能?

argsort
是按升序排序的,您希望按降序排序(最高优先)

这里我给大家举一个简单的例子:

import numpy as np

feature_list = ['ball', 'cat', 'apple', 'house', 'tree', 'school', 'child']
coeff = np.array([0.7, 0.3, 0.8, 0.2, 0.4, 0.1, 0.9])
# negate the coeff. to sort them in descending order
idx = (-coeff).argsort()
# map index to feature list
desc_feature = [feature_list[i] for i in idx]
# select the top 5 feature
top_feature = desc_feature [:5]
print(top_feature)
结果显示您的顶级功能:

['child', 'apple', 'ball', 'tree', 'cat']
['child', 'apple', 'ball', 'tree', 'cat']