Skip to content

Instantly share code, notes, and snippets.

@yamaguchiyuto
Created December 5, 2014 04:04
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/b5da661bacd99e3b9d1c to your computer and use it in GitHub Desktop.
Save yamaguchiyuto/b5da661bacd99e3b9d1c to your computer and use it in GitHub Desktop.
scikit-learn-compatible Ridge Regression
import numpy as np
from sklearn.base import BaseEstimator, RegressorMixin
class RidgeRegression(BaseEstimator, RegressorMixin):
def __init__(self,lamb=1.0):
self.lamb = lamb
def fit(self,X,y):
A = np.dot(X.T,X) + self.lamb * np.identity(X.shape[1])
b = np.dot(X.T,y)
self.coef_ = np.linalg.solve(A,b)
return self
def predict(self,X):
return np.dot(X,self.coef_)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment