def train_rls(): X_train, Y_train, X_test, Y_test = load_wine() #Map labels from set {1,2,3} to one-vs-all encoding Y_train = to_one_vs_all(Y_train) Y_test = to_one_vs_all(Y_test) regparams = [2.**i for i in range(-15, 16)] learner = LeaveOneOutRLS(X_train, Y_train, regparams=regparams, measure=ova_accuracy) P_test = learner.predict(X_test) #ova_accuracy computes one-vs-all classification accuracy directly between transformed #class label matrix, and a matrix of predictions, where each column corresponds to a class print("test set accuracy %f" %ova_accuracy(Y_test, P_test))
def train_rls(): X_train, Y_train, X_test, Y_test = load_wine() #Map labels from set {1,2,3} to one-vs-all encoding Y_train = to_one_vs_all(Y_train, False) Y_test = to_one_vs_all(Y_test, False) regparams = [2.**i for i in range(-15, 16)] learner = LeaveOneOutRLS(X_train, Y_train, regparams=regparams, measure=ova_accuracy) P_test = learner.predict(X_test) #ova_accuracy computes one-vs-all classification accuracy directly between transformed #class label matrix, and a matrix of predictions, where each column corresponds to a class print("test set accuracy %f" %ova_accuracy(Y_test, P_test))
def test(self): Y = np.random.randint(0, 5, 100) P = np.random.random((100, 5)) Y_ova = multiclass.to_one_vs_all(Y) perf1 = ova_accuracy(Y_ova, P) P = np.argmax(P, axis=1) perf2 = np.mean(Y == P) self.assertAlmostEqual(perf1, perf2)
def predict(self, X, y=None): ypred = self.learner.predict(X) if y is not None: if np.count_nonzero(y) >= len(y): zerolabels = False else: zerolabels = True y = to_one_vs_all(y, zerolabels) return ypred, self.measure(y, ypred) return ypred
def fit(self, X_src, y_src, X_tgt_known, y_tgt_known, X_tgt_unknown, y_tgt_unknown, verbose=False): # Map labels from set {1,2,3} to one-vs-all encoding if np.count_nonzero(y_src) >= len(y_src): zerolabels = False else: zerolabels = True y_src = to_one_vs_all(y_src, zerolabels) regparams = [2.**i for i in range(-15, 16)] if len(np.unique(y_src)) > 2: self.measure = ova_accuracy else: self.measure = accuracy self.learner = LeaveOneOutRLS(X_src, y_src, regparams=regparams, measure=self.measure) p_tgt = self.learner.predict(X_tgt_known) # ova_accuracy computes one-vs-all classification accuracy directly between transformed # class label matrix, and a matrix of predictions, where each column corresponds to a class self.learner = RLS(X_src, y_src) best_regparam = None best_accuracy = 0. # exponential grid of possible regparam values log_regparams = range(-15, 16) for log_regparam in log_regparams: regparam = 2.**log_regparam # RLS is re-trained with the new regparam, this # is very fast due to computational short-cut self.learner.solve(regparam) # Leave-one-out cross-validation predictions, this is fast due to # computational short-cut P_loo = self.learner.leave_one_out() acc = self.measure(y_src, P_loo) if verbose == True: print("LooRLS regparam 2**%d, loo-accuracy %f" % (log_regparam, acc)) if acc > best_accuracy: best_accuracy = acc best_regparam = regparam self.learner.solve(best_regparam) if verbose == True: print("LooRLS best regparam %f with loo-accuracy %f" % (best_regparam, best_accuracy))
def load_newsgroups(): T = np.loadtxt("train.data") # map indices from 1...n to 0...n-1 rows = T[:, 0] - 1 cols = T[:, 1] - 1 vals = T[:, 2] X_train = sp.coo_matrix((vals, (rows, cols))) X_train = X_train.tocsc() T = np.loadtxt("test.data") # map indices from 1...n to 0...n-1 rows = T[:, 0] - 1 cols = T[:, 1] - 1 vals = T[:, 2] X_test = sp.coo_matrix((vals, (rows, cols))) X_test = X_test.tocsc() # X_test has additional features not present in X_train X_test = X_test[:, : X_train.shape[1]] Y_train = np.loadtxt("train.label", dtype=int) Y_train = multiclass.to_one_vs_all(Y_train, False) Y_test = np.loadtxt("test.label", dtype=int) Y_test = multiclass.to_one_vs_all(Y_test, False) return X_train, Y_train, X_test, Y_test
def load_newsgroups(): T = np.loadtxt("train.data") #map indices from 1...n to 0...n-1 rows = T[:,0] -1 cols = T[:,1] -1 vals = T[:,2] X_train = sp.coo_matrix((vals, (rows, cols))) X_train = X_train.tocsc() T = np.loadtxt("test.data") #map indices from 1...n to 0...n-1 rows = T[:,0] -1 cols = T[:,1] -1 vals = T[:,2] X_test = sp.coo_matrix((vals, (rows, cols))) X_test = X_test.tocsc() #X_test has additional features not present in X_train X_test = X_test[:,:X_train.shape[1]] Y_train = np.loadtxt("train.label") Y_train = multiclass.to_one_vs_all(Y_train) Y_test = np.loadtxt("test.label") Y_test = multiclass.to_one_vs_all(Y_test) return X_train, Y_train, X_test, Y_test
import numpy as np from rlscore.utilities import multiclass from rlscore.measure import ova_accuracy Y = [0,0,1,1,2,2] Y_ova = multiclass.to_one_vs_all(Y) P_ova = [[1, 0, 0], [1.2,0.5, 0], [0, 1, -1], [1, 1.2, 0.5], [0.2, -1, -1], [0.3, -1, -2]] acc = ova_accuracy(Y_ova, P_ova) print("ova-mapped Y") print(Y_ova) print("P, class prediction is chosen with argmax") print(P_ova) print("Accuracy computed with one-vs-all mapped labels and predictions: %f" %acc) print("original Y") print(Y) print("P mapped to class predictions") P = multiclass.from_one_vs_all(P_ova) print(P) acc = np.mean(Y==P) print("Accuracy is the same:%f " %acc)
import numpy as np from rlscore.utilities import multiclass from rlscore.measure import ova_accuracy Y = [0, 0, 1, 1, 2, 2] Y_ova = multiclass.to_one_vs_all(Y) P_ova = [[1, 0, 0], [1.2, 0.5, 0], [0, 1, -1], [1, 1.2, 0.5], [0.2, -1, -1], [0.3, -1, -2]] acc = ova_accuracy(Y_ova, P_ova) print("ova-mapped Y") print(Y_ova) print("P, class prediction is chosen with argmax") print(P_ova) print("Accuracy computed with one-vs-all mapped labels and predictions: %f" % acc) print("original Y") print(Y) print("P mapped to class predictions") P = multiclass.from_one_vs_all(P_ova) print(P) acc = np.mean(Y == P) print("Accuracy is the same:%f " % acc)