ValueError: Expected 2D array, got 1D array instead: x and y values are 2 data types

69 Views Asked by At

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

There are 2 best solutions below

2
chc On

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.

0
Sasani Perera On

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.