Act fast, special offers end soon! Up to $294 is savings when you get Premium today.
Get offer codes

Univariate K-Nearest Neighbors Models error

Screen Link:

<ipython-input-25-b09f94ed8e64>:29: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.
  mse = mean_squared_error(test[[target_col]], price_predict[[target_col]])

While working on Guided Project: Predicting Car Prices. I got error below i have tried to resolve but o head way. Kindly support. Thanks in anticipation

IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

project link is below
Predicting Car Prices.ipynb (42.9 KB)

As your error points out, it occurs in the following line of code -

mse = mean_squared_error(test[[target_col]], price_predict[[target_col]])

The error also states -

It’s an IndexError, so, that means that it relates to some mistake you are making when you try to index.

In your code line above, there are two instances where you try to index something -

  • test[[target_col]]
  • price_predict[[target_col]]

Print those two out separately and see which one might be the source for the issue.

Thanks for your support. I tried implementing your suggestions but no head way yet

If you print out price_predict[[target_col]] you will get the same error. So, that’s the source of the problem.

The next step is to try and see why. If you print out price_predict, you will get a numpy array of shape (100, 1) - 100 rows and 1 column. You can print out the shape directly by printing price_predict.shape.

The array does not have a column named target_col.

That’s why price_predict[[target_col]] throws an error because you are trying to incorrectly index price_predict.

If you check the Scikit-learn’s documentation for MSE, this is the expected 2nd argument -

y_pred array-like of shape (n_samples,) or (n_samples, n_outputs)

So, the correct 2nd argument would be price_predict.

@the_doctor I sincerely appreciate your effort towards supporting me. However, you explanation has not helped me to resolve my issue.

I am still getting the same error. I will appreciate further support

I downloaded and ran your code with the fix I suggested above. It runs without any errors after that.

I would recommend making sure you are running all required code cells after making the necessary changes.

Thank you so much. Issue fixed