【问题标题】:How do I use feature importances with forest of trees from scikit-learn?如何在 scikit-learn 的森林中使用特征重要性?
【发布时间】:2020-10-15 09:07:09
【问题描述】:

我是 Python 新手,正在尝试将我的数据导入到 the scikit-learn example code。我添加并编辑了一些行:

data=open("./data.csv")
headernames=data.readline()
dataset=pd.read_csv("./data.csv")
dataset.head()

from sklearn.ensemble import RandomForestClassifier

X,y = dataset(n_samples=1000,
        n_features=18,
        n_informative=3,
        n_redundant=0,
        n_repeated=0,
        n_classes=2,
        random_state=0,
        shuffle=False)

# Build a forest and compute the impurity-based feature importances
forest = RandomForestClassifier(n_estimators=50,
                              random_state=0)

为了处理具有 18 个特征的 csv 文件:

A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,CATEGORY
1.215086E-4,0.02519655,24.835007,0.63186175,23.133001,0.048536107,21.718653,18.086424,58120.42,1.7020054,1.4143486,1.8447876,0.9806118,0.49139786,0.31718254,-0.0028839111,0.0011329651,1.0E-4 ,NEW
1.172429E-4,0.09888512,24.510912,0.4730912,23.124096,0.04912185,21.834633,19.374733,17750.31,1.386816,1.289463,1.3134937,0.716959,0.317791,0.19662476,0.18844986,-0.27341843,2.0E-4 ,NEW
1.808221E-4,0.06917137,22.345144,0.06795227,19.776121,0.002399989,18.459005,17.168722,135346.22,2.5690231,1.3171158,0.6007366,0.40520668,0.17715263,0.13420677,0.16839981,-0.19541931,2.0E-4 ,NEW
1.711274E-4,0.05774665,22.327171,0.06615994,20.04997,0.0030316357,19.093689,18.546497,38058.99,2.2772007,0.95628166,0.3409214,0.24905205,0.0615139,0.1291523,0.14673615,-0.38018417,4.0E-4 ,NEW
1.630188E-4,0.1470628,24.277617,0.40649226,21.303617,0.009781063,19.82426,17.663696,85836.8,2.973999,1.4793568,1.1171284,0.6929569,0.25898743,0.21943283,0.0052948,-0.13323593,6.0E-4 ,NEW

错误信息:

None
/usr/lib64/python3.7/site-packages/sklearn/externals/six.py:7: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
  import imp
/usr/lib64/python3.7/site-packages/sklearn/utils/__init__.py:4: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated since Python 3.3,and in 3.9 it will stop working
  from collections import Sequence
/usr/lib64/python3.7/site-packages/sklearn/ensemble/weight_boosting.py:29: DeprecationWarning: numpy.core.umath_tests is an internal NumPy module and should not be imported. It will be removed in a future NumPy release.
  from numpy.core.umath_tests import inner1d
Traceback (most recent call last):
  File "features_importance.py", line 32, in <module>
    shuffle=False)

【问题讨论】:

  • 看起来像 DeprecationWarning(我的意思是“警告”,而不是错误)
  • 是的,但是代码无论如何都不起作用。

标签: python scikit-learn


【解决方案1】:

您误解了链接中make_classification 的用途。这生成具有函数参数中描述的属性的数据集。由于您正在从 csv 加载数据集,因此您不需要任何这些。相反,您只需要将 pandas 数据框拆分为 X, y,例如:

X = dataset.drop('CATEGORY', axis='columns')
y = dataset['CATEGORY']

【讨论】:

  • 感谢您的回答。所以我不应该使用X,y = dataset(n_samples=1000, n_features=18, n_informative=3, n_redundant=0, n_repeated=0, n_classes=2, random_state=0, shuffle=False) 部分,而是以不同的方式定义 X 和 y 吗?对于 CATEGORY,我应该从第一行给出它的名称还是其他名称?而对于“列”应该是它的数量还是只是“列”这个词?在哪里包含文件名?
猜你喜欢
  • 2017-02-08
  • 2015-11-14
  • 2015-12-16
  • 2016-06-02
  • 2017-11-04
  • 2016-10-19
  • 2015-09-20
  • 2021-05-09
  • 2018-04-01
相关资源
最近更新 更多