-1
$\begingroup$

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.

$\endgroup$
1
  • $\begingroup$ Possibly you should convert your labels i.e., d_test_pass and d_train_pass into float before passing them into the fit function e.g., you could assign o (1) to those who passed (rejected). $\endgroup$ – ebrahimi Jul 5 '18 at 9:05
1
$\begingroup$

I cannot see your data (included in student-por.csv) but I suspect that it includes strings (maybe for student names). You should either drop the string variables or convert them to categorical (one character for each different value of the variable).

This means that if you have a column with subject names, you should convert "Maths" to "0", "History" to "1", "Biology" to "2" and so on. A very convenient way of doing this is with the sklearn.preprocessing.LabelEncoder, please check this.

In the end you should either feed continuous or categorical values to your Decision Tree during fit, no strings. Hope it helps :)

$\endgroup$
0
$\begingroup$

The answer by pcko1 is useful in the sense that it will make the code run. But what you exactly require to do is one hot encoding. I say that because encoding the categorical variables that are nominal with increasing numbers like 1, 2, 3 etc. does not make sense. Have a look at this question.

You need to encode all the categorical variables as 0-1. By that I mean, attach one column for each categorical variable in the data set, denoting its presence by a 1 and absence by a 0 in the respective rows of the data set.

#I don't know why he did this one

That has been done to have a separate data set for testing the model after it has been trained over the training set.

Just a reminder.

I am trying to copy a 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.

The correct term should be variables not parameters.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.