Skip to content

Instantly share code, notes, and snippets.

@yamaguchiyuto
Created December 5, 2014 04:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yamaguchiyuto/9efd331e1f201177dddb to your computer and use it in GitHub Desktop.
Save yamaguchiyuto/9efd331e1f201177dddb to your computer and use it in GitHub Desktop.
Grid search for oreore regression
import sys
import numpy as np
import matplotlib.pyplot as plt
from sklearn import grid_search
from oreore_ridge import RidgeRegression
def psi(xlist,M):
""" make a design matrix """
ret = []
for x in xlist:
ret.append([x**i for i in range(0,M+1)])
return np.array(ret)
np.random.seed(0)
""" Data for grid search """
N = 10
M = 15
xlist = np.linspace(0, 1, N)
ylist = np.sin(2 * np.pi * xlist) + np.random.normal(0, 0.2, xlist.size)
X = psi(xlist,M)
y = ylist
""" Grid search """
parameters = {'lamb':np.exp([i for i in range(-30,1)])}
reg = grid_search.GridSearchCV(RidgeRegression(),parameters,cv=5)
reg.fit(X,y)
best = reg.best_estimator_
""" Plot """
xs = np.linspace(0, 1, 500)
ideal = np.sin(2*np.pi*xs)
plt.plot(xlist,ylist,'bo')
plt.plot(xs,ideal)
plt.plot(xs,np.dot(psi(xs,M),best.coef_))
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment