Show Sidebar Hide Sidebar

# Nearest Neighbors Regression in Scikit-learn

Note: this page is part of the documentation for version 3 of Plotly.py, which is not the most recent version.
See our Version 4 Migration Guide for information about how to upgrade.

Demonstrate the resolution of a regression problem using a k-Nearest Neighbor and the interpolation of the target using both barycenter and constant weights.

#### New to Plotly?¶

Plotly's Python library is free and open source! Get started by downloading the client and reading the primer.
You can set up Plotly to work in online or offline mode, or in jupyter notebooks.
We also have a quick-reference cheatsheet (new!) to help you get started!

### Version¶

In :
import sklearn
sklearn.__version__

Out:
'0.18.1'

### Imports¶

In :
import plotly.plotly as py
import plotly.graph_objs as go
from plotly import tools

import numpy as np
from sklearn import neighbors


### Calculations¶

In :
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()

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

In :
def data_to_plotly(x):
k = []

for i in range(0, len(x)):
k.append(x[i])

return k


### Plot Results¶

In :
data = [[], []]
titles = []
n_neighbors = 5

for i, weights in enumerate(['uniform', 'distance']):
knn = neighbors.KNeighborsRegressor(n_neighbors, weights=weights)
y_ = knn.fit(X, y).predict(T)

if(i==0):
leg=True
else:
leg=False

p1 = go.Scatter(x=data_to_plotly(X), y=y,
mode='markers', showlegend=leg,
marker=dict(color='black'),
name='data')

p2 = go.Scatter(x=data_to_plotly(T), y=y_,
mode='lines', showlegend=leg,
line=dict(color='green'),
name='prediction')
data[i].append(p1)
data[i].append(p2)
titles.append("KNeighborsRegressor (k = %i, weights = '%s')" % (n_neighbors,
weights))

In :
fig = tools.make_subplots(rows=2, cols=1,
subplot_titles=tuple(titles),
print_grid=False)

for i in range(0, len(data)):
for j in range(0, len(data[i])):
fig.append_trace(data[i][j], i+1, 1)

fig['layout'].update(height=700, hovermode='closest')

for i in map(str, range(1, 3)):
x = 'xaxis' + i
y = 'yaxis' + i
fig['layout'][x].update(showgrid=False, zeroline=False)
fig['layout'][y].update(showgrid=False, zeroline=False)

py.iplot(fig)

Out: 