Created
July 23, 2015 08:45
-
-
Save nkt1546789/add3f33ceee65294d0a8 to your computer and use it in GitHub Desktop.
Laplacian regularized Ridge Classifier (LRRidge) on Python. This is for semi-supervised classification tasks. In demo, we compared LRRidge with ordinal Ridge using gaussian kernel model.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
from numpy import linalg,random | |
from sklearn.base import BaseEstimator | |
from sklearn import datasets,metrics | |
class LRRidge(BaseEstimator): | |
def __init__(self,alpha=1.,beta=1.,gamma=10.,k=10): | |
self.alpha=alpha | |
self.beta=beta | |
self.gamma=gamma | |
self.k=k | |
def fit(self,Xl,yl,Xu): | |
self.X=np.r_[Xl,Xu] | |
#self.X=self.X[random.permutation(len(self.X))] | |
self.X2=np.c_[np.sum(self.X**2,1)] | |
Xl2=np.c_[np.sum(Xl**2,1)] | |
Xu2=np.c_[np.sum(Xu**2,1)] | |
Phil=np.exp(-self.gamma*(Xl2+self.X2.T-2*Xl.dot(self.X.T))) | |
Phiu=np.exp(-self.gamma*(Xu2+self.X2.T-2*Xu.dot(self.X.T))) | |
Phiu2=np.c_[np.sum(Phiu**2,1)] | |
d=Phiu2+Phiu2.T-2*Phiu.dot(Phiu.T) | |
p=np.c_[np.sort(d,axis=1)[:,self.k+1]] | |
W=d<=p | |
W=(W+W.T)!=0 | |
D=np.diag(np.sum(W,axis=1)) | |
L=D-W | |
n_features=Phil.shape[1] | |
self.theta=linalg.pinv(Phil.T.dot(Phil)+self.alpha*np.identity(n_features)+self.beta*Phiu.T.dot(L).dot(Phiu)).dot(Phil.T).dot(yl) | |
return self | |
def predict(self,X): | |
X2=np.c_[np.sum(X**2,1)] | |
Phi=np.exp(-self.gamma*(X2+self.X2.T-2*X.dot(self.X.T))) | |
return (Phi.dot(self.theta)>=0)*2-1 | |
# demo | |
random.seed(1) | |
n=500 | |
X,y=datasets.make_circles(n_samples=n,factor=.5,noise=.05) | |
#X,y=datasets.make_moons(n_samples=n,noise=.05) | |
y=y*2-1 | |
nl=2 # number of labeled samples | |
idx=random.permutation(len(X)) | |
il=idx[:nl]; iu=idx[nl:]; | |
Xl=X[il]; Xu=X[iu]; | |
yl=y[il]; yu=y[iu]; | |
clf=LRRidge().fit(Xl,yl,Xu) | |
ypred=clf.predict(X) | |
yupred=clf.predict(Xu) | |
print "Accuracy (LRRidge):",metrics.accuracy_score(yu,yupred) | |
from sklearn.linear_model import RidgeClassifierCV | |
gamma=10. | |
X2=np.c_[np.sum(X**2,1)] | |
Phi=np.exp(-gamma*(X2+X2.T-2*X.dot(X.T))) | |
clf=RidgeClassifierCV().fit(Phi[il],yl) | |
yupred2=clf.predict(Phi[iu]) | |
print "Accuracy (Ridge):",metrics.accuracy_score(yu,yupred2) | |
colors=np.array(["r","b"]) | |
plt.figure(figsize=(12,6)) | |
plt.subplot(121) | |
plt.scatter(Xu[:,0],Xu[:,1],c="w",s=20) | |
plt.scatter(Xl[:,0],Xl[:,1],color=colors[(1+yl)/2],s=100) | |
plt.subplot(122) | |
plt.scatter(X[:,0],X[:,1],color=colors[(1+ypred)/2]) | |
plt.tight_layout() | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Result for 2 moons: