def test_linear_svm(self):
        """
        Train linear SVM on dataset 0, which is linearly separable. Then try on dataset 3, which is not separable, but
        close to linearly separable.
        """
        i = 0
        params = {'kernel': 'linear', 'C': 1.0}

        lin_svm_model = kernel_svm_train(self.train_data[i],
                                         self.train_labels[i], params)
        predictions, _ = kernel_svm_predict(self.test_data[i], lin_svm_model)
        test_accuracy = np.mean(predictions == self.test_labels[i])

        print("Linear SVM had test accuracy %2.3f (should be around 0.98)" %
              test_accuracy)
        assert test_accuracy > 0.95, "Accuracy was below 0.95."

        i = 3
        params = {'kernel': 'linear', 'C': 1.0}

        lin_svm_model = kernel_svm_train(self.train_data[i],
                                         self.train_labels[i], params)
        predictions, _ = kernel_svm_predict(self.test_data[i], lin_svm_model)
        test_accuracy = np.mean(predictions == self.test_labels[i])

        print("Linear SVM had test accuracy %2.3f (should be around 0.93)" %
              test_accuracy)
        assert test_accuracy > 0.9, "Accuracy was below 0.9."
    def test_rbf_svm(self):
        """
        Train RBF SVM on dataset 1, which is not linearly separable.
        """
        i = 1
        params = {'kernel': 'rbf', 'C': 1.0, 'sigma': 1.0}

        rbf_svm_model = kernel_svm_train(self.train_data[i],
                                         self.train_labels[i], params)
        predictions, _ = kernel_svm_predict(self.test_data[i], rbf_svm_model)
        test_accuracy = np.mean(predictions == self.test_labels[i])

        print("RBF SVM had test accuracy %2.3f (should be around 0.92)" %
              test_accuracy)
        assert test_accuracy > 0.9, "Accuracy was below 0.9."
    def test_poly_svm(self):
        """
        Train quadratic polynomial SVM on dataset 1, which is not linearly separable.
        """
        i = 1
        params = {'kernel': 'polynomial', 'C': 1.0, 'order': 2}

        poly_svm_model = kernel_svm_train(self.train_data[i],
                                          self.train_labels[i], params)
        predictions, _ = kernel_svm_predict(self.test_data[i], poly_svm_model)
        test_accuracy = np.mean(predictions == self.test_labels[i])

        print(
            "Polynomial SVM had test accuracy %2.3f (should be around 0.94)" %
            test_accuracy)
        assert test_accuracy > 0.9, "Accuracy was below 0.9."
Example #4
0
    def test_rbf_svm(self):
        """
        Train RBF SVM on datasets 0 to 9.
        """

        threshold_accuracy = [0.95,0.9,0.875,0.95,0.9,0.9,0.95,0.9,0.875,0.875] # threshold accuracy values to beat for every dataset
        pass_vec = np.zeros(10, dtype =bool) # stores whether test passed on a dataset or not
        
        for i in range(10):

            params = {'kernel': 'rbf', 'C': 1.0, 'sigma': 0.5}
    
            svm_model = kernel_svm_train(self.train_data[i], self.train_labels[i], params)
            predictions, _ = kernel_svm_predict(self.test_data[i], svm_model)
            test_accuracy = np.mean(predictions == self.test_labels[i])

            print("On dataset %d, Kernel SVM had test accuracy %2.3f (should be greater than %2.3f)" %
                  (i, test_accuracy,threshold_accuracy[i]))
            pass_vec = test_accuracy - threshold_accuracy[i] < 0
            
        assert np.sum(pass_vec) == 0, "Kernel SVM accuracy was less than threshold for one of the datasets."