Show Sidebar Hide Sidebar

Nearest Neighbors Regression in Scikit-learn

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 [1]:
import sklearn
sklearn.__version__
Out[1]:
'0.18.1'

Imports

In [2]:
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 [3]:
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 [4]:
def data_to_plotly(x):
    k = []
    
    for i in range(0, len(x)):
        k.append(x[i][0])
        
    return k

Plot Results

In [5]:
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 [6]:
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[6]:
Still need help?
Contact Us

For guaranteed 24 hour response turnarounds, upgrade to a Developer Support Plan.