未知的标签类型sklearn

时间:2016-11-05 18:41:17

标签: python pandas scikit-learn

我是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)

我看到了这个错误

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

什么问题?

1 个答案:

答案 0 :(得分:1)

您目前正在提供数据框,而不是它的numpy数组表示作为fit方法的训练输入。这样做:

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