0
$\begingroup$

I'm new to data science , while i'm implementing decision tree. I'm facing the following error. Where i went wrong;

Sample data in csv is: enter image description here



x=dataset.iloc[:,:-1].values
y=dataset.iloc[:,:2].values
from sklearn.preprocessing import LabelEncoder,OneHotEncoder
from sklearn.compose import ColumnTransformer
labelencoder_x=LabelEncoder()
x[:,0]=labelencoder_x.fit_transform(x[:,0])
onehotencoder=OneHotEncoder()
columntransformer=ColumnTransformer([('dummy cols',onehotencoder,[0])],remainder='passthrough')
x=columntransformer.fit_transform(x)


from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=1)

from sklearn.tree import DecisionTreeRegressor
regressor=DecisionTreeRegressor(random_state=0)
regressor.fit(x,y)  #Error occurance

Error: ''' ValueError: could not convert string to float: 'Business Analyst' '''

$\endgroup$
10
  • $\begingroup$ You haven't included details of the error you're experiencing $\endgroup$
    – Dan Scally
    Aug 28, 2019 at 7:29
  • $\begingroup$ @DanScally thanks for reminding. I forgot to embed it. I have updated it. $\endgroup$ Aug 28, 2019 at 9:32
  • $\begingroup$ Which line gives you this error ? $\endgroup$
    – ashukid
    Aug 28, 2019 at 10:34
  • $\begingroup$ @ashukid regressor.fit() is giving me the error $\endgroup$ Aug 28, 2019 at 11:02
  • $\begingroup$ You have strings values in x when you call regressor.fit(x,y). All values need to be float or int $\endgroup$ Aug 28, 2019 at 11:09

1 Answer 1

1
$\begingroup$

The problem is with the y value.

x=dataset.iloc[:,:-1].values -> selecting all columns but last #this one is fine

y=dataset.iloc[:,:2].values -> selecting all columns till 2nd #this one is wrong

Change this to :

y=dataset.iloc[:,2].values #selecting only 2nd column.
$\endgroup$
0

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