Skip to content

DOC Add link to plot_regression.py #29232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions examples/neighbors/plot_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,44 @@
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))

# %%
# 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))
Expand Down
4 changes: 4 additions & 0 deletions sklearn/neighbors/_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down