0

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.

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

1 Comment

What an unexpected problem I had. My mind was miles away. Thanks!

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.