【发布时间】: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