获取在特征选择方法之后选择的列名

Getting the column names chosen after a feature selection method

下面给出了一个简单的特征选择代码,我想知道特征选择后选择的列(数据集包括一个header V1 ... V20

import pandas as pd
from sklearn.feature_selection import SelectFromModel, SelectKBest, f_regression


def feature_selection(data):
    y = data['Class']
    X = data.drop(['Class'], axis=1)
    fs = SelectKBest(score_func=f_regression, k=10)

    # Applying feature selection
    X_selected = fs.fit_transform(X, y)
    # TODO: determine the columns being selected

    return X_selected


data = pd.read_csv("../dataset.csv")
new_data = feature_selection(data)

感谢您的帮助。

我在示例中使用了 iris 数据集,但您可以轻松修改代码以匹配您的用例。 SelectKBest 方法具有我用来对特征进行排序的 scores_ 属性。

如有任何疑问,请随时提出。

import pandas as pd
import numpy as np
from sklearn.feature_selection import SelectFromModel, SelectKBest, f_regression
from sklearn.datasets import load_iris


def feature_selection(data):
    y = data[1]
    X = data[0]
    column_names = ["A", "B", "C", "D"]  # Here you should use your dataframe's column names
    k = 2

    fs = SelectKBest(score_func=f_regression, k=k)

    # Applying feature selection
    X_selected = fs.fit_transform(X, y)

    # Find top features 
    # I create a list like [[ColumnName1, Score1] , [ColumnName2, Score2], ...]
    # Then I sort in descending order on the score
    top_features = sorted(zip(column_names, fs.scores_), key=lambda x: x[1], reverse=True)
    print(top_features[:k])

    return X_selected


data = load_iris(return_X_y=True)
new_data = feature_selection(data)

我不知道内置方法,但可以轻松编码。

n_columns_selected = X_new.shape[0]
new_columns = list(sorted(zip(fs.scores_, X.columns))[-n_columns_selected:])
# new_columns order is perturbed, we need to restore it. We use the names of the columns of X as a reference
new_columns = list(sorted(cols_new, key=lambda x: list(X.columns).index(x)))