def test_reformat(self): negatives=[[3,1],[-4,-5],[10,-1]] positives=[[3,6],[-1,0],[2,3]] (X,y)=MySVM.reformat(positives,negatives) self.assertEqual(y,[1,1,1,0,0,0]) self.assertEqual(X,[[3,6],[-1,0],[2,3],[3,1],[-4,-5],[10,-1]])
def test_reformat(self): negatives = [[3, 1], [-4, -5], [10, -1]] positives = [[3, 6], [-1, 0], [2, 3]] (X, y) = MySVM.reformat(positives, negatives) self.assertEqual(y, [1, 1, 1, 0, 0, 0]) self.assertEqual(X, [[3, 6], [-1, 0], [2, 3], [3, 1], [-4, -5], [10, -1]])
def test_easy(self): #y>x negatives=[[3,1],[-4,-5],[10,-1]] positives=[[3,6],[-1,0],[2,3]] testpairs=[[3,1],[3,6],[3,2],[3,4],[3,2.2],[3,3.5]] testanswers=[0,1,0,1,0,1] results = MySVM.train_test(positives,negatives,testpairs) for i in range(len(testpairs)): print "Test "+str(i)+" : "+str(testanswers[i])+" : "+str(results[i]) self.assertEqual(results[i],testanswers[i])
def main(): #default, two class, 100 samples, 20 features #X, y = datasets.make_classification(n_samples=510) X, y = datasets.load_svmlight_file("data") #X = np.array([[1,2], [2,2], [3,2], [4,2], [5,2], [6,2],\ # [1,0], [2,0], [3,0], [4,0], [5,0], [6,0]]) #y = np.array([1,1,1,0,1,1,0,0,0,1,0,0]) #processing_y(y) X = X.toarray() #X_train = X[:100] #y_train = y[:100] #X_test = X[100:] #y_test = y[100:] #X_train = X #y_train = y #X_test = np.array([[7,2],[8,-2],[9,5],[10,0]]) #y_test = np.array([1,-1,1,-1]) X_train = X[:500] y_train = y[:500] X_test = X[500:510] y_test = y[500:510] clf = MySVM(kernel=my_kernel_2, c=1) clf.fit(X_train, y_train) print "total iterate times: %d" % clf.rounds print "here are multipliers:" print clf._multipilers #print "the w is ", clf.get_w() #print "the b is ", clf._b acount = 0.0 #print clf.y #for i in range(4): for i in range(10): yp = clf.predict(X_test[i]) print yp, y_test[i] if yp == y_test[i]: acount += 1 print "Accuracy: ", acount/10 print "The alphas is :", len(clf.support_mislabeled) print clf.support_mislabeled
def test_easy(self): #y>x negatives = [[3, 1], [-4, -5], [10, -1]] positives = [[3, 6], [-1, 0], [2, 3]] testpairs = [[3, 1], [3, 6], [3, 2], [3, 4], [3, 2.2], [3, 3.5]] testanswers = [0, 1, 0, 1, 0, 1] results = MySVM.train_test(positives, negatives, testpairs) for i in range(len(testpairs)): print "Test " + str(i) + " : " + str(testanswers[i]) + " : " + str( results[i]) self.assertEqual(results[i], testanswers[i])