0

I am trying to train a very simple model using DecisionTreeClassifier().

iris.csv is my dataset.

dtc1 = DecisionTreeClassifier()
dtc1.fit(x1Train,y1Train)
predicted1 = dtc1.predict(x1Test)
predicted1

data shape

The error I am getting is, ValueError

Is this because my training x and y are numeric and strings?? How can I solve this?

I have tried to convert my x1, y1, and x1Train, y1Train to to_numpy() but the same issue occurs.

Edit: Tried according to the comments.

x1Train.values.reshape(1,-1)
y1Train.values.reshape(1,-1)
dtc1.fit([x1Train],[y1Train]) #this line runs
predicted1 = dtc1.predict(x1Test.reshape(-1,1)) #this line gives error
predicted1

Error getting AttributeError

2 Answers2

0

According to the error message you should do

dtc1 = DecisionTreeClassifier()
dtc1.fit(x1Train,y1Train)
predicted1 = dtc1.predict(x1Test.reshape(-1,1))
predicted1

or

dtc1 = DecisionTreeClassifier()
dtc1.fit(x1Train,y1Train)
predicted1 = dtc1.predict(x1Test.reshape(1,-1))
predicted1

depending on your data.

chc
  • 498
  • 1
  • 4
  • 18
  • I get the Value Error at this point ```dtc1.fit(x1Train,y1Train)```. Tried reshaping x1Train and y1Train. Did not work either. – Sasani Perera Jul 06 '23 at 16:23
  • @SasaniPerera you're getting an `AttributeError` now. That is because you are trying to use the method `reshape` to something that has no such method. It seems that `x1Test` is not a numpy array. Maybe you should convert it first. Something like `dtc1.predict(np.array(x1Test).reshape(-1,1))` – chc Jul 10 '23 at 12:23
0

So the actual issue was assigning data to x1 and y1. It should have been

x1 = iris[['SepalLengthCm']]
y1 = iris[['Species']]

rather than

x1 = iris['SepalLengthCm']
y1 = iris['Species']

Because we are getting data to a pandas DataFrame.