I am trying to copy some code from a video to do a decision tree program, which will predict if a student will pass or not depending on 30 parameters given. I did exactly as written but get an error as below:
import pandas as pd import numpy as np from sklearn import tree import graphviz #importing the data set d = pd.read_csv('student-por.csv', sep= ';') #Each 'G' grade is out of 20 #Setting the pass mark as 35 /60 d['pass'] = d.apply(lambda row: 1 if (row['G1']+ row['G2']+ row ['G3']) >= 35 else 0 , axis=1) d = d.drop(['G1', 'G2','G3'], axis=1 ) #shuffle rows d = d.sample(frac=1) #split traning and test d_train = d[:500] d_test = d[500:] # to be used in .fit d_train_att = d_train.drop(['pass'], axis=1) d_train_pass= d_train['pass'] #I don't know why he did this one d_test_att = d_test.drop(['pass'], axis=1) d_test_pass= d_test['pass'] d_att = d.drop(['pass'], axis=1) d_pass = d['pass'] #Calculating how many students passed print ('passing: %d out of %d (%.2f%%)'%(np.sum(d_pass), len(d_pass), 100*float(np.sum(d_pass)/len(d_pass))) ) t = tree.DecisionTreeClassifier(criterion ='entropy', max_depth = 5) t= t.fit (d_train_att, d_train_pass) # To visualize the decision tree dot_data = tree.export_graphviz(t,out_file = None, label ='all', imputiry=False, proportion= True, feature_names=list(d_train_att), class_names=['fail', 'pass'], filled = True, rounded=True) graph = graphviz.Source (dot_data)
And the output is:
Traceback (most recent call last): File "students.py", line 29, in <module> t= t.fit (d_train_att, d_train_pass) File "/home/mohamed/.virtualenvs/cv/lib/python3.5/site-packages/sklearn/tree/tree.py", line 790, in fit X_idx_sorted=X_idx_sorted) File "/home/mohamed/.virtualenvs/cv/lib/python3.5/site-packages/sklearn/tree/tree.py", line 116, in fit X = check_array(X, dtype=DTYPE, accept_sparse="csc") File "/home/mohamed/.virtualenvs/cv/lib/python3.5/site-packages/sklearn/utils/validation.py", line 433, in check_array array = np.array(array, dtype=dtype, order=order, copy=copy) ValueError: could not convert string to float: 'no'
If possible, could you please explain to me how to fix it?
edit: solved it after the explanation of pcko1. I used
pd.get_dummies to get rid of floats.