{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Nearest Neighbors regression\n\nDemonstrate the resolution of a regression problem\nusing a k-Nearest Neighbor and the interpolation of the\ntarget using both barycenter and constant weights.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Authors: The scikit-learn developers\n# SPDX-License-Identifier: BSD-3-Clause" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 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" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "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))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Fit regression model\nHere we train a model and visualize how `uniform` and `distance`\nweights in prediction effect predicted values.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "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()" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.21" } }, "nbformat": 4, "nbformat_minor": 0 }