Skip to content

Commit d0f832c

Browse files
committed
Pushing the docs to dev/ for branch: main, commit 1e3c7be5ff7017550438036c42f0af502b9a2331
1 parent 594be91 commit d0f832c

File tree

1,265 files changed

+6165
-6158
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,265 files changed

+6165
-6158
lines changed
Binary file not shown.

dev/_downloads/437df39fcde24ead7b91917f2133a53c/plot_regression.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,44 @@
66
Demonstrate the resolution of a regression problem
77
using a k-Nearest Neighbor and the interpolation of the
88
target using both barycenter and constant weights.
9-
109
"""
1110

1211
# Authors: The scikit-learn developers
1312
# SPDX-License-Identifier: BSD-3-Clause
1413

15-
1614
# %%
1715
# Generate sample data
1816
# --------------------
17+
# Here we generate a few data points to use to train the model. We also generate
18+
# data in the whole range of the training data to visualize how the model would
19+
# react in that whole region.
1920
import matplotlib.pyplot as plt
2021
import numpy as np
2122

2223
from sklearn import neighbors
2324

24-
np.random.seed(0)
25-
X = np.sort(5 * np.random.rand(40, 1), axis=0)
26-
T = np.linspace(0, 5, 500)[:, np.newaxis]
27-
y = np.sin(X).ravel()
25+
rng = np.random.RandomState(0)
26+
X_train = np.sort(5 * rng.rand(40, 1), axis=0)
27+
X_test = np.linspace(0, 5, 500)[:, np.newaxis]
28+
y = np.sin(X_train).ravel()
2829

2930
# Add noise to targets
3031
y[::5] += 1 * (0.5 - np.random.rand(8))
3132

3233
# %%
3334
# Fit regression model
3435
# --------------------
36+
# Here we train a model and visualize how `uniform` and `distance`
37+
# weights in prediction effect predicted values.
3538
n_neighbors = 5
3639

3740
for i, weights in enumerate(["uniform", "distance"]):
3841
knn = neighbors.KNeighborsRegressor(n_neighbors, weights=weights)
39-
y_ = knn.fit(X, y).predict(T)
42+
y_ = knn.fit(X_train, y).predict(X_test)
4043

4144
plt.subplot(2, 1, i + 1)
42-
plt.scatter(X, y, color="darkorange", label="data")
43-
plt.plot(T, y_, color="navy", label="prediction")
45+
plt.scatter(X_train, y, color="darkorange", label="data")
46+
plt.plot(X_test, y_, color="navy", label="prediction")
4447
plt.axis("tight")
4548
plt.legend()
4649
plt.title("KNeighborsRegressor (k = %i, weights = '%s')" % (n_neighbors, weights))
Binary file not shown.

dev/_downloads/8daa075623a97eefa5be2c4a6eb55992/plot_regression.ipynb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"cell_type": "markdown",
2323
"metadata": {},
2424
"source": [
25-
"## Generate sample data\n\n"
25+
"## Generate sample data\nHere we generate a few data points to use to train the model. We also generate\ndata in the whole range of the training data to visualize how the model would\nreact in that whole region.\n\n"
2626
]
2727
},
2828
{
@@ -33,14 +33,14 @@
3333
},
3434
"outputs": [],
3535
"source": [
36-
"import matplotlib.pyplot as plt\nimport numpy as np\n\nfrom sklearn import neighbors\n\nnp.random.seed(0)\nX = np.sort(5 * np.random.rand(40, 1), axis=0)\nT = np.linspace(0, 5, 500)[:, np.newaxis]\ny = np.sin(X).ravel()\n\n# Add noise to targets\ny[::5] += 1 * (0.5 - np.random.rand(8))"
36+
"import matplotlib.pyplot as plt\nimport numpy as np\n\nfrom sklearn import neighbors\n\nrng = np.random.RandomState(0)\nX_train = np.sort(5 * rng.rand(40, 1), axis=0)\nX_test = np.linspace(0, 5, 500)[:, np.newaxis]\ny = np.sin(X_train).ravel()\n\n# Add noise to targets\ny[::5] += 1 * (0.5 - np.random.rand(8))"
3737
]
3838
},
3939
{
4040
"cell_type": "markdown",
4141
"metadata": {},
4242
"source": [
43-
"## Fit regression model\n\n"
43+
"## Fit regression model\nHere we train a model and visualize how `uniform` and `distance`\nweights in prediction effect predicted values.\n\n"
4444
]
4545
},
4646
{
@@ -51,7 +51,7 @@
5151
},
5252
"outputs": [],
5353
"source": [
54-
"n_neighbors = 5\n\nfor i, weights in enumerate([\"uniform\", \"distance\"]):\n knn = neighbors.KNeighborsRegressor(n_neighbors, weights=weights)\n y_ = knn.fit(X, y).predict(T)\n\n plt.subplot(2, 1, i + 1)\n plt.scatter(X, y, color=\"darkorange\", label=\"data\")\n plt.plot(T, y_, color=\"navy\", label=\"prediction\")\n plt.axis(\"tight\")\n plt.legend()\n plt.title(\"KNeighborsRegressor (k = %i, weights = '%s')\" % (n_neighbors, weights))\n\nplt.tight_layout()\nplt.show()"
54+
"n_neighbors = 5\n\nfor i, weights in enumerate([\"uniform\", \"distance\"]):\n knn = neighbors.KNeighborsRegressor(n_neighbors, weights=weights)\n y_ = knn.fit(X_train, y).predict(X_test)\n\n plt.subplot(2, 1, i + 1)\n plt.scatter(X_train, y, color=\"darkorange\", label=\"data\")\n plt.plot(X_test, y_, color=\"navy\", label=\"prediction\")\n plt.axis(\"tight\")\n plt.legend()\n plt.title(\"KNeighborsRegressor (k = %i, weights = '%s')\" % (n_neighbors, weights))\n\nplt.tight_layout()\nplt.show()"
5555
]
5656
}
5757
],

dev/_downloads/scikit-learn-docs.zip

4.32 KB
Binary file not shown.
-172 Bytes
-21 Bytes
218 Bytes
375 Bytes
-53 Bytes

0 commit comments

Comments
 (0)