{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Nearest Neighbors regression\n\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\nHubness reduction of this low-dimensional dataset\nshows only small effects.\n\nAdapted from `<https://scikit-learn.org/stable/auto_examples/neighbors/plot_regression.html>`_\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(__doc__)\n\n# Author: Alexandre Gramfort <alexandre.gramfort@inria.fr>\n#         Fabian Pedregosa <fabian.pedregosa@inria.fr>\n#\n# License: BSD 3 clause (C) INRIA\n\n\n# #############################################################################\n# Generate sample data\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom skhubness.neighbors import KNeighborsRegressor\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))\n\n# #############################################################################\n# Fit regression model\nn_neighbors = 5\n\nf = plt.figure()\nfor i, weights in enumerate(['uniform', 'distance']):\n    for j, hubness in enumerate([None, 'local_scaling']):\n        knn = KNeighborsRegressor(n_neighbors,\n                                  algorithm_params={'n_candidates': 39},\n                                  weights=weights,\n                                  hubness=hubness)\n        y_ = knn.fit(X, y).predict(T)\n\n        plt.subplot(2, 2, i * 2 + j + 1)\n        f.set_figheight(15)\n        f.set_figwidth(15)\n        plt.scatter(X, y, c='k', label='data')\n        plt.plot(T, y_, c='g', label='prediction')\n        plt.axis('tight')\n        plt.legend()\n        plt.title(f\"KNeighborsRegressor (k = {n_neighbors}, weights = '{weights}', hubness = '{hubness}')\")\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.7.4"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}