diff --git a/examples/neighbors/plot_regression.py b/examples/neighbors/plot_regression.py index 3cd048c84d51b..431540e81761b 100644 --- a/examples/neighbors/plot_regression.py +++ b/examples/neighbors/plot_regression.py @@ -6,25 +6,26 @@ Demonstrate the resolution of a regression problem using a k-Nearest Neighbor and the interpolation of the target using both barycenter and constant weights. - """ # Authors: The scikit-learn developers # SPDX-License-Identifier: BSD-3-Clause - # %% # Generate sample data # -------------------- +# Here we generate a few data points to use to train the model. We also generate +# data in the whole range of the training data to visualize how the model would +# react in that whole region. import matplotlib.pyplot as plt import numpy as np from sklearn import neighbors -np.random.seed(0) -X = np.sort(5 * np.random.rand(40, 1), axis=0) -T = np.linspace(0, 5, 500)[:, np.newaxis] -y = np.sin(X).ravel() +rng = np.random.RandomState(0) +X_train = np.sort(5 * rng.rand(40, 1), axis=0) +X_test = np.linspace(0, 5, 500)[:, np.newaxis] +y = np.sin(X_train).ravel() # Add noise to targets y[::5] += 1 * (0.5 - np.random.rand(8)) @@ -32,15 +33,17 @@ # %% # Fit regression model # -------------------- +# Here we train a model and visualize how `uniform` and `distance` +# weights in prediction effect predicted values. n_neighbors = 5 for i, weights in enumerate(["uniform", "distance"]): knn = neighbors.KNeighborsRegressor(n_neighbors, weights=weights) - y_ = knn.fit(X, y).predict(T) + y_ = knn.fit(X_train, y).predict(X_test) plt.subplot(2, 1, i + 1) - plt.scatter(X, y, color="darkorange", label="data") - plt.plot(T, y_, color="navy", label="prediction") + plt.scatter(X_train, y, color="darkorange", label="data") + plt.plot(X_test, y_, color="navy", label="prediction") plt.axis("tight") plt.legend() plt.title("KNeighborsRegressor (k = %i, weights = '%s')" % (n_neighbors, weights)) diff --git a/sklearn/neighbors/_regression.py b/sklearn/neighbors/_regression.py index 2ef95247e83d1..98a5734c5140b 100644 --- a/sklearn/neighbors/_regression.py +++ b/sklearn/neighbors/_regression.py @@ -43,6 +43,10 @@ class KNeighborsRegressor(KNeighborsMixin, RegressorMixin, NeighborsBase): Uniform weights are used by default. + See the following example for a demonstration of the impact of + different weighting schemes on predictions: + :ref:`sphx_glr_auto_examples_neighbors_plot_regression.py`. + algorithm : {'auto', 'ball_tree', 'kd_tree', 'brute'}, default='auto' Algorithm used to compute the nearest neighbors: