逻辑回归:如何找出权重最高的前三个特征?

我正在使用UCI乳腺癌数据集,并试图找出权重最高的前三个特征。我已经能够使用logmodel.coef_找到所有特征的权重,但如何获取特征名称呢?以下是我的代码、输出和数据集(从scikit导入的)。

from sklearn.model_selection import train_test_splitfrom sklearn.datasets import load_breast_cancerfrom sklearn.linear_model import LogisticRegressioncancer = load_breast_cancer()X_train, X_test, y_train, y_test = train_test_split(    cancer.data, cancer.target, stratify=cancer.target, random_state=42)logmodel = LogisticRegression(C=1.0).fit(X_train, y_train)logmodel.coef_[0]

上述代码输出权重数组。使用这些权重,我如何获取相关的特征名称?

Output:    array([  1.90876683e+00,   9.98788148e-02,  -7.65567571e-02,             1.30875965e-03,  -1.36948317e-01,  -3.86693503e-01,            -5.71948682e-01,  -2.83323656e-01,  -2.23813863e-01,            -3.50526844e-02,   3.04455316e-03,   1.25223693e+00,             9.49523571e-02,  -9.63789785e-02,  -1.32044174e-02,            -2.43125981e-02,  -5.86034313e-02,  -3.35199227e-02,            -4.10795998e-02,   1.53205924e-03,   1.24707244e+00,            -3.19709151e-01,  -9.61881472e-02,  -2.66335879e-02,            -2.44041661e-01,  -1.24420873e+00,  -1.58319440e+00,            -5.78354663e-01,  -6.80060645e-01,  -1.30760323e-01])

谢谢。任何帮助我都会非常感激。


回答:

这将完成任务:

import numpy as npcoefs=logmodel.coef_[0]top_three = np.argpartition(coefs, -3)[-3:]print(cancer.feature_names[top_three])

这将输出

['worst radius' 'texture error' 'mean radius']

请注意,这些特征是前三名,但它们之间不一定是排序的。如果你希望它们排序,可以这样做:

import numpy as npcoefs=logmodel.coef_[0]top_three = np.argpartition(coefs, -3)[-3:]top_three_sorted=top_three[np.argsort(coefs[top_three])]print(cancer.feature_names[top_three_sorted])

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注