0

I have a data set(19 attributes and 1700 instances),I am training Logistic regression, decision tree, random forest on it. logistic regression works fine but when I try decision trees I get the error:

Expected 2D array, got 1D array instead:
array=[1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0.
 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 1. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 0. 0. 0. 0.
data = data.interpolate()
print(data.isnull().sum())

data['Year_upd'].fillna('0', inplace=True)
data['Month_upd'].fillna('0', inplace=True)
data['Day_upd'].fillna('0', inplace=True)
data['Hour_upd'].fillna('0', inplace=True)

print(data.isnull().sum())

x = data.drop(labels='Type', axis='columns')
y = data.iloc [:, 13]
x.head()

x_train, x_test, y_train, y_test = train_test_split(x, y ,test_size = 0.2,)
model = LogisticRegression(max_iter = 4000)
model.fit (x_train, y_train)

model3= tree.DecisionTreeClassifier()
model3.fit(x_train,y_train)

I tried using the following code to fix the issue

model3= tree.DecisionTreeClassifier()
model3.fit(x_train.values.reshape(-1, 1),y_train)

and I still get: Number of labels=1424 does not match number of samples=29904

1 Answer 1

0

x_train should be a 2D features matrix with shape (n_instances, n_features). I would recommend converting the tables to NumPy arrays before splitting and testing, like so:

x = data.drop(labels='Type', axis='columns').to_numpy()
y = data.iloc[:, 13].to_numpy()

# ...
model3= tree.DecisionTreeClassifier()
model3.fit(x_train, y_train)
Sign up to request clarification or add additional context in comments.

6 Comments

Hi, I did as you suggested but I still get the same error.
Number of labels=1424 does not match number of samples=29904
What are the shapes of x_train and y_train?
y_train.shape (1424,) (x_train.shape) (1424, 21)
Then it should be working. Are you still flattening the array or something? Obviously you need to remove the reshape(-1, 1) if that's still present.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.