Package ml :: Package var :: Module prank
[hide private]
[frames] | no frames]

Source Code for Module ml.var.prank

  1  ''' 
  2  Created on 13 Oct 2012 
  3   
  4  @author: Eleftherios Avramidis 
  5  ''' 
  6   
  7  from classifier import Classifier 
  8  import numpy as np 
  9  import logging 
 10   
11 -class PRank(Classifier):
12 ''' 13 Implements PRank algorithm 14 ''' 15 16
17 - def __init__(self, k):
18 ''' 19 Constructor 20 ''' 21 self.k = k
22
23 - def learn(self, X, Y, iterations):
24 k = self.k 25 #get the size of the featureset 26 n = np.size(X, 1); 27 #instantiate rule vector w with zeros 28 w = np.zeros(n) 29 #instantiate k thresholds b 30 B = np.zeros(k) 31 #last b should be infinite 32 B[k-1] = np.Infinity 33 34 #size of training set 35 T = np.size(X,0) 36 assert (T==np.size(Y,0)) 37 assert (np.size(Y,1)==1) 38 39 logging.debug("Initializing zero vectors\n w={}\tB={}".format(w, B)) 40 41 #iterate over all training instances 42 for i in xrange(iterations): 43 logging.info("\nIteration {} from {}".format(i+1, iterations)) 44 for t in xrange(T): 45 logging.info("\nRound {} from {}".format(t+1, T)) 46 #get a new rank (featurevector) 47 x = X[t] 48 #predict 49 wx = np.inner(w, x) 50 yp = self.predict_y(wx, B) 51 #get a new label 52 y = Y[t,0] 53 print "yp =",yp, ", y =",y 54 if y != yp: 55 w, B = self.update_w(k, y, B, wx, w, x) 56 print "after correction" 57 wx = np.inner(w, x) 58 yp = self.predict_y(wx, B) 59 print "yp =",yp, ", y =",y 60 self.w = w 61 self.B = B
62
63 - def update_w(self, k, y, B, wx, w, x):
64 Yr = np.empty(k-1) 65 for r in xrange(k-1): 66 if y <= r: 67 Yr[r] = -1 68 else: 69 Yr[r] = 1 70 print "Y =", Yr 71 72 tau = np.zeros(k-1) 73 for r in xrange(k-1): 74 if (wx - B[r]) * Yr[r] <= 0: 75 tau[r] = Yr[r] 76 else: 77 tau[r] = 0 78 print "tau =", tau 79 w = w + np.multiply(np.sum(tau), x) 80 for r in xrange(k-1): 81 B[r] = B[r] - tau[r] 82 print "w={} B={}".format(w, B) 83 return w, B
84 85 86
87 - def predict_y(self, wx, B):
88 89 for r, b in enumerate(B): 90 if (wx - b < 0): 91 return r
92 93
94 - def rank(self, x):
95 print "decoding" 96 wx = np.inner(self.w, x) 97 return self.predict_y(wx, self.B)
98 99 if __name__ == '__main__': 100 traindata = np.array([[1, 1, 1, 1, 1], 101 102 [0.5, 1, 2, 1, 3], 103 [1, 1, 1, 0, 0], 104 [1, 1, 0, 0, 0], 105 ]) 106 107 labels = np.array([[0], 108 [1], 109 [2], 110 [3], 111 112 113 ]) 114 k = len(set(labels[:,0])) 115 ranker = PRank(k) 116 ranker.learn(traindata, labels) 117 testdata = np.array([1.1, 1, 0, 0, 1]) 118 print ranker.rank(testdata) 119