Skip to content

Instantly share code, notes, and snippets.

@nkt1546789
Last active October 11, 2015 21:45
Show Gist options
  • Save nkt1546789/33c742db508d36309a05 to your computer and use it in GitHub Desktop.
Save nkt1546789/33c742db508d36309a05 to your computer and use it in GitHub Desktop.
Kernel Ridge Classifier on Python. You can use your own kernel.
from sklearn.base import BaseEstimatior
class KernelRidgeClassifier(BaseEstimator):
def __init__(self,alpha=0.1,**kwds):
self.alpha=alpha
self.kwds=kwds
def fit(self,X,y):
n=len(y)
K=array([[self.kwds["kernel"](xi,xj) for xj in X] for xi in X])
self.theta=pinv(K+self.alpha*identity(n)).dot(c_[y])
self.X=X
return self
def predict_proba(self,X):
K=array([[self.kwds["kernel"](xi,xj) for xj in self.X] for xi in X])
return K.dot(self.theta).ravel()
def predict(self,X):
proba=self.predict_proba(X)
return 2*(proba>=0)-1
def score(self,X,y):
return sum(self.predict(X)==y)/float(len(y))
@RichieMoreno
Copy link

how can i put a kernel in this code?
a tried to use it, but i have no idea how to use the code
and some of the functions give me an error
I'll be thankful if you can help me
(sorry for my english)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment