【问题标题】:UserWarning: X does not have valid feature names, but DecisionTreeClassifier was fitted with feature namesUserWarning:X 没有有效的特征名称,但 DecisionTreeClassifier 配备了特征名称
【发布时间】:2022-11-27 07:48:08
【问题描述】:

我正在使用 Mosh 频道编程学习机器学习。 在这种情况下我得到了想要的输出。

output=array(['HipHop', 'Acoustic', 'Classical'], dtype=object)

但是有这样的警告,我找不到哪一部分是错误的。

C:\Users\User\anaconda3\lib\site-packages\sklearn\base.py:450: UserWarning: X does not have valid feature names, but DecisionTreeClassifier was fitted with feature names
  warnings.warn(

你知道我该如何纠正这个问题吗?

代码:

import pandas as pd
from sklearn.tree import DecisionTreeClassifier
music_data=pd.read_csv('C:\\Users\\User\\Desktop\\machine learning tutorial\\Python Tutorial Supplementary Materials\\music.csv')
y=music_data['genre']
X=music_data.drop(columns=['genre'])

model = DecisionTreeClassifier()
model.fit(X,y)
predictions=model.predict([[22,1],[26,0],[39,1]])
predictions

【问题讨论】:

  • 您的问题已经解决 here

标签: python scikit-learn decision-tree


【解决方案1】:

在第 5 行之后,在“model = DecisionTreeClassifier”之前再添加两行:

X = X.values
y = y.values

可以在此处找到更深入的解决方案和解释: UserWarning: X does not have valid feature names, but LogisticRegression was fitted with feature names

【讨论】:

    猜你喜欢
    • 2022-11-25
    • 2022-10-07
    • 2022-11-07
    • 1970-01-01
    • 2022-01-19
    • 2012-12-17
    • 2020-03-01
    • 2020-07-27
    • 2017-11-14
    相关资源
    最近更新 更多