Esempio n. 1
0
 def test_grad(self):
     my_cs = MulticlassSVM('crammer-singer')
     norm_diff = np.linalg.norm(
         my_cs.grad_student(self.W, self.X, self.y) -
         np.array([[-2.30447458, -0.94740057], [2.62578591, 1.11242235],
                   [0.16124486, 1.71850243]]))
     self.assertTrue(norm_diff < 5e-3)
Esempio n. 2
0
    def test_self_ovo_accuracy(self):
        self_ovr = MulticlassSVM('ovo')
        self_ovr.fit(self.X_train, self.y_train)

        train_acc = metrics.accuracy_score(self.y_train,
                                           self_ovr.predict(self.X_train))
        test_acc = metrics.accuracy_score(self.y_test,
                                          self_ovr.predict(self.X_test))

        self.assertTrue(abs(train_acc - 1.0) < 5e-3)
        self.assertTrue(abs(test_acc - 0.892625) < 5e-3)
Esempio n. 3
0
    def test_self_ovr_accuracy(self):
        self_ovr = MulticlassSVM('ovr')
        self_ovr.fit(self.X_train, self.y_train)

        train_acc = metrics.accuracy_score(self.y_train,
                                           self_ovr.predict(self.X_train))
        test_acc = metrics.accuracy_score(self.y_test,
                                          self_ovr.predict(self.X_test))

        self.assertTrue(train_acc >= 1 - 1e-6)
        # self.assertTrue(abs(train_acc - 1.0) < 1e-6)
        self.assertTrue(test_acc >= 0.814875 - 1e-6)
    def test_self_ovr_accuracy(self):
        self_ovr = MulticlassSVM('ovr')
        self_ovr.fit(self.X_train, self.y_train)

        train_acc = metrics.accuracy_score(self.y_train,
                                           self_ovr.predict(self.X_train))
        test_acc = metrics.accuracy_score(self.y_test,
                                          self_ovr.predict(self.X_test))
        print("my3 train_acc:", abs(train_acc - 1.0))
        print("my3 test_acc:", abs(test_acc - 0.814875))
        self.assertTrue(abs(train_acc - 1.0) < 5e-3)
        self.assertTrue(abs(test_acc - 0.814875) < 5e-3)
Esempio n. 5
0
 def test_loss(self):
     my_cs = MulticlassSVM('crammer-singer')
     loss_diff = abs(
         my_cs.loss_student(self.W, self.X, self.y) - 7.441854144192854)
     self.assertTrue(loss_diff < 5e-3)
Esempio n. 6
0
 def test_score_shape(self):
     msvm = MulticlassSVM('ovr')
     msvm.fit(self.X_train, self.y_train)
     scores = msvm.scores_ovr_student(self.X_test)
     print('socre shape', scores.shape)
     self.assertTrue(scores.shape[0] == 5000 and scores.shape[1] == 10)
def main():
    print('Loading data...')
    mnist = np.loadtxt('data/mnist_test.csv', delimiter=',')

    X_train = mnist[:len(mnist)//2, 1:]
    y_train = mnist[:len(mnist)//2, 0].astype(np.int)

    X_test = mnist[len(mnist)//2:, 1:]
    y_test = mnist[len(mnist)//2:, 0].astype(np.int)

    print('Training Sklearn OVR...')
    y_pred_train, y_pred_test = sklearn_multiclass_prediction(
        'ovr', X_train, y_train, X_test)
    print('Sklearn OVR Accuracy (train):',
          metrics.accuracy_score(y_train, y_pred_train))
    print('Sklearn OVR Accuracy (test) :',
          metrics.accuracy_score(y_test, y_pred_test))

    print('Training Sklearn OVO...')
    y_pred_train, y_pred_test = sklearn_multiclass_prediction(
        'ovo', X_train, y_train, X_test)
    print('Sklearn OVO Accuracy (train):',
          metrics.accuracy_score(y_train, y_pred_train))
    print('Sklearn OVO Accuracy (test) :',
          metrics.accuracy_score(y_test, y_pred_test))

    print('Training Sklearn Crammer-Singer...')
    y_pred_train, y_pred_test = sklearn_multiclass_prediction(
        'crammer', X_train, y_train, X_test)
    print('Sklearn Crammer-Singer Accuracy (train):',
          metrics.accuracy_score(y_train, y_pred_train))
    print('Sklearn Crammer-Singer Accuracy (test) :',
          metrics.accuracy_score(y_test, y_pred_test))

    print('Training self OVR...')
    self_ovr = MulticlassSVM('ovr')
    self_ovr.fit(X_train, y_train)
    print('Self OVR Accuracy (train):',
          metrics.accuracy_score(y_train, self_ovr.predict(X_train)))
    print('Self OVR Accuracy (test) :',
          metrics.accuracy_score(y_test, self_ovr.predict(X_test)))

    print('Training self OVO...')
    self_ovo = MulticlassSVM('ovo')
    self_ovo.fit(X_train, y_train)
    print('Self OVO Accuracy (train):',
          metrics.accuracy_score(y_train, self_ovo.predict(X_train)))
    print('Self OVO Accuracy (test) :',
          metrics.accuracy_score(y_test, self_ovo.predict(X_test)))

    print('Training self Crammer-Singer...')
    self_cs = MulticlassSVM('crammer-singer')
    self_cs.fit(X_train, y_train)
    print('Self Crammer-Singer Accuracy (train):',
          metrics.accuracy_score(y_train, self_cs.predict(X_train)))
    print('Self Crammer-Singer Accuracy (test) :',
          metrics.accuracy_score(y_test, self_cs.predict(X_test)))
Esempio n. 8
0
        'ovo', X_train, y_train, X_test)
    print('Sklearn OVO Accuracy (train):',
          metrics.accuracy_score(y_train, y_pred_train))
    print('Sklearn OVO Accuracy (test) :',
          metrics.accuracy_score(y_test, y_pred_test))

    print('Training Sklearn Crammer-Singer...')
    y_pred_train, y_pred_test = sklearn_multiclass_prediction(
        'crammer', X_train, y_train, X_test)
    print('Sklearn Crammer-Singer Accuracy (train):',
          metrics.accuracy_score(y_train, y_pred_train))
    print('Sklearn Crammer-Singer Accuracy (test) :',
          metrics.accuracy_score(y_test, y_pred_test))

    print('Training self OVR...')
    self_ovr = MulticlassSVM('ovr')
    self_ovr.fit(X_train, y_train)
    print('Self OVR Accuracy (train):',
          metrics.accuracy_score(y_train, self_ovr.predict(X_train)))
    print('Self OVR Accuracy (test) :',
          metrics.accuracy_score(y_test, self_ovr.predict(X_test)))

    print('Training self OVO...')
    self_ovo = MulticlassSVM('ovo')
    self_ovo.fit(X_train, y_train)
    print('Self OVO Accuracy (train):',
          metrics.accuracy_score(y_train, self_ovo.predict(X_train)))
    print('Self OVO Accuracy (test) :',
          metrics.accuracy_score(y_test, self_ovo.predict(X_test)))

    print('Training self Crammer-Singer...')