-
Notifications
You must be signed in to change notification settings - Fork 47
/
plot_regressors.py
92 lines (72 loc) · 2.67 KB
/
plot_regressors.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""
An example script plotting some regressors using prediction_plotter.
"""
import numpy as np
import matplotlib.pylab as pl
pl.ion()
from sklearn.linear_model import LinearRegression
from sklearn.neighbors import KNeighborsRegressor
from sklearn import svm
from sklearn.gaussian_process import GaussianProcess
from rosetta.modeling import prediction_plotter
###############################################################################
# Make training data
###############################################################################
# X data
N = 10 # Number of data points
x1min, x1max, x2min, x2max = 0., 200., 1., 20.
x1 = np.random.randint(x1min, x1max+1, size=N)
x2 = np.random.randint(x2min, x2max+1, size=N)
X = np.c_[x1, x2]
# y data
# y is bigger near the center of the support of X
# noise is added to y
x1mid = (x1max - x1min) / 2.
x2mid = (x2max - x2min) / 2.
center = np.array([x1mid, x2mid])
width1, width2 = x1mid/2, x2mid/2
noise_level = 0.2
product = ((X[:, 0] - center[0]) / width1)**4 + ((X[:, 1] - center[1]) / width2)**4
y = np.exp(- product / 2. ) + noise_level * np.random.randn(N)
###############################################################################
# Initialize the plotter
###############################################################################
plotter = prediction_plotter.RegressorPlotter2D(
x_names=['age', 'height'], y_name='measured-data')
###############################################################################
# Linear Regression
###############################################################################
pl.figure(1)
pl.clf()
clf = LinearRegression().fit(X, y)
plotter.plot(clf, X, y)
pl.title("LinearRegression")
pl.colorbar()
###############################################################################
# K Nearest Neighbors
###############################################################################
pl.figure(2)
pl.clf()
n_neighbors = max(2, N/10)
clf = KNeighborsRegressor(n_neighbors=n_neighbors).fit(X, y)
plotter.plot(clf, X, y)
pl.title("KNeighborsRegressor")
pl.colorbar()
###############################################################################
# Support Vector Regressor (SVR, a.k.a. RVM)
###############################################################################
pl.figure(4)
pl.clf()
clf = svm.SVR(gamma=0.01).fit(X, y)
plotter.plot(clf, X, y)
pl.title("svm.SVR")
pl.colorbar()
###############################################################################
# Gaussian Process Models
###############################################################################
pl.figure(5)
pl.clf()
clf = GaussianProcess(regr='linear', theta0=2).fit(X, y)
plotter.plot(clf, X, y)
pl.title("GaussianProcess")
pl.colorbar()