2017-09-15 158 views
0

在scikit-learn中运行特征选择后,我想公开相关变量,向我显示从方法中选择的变量,它怎么可能?命令X.shape只显示变量的数量,我想在功能选择后看到变量的名称。特征选择Scikit学习

from sklearn.datasets import load_iris 
from sklearn.feature_selection import SelectKBest 
from sklearn.feature_selection import chi2 

iris = load_iris() 

X, y = iris.data, iris.target 

X.shape 

X_new = SelectKBest(chi2, k=2).fit_transform(X, y) 

X_new.shape 
+0

有可能通过使用大熊猫。看到我的答案 – sera

回答

0
from sklearn.datasets import load_iris 

from sklearn.feature_selection import SelectKBest 

from sklearn.feature_selection import chi2 

iris = load_iris() 

X, y = iris.data, iris.target 

X.shape 

skb = SelectKBest(chi2, k=2) 
skb.fit(X, y) 
X_new = skb.transform(X) 

X_new.shape 

print skb.get_support(indices=True) 

这会给你的选择的功能指标。

0

您可以获取姓名,但需要使用熊猫并将numpy转换为数据框。

from sklearn.datasets import load_iris 
from sklearn.feature_selection import SelectKBest 
from sklearn.feature_selection import chi2 
import pandas as pd 

iris = load_iris() 
X = pd.DataFrame(iris.data, columns=iris.feature_names) 
y = pd.DataFrame(iris.target) 

selector = SelectKBest(chi2, k=2) 
selector.fit(X, y) 

X_new = selector.transform(X) 
X_new.shape 

#text format 
X.columns[selector.get_support(indices=True)] 
#vector format 
vector_names = list(X.columns[selector.get_support(indices=True)]) 

print(vector_names) 

#2nd way 
X.columns[selector.get_support(indices=True)].tolist() 

结果

Index([u'petal length (cm)', u'petal width (cm)'], dtype='object') 
['petal length (cm)', 'petal width (cm)'] 
['petal length (cm)', 'petal width (cm)']