【问题标题】:Feature importance in Python using SelectKBest使用 SelectKBest 在 Python 中的特征重要性
【发布时间】:2021-01-18 05:09:14
【问题描述】:

我正在尝试使用 X_train 和 y_train 获取我的数据帧 df 的前 5 个功能。

bestfeatures = SelectKBest(score_func=chi2, k=5) #k=5 means select top 5 features
fit = bestfeatures.fit(X_train,y_train)
dfscores = pd.DataFrame(fit.scores_)
dfcolumns = pd.DataFrame(X_train.columns)
#concat two dataframes for better visualization 
featureScores = pd.concat([dfcolumns,dfscores],axis=1)
featureScores.columns = ['Features','Score']  #naming the dataframe columns
print(featureScores.nlargest(5,'Score'))  #print 5best features

错误

ValueError                                Traceback (most recent call last)
<ipython-input-54-47286ab0e6e9> in <module>
      6 
      7 bestfeatures = SelectKBest(score_func=chi2, k=5)
----> 8 fit = bestfeatures.fit(X_train,y_train)
   
    ValueError: Unknown label type: (array([23.5, 35, 38.......
   .......]),)      

附:我的 Y_train 是 23.5 , 35, 38 等等...如 valueerror

如何解决?

【问题讨论】:

    标签: python pandas dataframe regression feature-selection


    【解决方案1】:

    你的评分函数是chi2,所以你是在做分类,而不是回归。因此,您必须在有限空间中传递值(例如:字符串、整数等);浮点数只能用于回归。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-24
      • 1970-01-01
      • 2014-02-17
      • 2017-10-21
      • 2022-06-20
      • 2022-01-19
      • 2019-06-30
      相关资源
      最近更新 更多