Sophie

Sophie

distrib > Mandriva > 2010.2 > i586 > media > contrib-backports > by-pkgid > e578866d55cd81fdb23827cdf3cec911 > files > 463

python-scikits-learn-0.6-1mdv2010.2.i586.rpm

"""
==============================
k-Nearest Neighbors regression
==============================

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

"""
print __doc__

###############################################################################
# Generate sample data
import numpy as np

np.random.seed(0)
X = np.sort(5*np.random.rand(40, 1), axis=0)
T = np.linspace(0, 5, 500)
y = np.sin(X).ravel()

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

###############################################################################
# Fit regression model

from scikits.learn import neighbors

knn_barycenter = neighbors.NeighborsBarycenter(n_neighbors=5)
y_ = knn_barycenter.fit(X, y).predict(T)

###############################################################################
# look at the results
import pylab as pl
pl.scatter(X, y, c='k', label='data')
pl.hold('on')
pl.plot(T, y_, c='g', label='k-NN prediction')
pl.xlabel('data')
pl.ylabel('target')
pl.legend()
pl.title('k-NN Regression')
pl.show()