Unknown label type sklearn

12,422

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

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

Related videos on Youtube

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

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

Updated on June 04, 2022

Comments

  • Руслан Вергунов
    Руслан Вергунов almost 2 years

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

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

    What is a problem?

  • Руслан Вергунов
    Руслан Вергунов over 7 years
    Thanks for your answer!
  • sgelves
    sgelves almost 7 years
    Why isn't as_matrix recommended?
  • sgelves
    sgelves almost 7 years
    I found an answer to my question, tanks pandas.pydata.org/pandas-docs/stable/generated/…