【问题标题】:python-how to properly select k best numerical features?python-如何正确选择k个最佳数值特征?
【发布时间】:2021-10-07 10:30:27
【问题描述】:

我正在尝试将 SelectKBest() 函数应用于来自名为 x_train 的 pandas 数据帧的特定连续数值特征,同时标签列被定义为名为 y_train 的二进制响应变量 (1,0) 列:

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)

但是当get_numerical_features()被应用时,输出是下一个:

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

我错过了什么?

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

数据演示

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]})

【问题讨论】:

    标签: python pandas machine-learning scikit-learn


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 2014-09-16
      • 2012-05-09
      • 2018-12-15
      • 2021-07-26
      • 2017-03-18
      • 1970-01-01
      • 2016-03-16
      • 2012-04-20
      • 2016-01-27
      相关资源
      最近更新 更多