Skip to content

Decision tree with string values #8

@Neodrox

Description

@Neodrox

Hey there how are you,

I am new to python and doing PBL learning, I was looking at the challenge with the classifier, and wanted to run the same decision tree but with other classifiers as string values to determine either male or female by Movie title, hair length and food. Here is the code to see if you can help me, and this is the error I get when running it.

Code:

from sklearn import tree

clf = tree.DecisionTreeClassifier()

#[movie, hair, food]
X = [['Black Panther', 'Long', 'Burger'],
['Avengers', 'Short', 'Taco'],
['96', 'Long', 'Pizza'],
['Spiderman', 'Long', 'Burrito'],
['Roma', 'Short', 'Cheesesteak'],
['Greenbreak', 'Long', 'Icecream'],
['A star is Born', 'Long', 'Hot Pocket'],
['The favourite', 'Short', 'Nachos'],
['A quiet place', 'Long', 'Salad'],
['Ant-man', 'Short', 'Pop-corn'],
['Solo', 'Long', 'Chocolate']]

Y = ['male', 'male', 'female', 'female', 'male', 'male', 'female', 'female',
'female', 'male', 'male']

clf = clf.fit(X, Y)

prediction = clf.predict([['Mary Poppins','Long','Candy']])

print(prediction)

Error:

Traceback (most recent call last):
File "D:\Python\demo2.py", line 21, in
clf = clf.fit(X, Y)
File "D:\Python\lib\site-packages\sklearn\tree\tree.py", line 801, in fit
X_idx_sorted=X_idx_sorted)
File "D:\Python\lib\site-packages\sklearn\tree\tree.py", line 116, in fit
X = check_array(X, dtype=DTYPE, accept_sparse="csc")
File "D:\Python\lib\site-packages\sklearn\utils\validation.py", line 527, in check_array
array = np.asarray(array, dtype=dtype, order=order)
File "D:\Python\lib\site-packages\numpy\core\numeric.py", line 538, in asarray
return array(a, dtype, copy=False, order=order)
ValueError: could not convert string to float: 'Black Panther'

capture

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions