pandas 未知标签类型 sklearn

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40441857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:21:42  来源:igfitidea点击:

Unknown label type sklearn

pythonpandasscikit-learn

提问by Руслан Вергунов

I 'm new in sklearn. I 'm trying to do this code

我是 sklearn 的新手。我正在尝试执行此代码

data = pandas.read_csv('titanic.csv')
data= data[data['Pclass'].notnull() & data['Sex'].notnull() &         data['Age'].notnull() & data['Fare'].notnull()]   
test = data.loc[:,['Pclass','Sex','Age','Fare']]
target = data.loc[:,['Survived']]
test = test.replace(to_replace=['male','female'],value=[1,0])
clf=DecisionTreeClassifier(random_state=241)
clf.fit(target,test)

And I saw this error

我看到了这个错误

ValueError: Unknown label type: array([[ 22.    ,   3.    ,   7.25  ,        1.    ],
   [ 38.    ,   1.    ,  71.2833,   0.    ],
   [ 26.    ,   3.    ,   7.925 ,   0.    ],
   ..., 
   [ 19.    ,   1.    ,  30.    ,   0.    ],
   [ 26.    ,   1.    ,  30.    ,   1.    ],
   [ 32.    ,   3.    ,   7.75  ,   1.    ]])
ValueError: Unknown label type: array([[ 22.    ,   3.    ,   7.25  ,        1.    ],
   [ 38.    ,   1.    ,  71.2833,   0.    ],
   [ 26.    ,   3.    ,   7.925 ,   0.    ],
   ..., 
   [ 19.    ,   1.    ,  30.    ,   0.    ],
   [ 26.    ,   1.    ,  30.    ,   1.    ],
   [ 32.    ,   3.    ,   7.75  ,   1.    ]])

What is a problem?

什么是问题?

采纳答案by Nickil Maveli

You are currently providing a dataframe and not it's numpy array representation as the training input to the fitmethod. Do this instead:

您当前提供的是一个数据帧,而不是它的 numpy 数组表示作为该fit方法的训练输入。改为这样做:

clf.fit(X=test.values, y=target.values)   
# Even .asmatrix() works but is not generally recommended