获取 SelectKBest 函数的特征名称 python
get feature names of SelectKBest function python
我从 sklearn 实现了 SelectKBest,我想获得 K 个最佳列的名称,而不仅仅是每个列的值。
我需要做什么?
我的代码:
X_new = SelectKBest(chi2, k=2).fit_transform(X, y)
X_new.shape
X_new 是一个 numpy.ndarray 并且它有 k col 但没有 col 名称。
model=SelectKBest(k=5)
model.fit(X,y)
print(model.get_params)
您可以获得所选特征的索引。
示例 1:
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
selector = SelectKBest(chi2, k=2)
selector.fit(X, y)
X_new = selector.transform(X)
X_new.shape
print(selector.get_support(indices=True))
现在如果你真的想得到我们需要使用的列的实际名称pandas。
示例 2:
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)
print(X_new.shape)
X.columns[selector.get_support(indices=True)]
# 1st way to get the list
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)']
model=SelectKBest(f_classif, k=8).fit(X,Y)
Selected_feature_names=X.columns[model.get_support()]
我从 sklearn 实现了 SelectKBest,我想获得 K 个最佳列的名称,而不仅仅是每个列的值。
我需要做什么?
我的代码:
X_new = SelectKBest(chi2, k=2).fit_transform(X, y)
X_new.shape
X_new 是一个 numpy.ndarray 并且它有 k col 但没有 col 名称。
model=SelectKBest(k=5)
model.fit(X,y)
print(model.get_params)
您可以获得所选特征的索引。
示例 1:
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
selector = SelectKBest(chi2, k=2)
selector.fit(X, y)
X_new = selector.transform(X)
X_new.shape
print(selector.get_support(indices=True))
现在如果你真的想得到我们需要使用的列的实际名称pandas。
示例 2:
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)
print(X_new.shape)
X.columns[selector.get_support(indices=True)]
# 1st way to get the list
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)']
model=SelectKBest(f_classif, k=8).fit(X,Y)
Selected_feature_names=X.columns[model.get_support()]