Py学习  »  Python

python randomforest正在将rfe.support转换为列表

EGM8686 • 4 年前 • 286 次点击  

在使用randomforest运行rfe模型之后,我得到一个rfe.support结果(带有true和false的数组)。

如果我做了df.columns[rfe.support\u]我会得到一个保留值列表。如何将此值作为列表获取?x.columns[rfe.support\u]似乎是不可调用的对象。

from sklearn.ensemble import RandomForestClassifier
from sklearn.datasets import load_iris
iris = load_iris()
rfe = RFE(estimator=clf_rf_3, n_features_to_select=2, step=1)
rfe = rfe.fit(iris["data"], iris["target"])
rfe.support_

support是一个具有false和true的数组,我想得到一个与rfe.support数组中的“true”相对应的保留变量列表。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/46554
 
286 次点击  
文章 [ 1 ]  |  最新文章 4 年前
Chiradip Bhattacharya
Reply   •   1 楼
Chiradip Bhattacharya    5 年前

您可以使用to“.toList()”函数作为- list=rfe.support_u.tolist()

现在有了数组项的列表

然后使用for循环从创建的整个列表中筛选出真值,同时保持一个单独的列表作为真值的索引。

列表2=[]

对于范围(1,len(list))中的i:

if(int(list[i])==1):          # for the true values

    list2.append(i)           # the separate list with the index of true value items