August 17, 2020
K-Nearest Neighbor Regressor
KNN is typically used for classification, but it can also perform regression.
The process is almost identical to classification, except for the final step.
Instead of counting a 1 vs 0, the regressor takes the average of the values, like an average of IMDB ratings.
We can also compute a weighted average based on how close each neighbor is, so that the closest neighbor has more weight on the prediction.
Scikit-learn has a built-in K-Nearest Neighbor Regression model.
KNeighborsRegressor is very similar to KNeighborsClassifier. We can choose whether or not to use a weighted average using the parameter “weights.” If weights = “uniform,” all neighbors will be considered equally. If it = “distance,” then a weighted average is used.
Create the regressor:
classifier = KNeighborsRegressor(n_neighbors = 3, weights = “distance”)
Fit the model to the training data:
classifier.fit(training_data, training_labels)
Make predictions on new data points
predictions = classifier.predict(unknown_points)