In a dataset of longitude, latitude and price (of houses) I'm using sklearn's KNearestRegressor to get the 5 nearest neighbors mean price for each point. The problem is I want to do this for the whole dataset and each point is taking its own price into consideration since KNearestRegressor is a supervised algorithm, and I'm predicting the training set. How can I get the 5 nearest neighbors mean price for each point, not taking into consideration that point's price?

I have tried sklearn's unsupervised NearestNeighbours but it only outputs the indexes of the nearest neighbors, not the mean price.


1 Answer 1


First of all you train/ fit a model on the training set, may be cross validate using a training set. But it is not meant to predict itself. That's what test set is for.

You can achieve your objective by predicting each observation in a Leave One Out manner (also used for cross validation):

For every observation (i) in your dataset, fit a KNN regressor using all data except (i), evaluate your prediction for (i).


Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.