2010-11-23 02:40:48 +08:00
|
|
|
"""
|
|
|
|
|
===============================
|
|
|
|
|
Ordinary Least Squares with SGD
|
|
|
|
|
===============================
|
|
|
|
|
|
|
|
|
|
Simple Ordinary Least Squares example with stochastic
|
|
|
|
|
gradient descent, we draw the linear least
|
|
|
|
|
squares solution for a random set of points in the plane.
|
|
|
|
|
"""
|
|
|
|
|
print __doc__
|
|
|
|
|
|
|
|
|
|
import pylab as pl
|
|
|
|
|
|
2011-09-02 17:00:02 +08:00
|
|
|
from sklearn.linear_model import SGDRegressor
|
2012-03-19 03:53:00 +08:00
|
|
|
from sklearn.datasets.samples_generator import make_regression
|
2010-11-23 02:40:48 +08:00
|
|
|
|
|
|
|
|
# this is our test set, it's just a straight line with some
|
|
|
|
|
# gaussian noise
|
2012-12-25 20:16:05 +08:00
|
|
|
X, Y = make_regression(n_samples=100, n_features=1, n_informative=1,
|
|
|
|
|
random_state=0, noise=35)
|
2010-11-23 02:40:48 +08:00
|
|
|
|
|
|
|
|
# run the classifier
|
2010-11-30 19:14:30 +08:00
|
|
|
clf = SGDRegressor(alpha=0.1, n_iter=20)
|
2010-11-23 02:40:48 +08:00
|
|
|
clf.fit(X, Y)
|
|
|
|
|
|
|
|
|
|
# and plot the result
|
|
|
|
|
pl.scatter(X, Y, color='black')
|
|
|
|
|
pl.plot(X, clf.predict(X), color='blue', linewidth=3)
|
|
|
|
|
pl.show()
|