python-如何正确选择k个最佳数值特征?

时间:2021-08-02 00:31:50

标签: python pandas machine-learning scikit-learn

我正在尝试将 SelectKBest() 函数应用于名为 x_train 的 Pandas 数据帧中的特定连续数字特征,同时标签列被定义为名为 {{ 的二元响应变量 (1,0) 列1}}:

y_train

然而,当应用 from sklearn.metrics import mutual_info_score from sklearn.feature_selection import SelectKBest, f_classif numerical_features=['col1', 'col2'] ######################################################################## def get_numerical_features(features, class_label): class_label=pd.DataFrame(class_label) fs=SelectKBest(f_classif, k='all') for feature in features: fs.fit(class_label, feature) return(print('Feature %d: %f' % (feature, fs.scores_[feature]))) ####################################################################### # applying the function get_numerical_features(features=x_train[numerical_features], class_label=y_train) 时,输出是下一个:

<块引用>

TypeError: Singleton array array('col1', dtype='

我错过了什么?

有没有办法将每一列转换为有效的集合?

数据演示

get_numerical_features()

1 个答案:

答案 0 :(得分:1)

<块引用>

我错过了什么?

fs.scores_ 实际上是一个形状为 (2,) 的数组,您无法使用 feature 对其进行索引。试试:

from sklearn.metrics import mutual_info_score
from sklearn.feature_selection import SelectKBest, f_classif

numerical_features=['col1', 'col2']

def get_numerical_features(features, class_label):
    #class_label is already a Dataframe in your data demo
    fs=SelectKBest(f_classif, k='all')
    fs.fit(features, class_label) # this should be here 

    for i, feature in zip(range(len(features)), features): 
        print('Feature %s: %f' % (feature, fs.scores_[i]))
    
    
# applying the function
x_train = pd.DataFrame({'col1': [1, 2, 7, 10, 2], 'col2': [3, 4, 27, 3, 1]})
y_train = pd.DataFrame({'label': [0, 0, 0, 1, 1]})

get_numerical_features(features=x_train[numerical_features], class_label=y_train['label']) 

#output: 
#Feature col1: 0.486076
#Feature col2: 0.846043
<块引用>

有没有办法将每一列转换为有效的集合?

为此,您可以使用 fit_transform 自动选择得分最高的 k 功能。

fs = SelectKBest(f_classif, k=1) # with `all` all features will be selected, default=10
x_train_new = fs.fit_transform(features, class_label)
print(x_train_new) # since k=1, this prints the values of col2 wich has high score
相关问题