Skip to content

Instantly share code, notes, and snippets.

@nkt1546789
Created January 18, 2016 19:54
Show Gist options
  • Save nkt1546789/026faaa210639f31ad9d to your computer and use it in GitHub Desktop.
Save nkt1546789/026faaa210639f31ad9d to your computer and use it in GitHub Desktop.
A demo code for ranking SVM.
"""
A demo code for ranking SVM
The data used in this code comes from http://download.joachims.org/svm_light/examples/example3.tar.gz
"""
import numpy as np
import itertools
from sklearn.linear_model import SGDClassifier
from sklearn.grid_search import GridSearchCV
np.random.seed(0)
def transform(X,y):
order=np.argsort(y)
Xordered=X[order]
yordered=y[order]
Xnew=[]
ynew=[]
for i,j in itertools.combinations(range(X.shape[0]),2):
Xnew.append(Xordered[i]-Xordered[j])
ynew.append(1)
return Xnew,ynew
def transform_randomly(X,y):
idx=np.random.permutation(len(y))
X=X[idx]
y=y[idx]
Xnew=[]
ynew=[]
for i,j in itertools.combinations(range(X.shape[0]),2):
Xnew.append(X[i]-X[j])
if y[i]>=y[j]:
ynew.append(1)
else:
ynew.append(-1)
return Xnew,ynew
Xtr=np.array([[1, 1, 0, 0.2, 0],
[0, 0, 1, 0.1, 1],
[0, 1, 0, 0.4, 0],
[0, 0, 1, 0.3, 0],
[0, 0, 1, 0.2, 0],
[1, 0, 1, 0.4, 0],
[0, 0, 1, 0.1, 0],
[0, 0, 1, 0.2, 0],
[0, 0, 1, 0.1, 1],
[1, 1, 0, 0.3, 0],
[1, 0, 0, 0.4, 1],
[0, 1, 1, 0.5, 0]])
ytr=np.array([3,2,1,1,1,2,1,1,2,3,4,1])
qids=np.array([1,1,1,1,2,2,2,2,3,3,3,3])
Xte=np.array([[1, 0, 0, 0.2, 1],
[1, 1, 0, 0.3, 0],
[0, 0, 0, 0.2, 1],
[0, 0, 1, 0.2, 0]])
yte=np.array([4,3,2,1])
Xtr_transformed=[]
ytr_transformed=[]
for qid in np.unique(qids):
#Xp,yp=transform(Xtr[qids==qid],ytr[qids==qid])
Xp,yp=transform_randomly(Xtr[qids==qid],ytr[qids==qid])
Xtr_transformed+=Xp
ytr_transformed+=yp
Xtr_transformed=np.array(Xtr_transformed)
ytr_transformed=np.array(ytr_transformed)
clf=SGDClassifier(loss="hinge",penalty="l2")
clf=GridSearchCV(clf,param_grid={"alpha":np.logspace(-1,7,10)}).fit(Xtr_transformed,ytr_transformed).best_estimator_
scores=clf.decision_function(Xte).ravel()
print "true ranking:",np.argsort(yte)[::-1]
print "predicted ranking:",np.argsort(scores)[::-1]
print "scores:",scores
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment