def with_replacement(X, Y, T): x = CuckooVector({}) m = len(Y) for t in range(T): i = np.random.randint(m) y = Y[i] x.reset(X[i]) yield (x, y)
def svm_primal_sgd(X, Y, lam, T): w = CuckooVector({}) x = CuckooVector({}) m = len(Y) for t in range(T): step = 1 / (lam * (t+1)) i = np.random.randint(m) y = Y[i] x.reset(X[i]) w.scale(1 - step * lam) if y * w.dot(x) < 1: w.add_scale(x, step * y) return w
def weightedError(w, Xt, Yt, thresh = 0): xv = CuckooVector({}) return (1 + np.mean([(xv.reset(x) == None and np.sign(xv.dot(w) - thresh)*y) for (x,y) in zip(Xt,Yt)]))/2