Efi Kir
Efi Kir

Reputation: 3

ValueError: Expected 2D array, got 1D array instead insists after converting 1D array to 2D

I looked into other answers but still cannot understand why the problem insists.

Classic machine learning practice with Iris dataset.

Code:

dataset=load_iris()

X = np.array(dataset.data)
y = np.array(dataset.target)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

model = KNeighborsClassifier()
model.fit(X_train, y_train)

prediction=model.predict(X_test)

The shapes of all arrays:

  1. X shape: (150, 4)
  2. y shape: (150,)
  3. X_train: (105, 4)
  4. X_test: (45, 4)
  5. y_train: (105,)
  6. y_test (45,)
  7. prediction: (45,)

Trying to print this model.score(y_test, prediction) and I get the error. I tried to convert y_test and prediction into 2D arrays by using .reshape(-1,1) and I get another error: query data dimension must match training data dimension.

It's not only about the solution, it's about understanding what's wrong.

Upvotes: 0

Views: 452

Answers (1)

NickHilton
NickHilton

Reputation: 682

Its often useful to look at the signature and docstrings for functions you use. model.score for example has

Parameters
----------
X : array-like, shape = (n_samples, n_features)
    Test samples.

y : array-like, shape = (n_samples) or (n_samples, n_outputs)
    True labels for X.

in the docstrings to show you the exact type of input you should give it.

Here you would do model.score(X_test, y_test)

model.score will do both the prediction from X_test and the comparison with y_test

Upvotes: 0

Related Questions