def train(self, X, Y): # Get relevant data idx = Y==self.i jdx = Y==self.j ijdx = np.logical_or(idx, jdx) x = X[ijdx] y = Y[ijdx] # Project y onto {-1, +1} y[y==self.i] = -1.0 y[y==self.j] = 1.0 # Train SMO self.w, self.b = train(x, y, C=1e-5, tol=1e-2, eps=1e-3)
def proc((c,code)): print c,code coded_labels = train_labels.copy() # Create the code-mapped label vector for i, label in enumerate(train_labels): coded_labels[i] = code[int(label)] # SVM for this code w, b = train(train_data, coded_labels, C=0.00001, tol=0.00001, eps=1e-2) # Hypothesis h_train = hypothesis(train_data, w, b) h_test = hypothesis(test_data, w, b) return c, h_train, h_test
data_file = "../data/spambase/spambase.data" dmat = [] f = open(data_file, "r") for line in f: x = line.split(',') x = [float(e) for e in x] dmat.append(x) data = np.array(dmat) # k-folds xvalidation k = 10 kfolder = KFolder(data, k, standard=True, shuffle=False) for i in range(1): print "Fold:", i+1 # Get data and labels at fold k X,Y = kfolder.training(i) # Get the testing data Xi,Yi = kfolder.testing(i) Yi[Yi==0] = -1.0 # Train Y[Y==0] = -1.0 w, b = train(X, Y.ravel()) # Test print "Training accuracy:", test(X, Y, w, b) print "Testing accuracy:", test(Xi, Yi, w, b)