from __future__ import division

import time

import numpy as np
from sklearn.datasets import load_digits
from matplotlib import pyplot

from scw import SCW1, SCW2


def generate_dataset():
    digits = load_digits(2)

    classes = np.unique(digits.target)
    y = np.array(digits.target, copy=True)
    y[y==classes[0]] = -1
    y[y==classes[1]] = 1
    return digits.data, y


def calc_accuracy(results, answers):
    n_correct_answers = 0
    for result, answer in zip(results, answers):
        if(result == answer):
            n_correct_answers += 1
    accuracy = n_correct_answers/len(results)
    return accuracy


def test_scw(scw, training, test):
    t1 = time.time()
    # online fitting
    for x, y in zip(*training):
        scw.fit([x], [y])
    t2 = time.time()

    samples, labels = test

    t3 = time.time()
    results = scw.predict(samples)
    t4 = time.time()

    accuracy = calc_accuracy(results, labels)

    assert(accuracy == 1.0)

    print("fitting time    : {:3.6f}\n"
          "predicting time : {:3.6f}\n"
          "accuracy        : {:1.3f}\n".format(t2-t1, t4-t3, accuracy))



X, y = generate_dataset()
N = int(len(X)*0.8)

training = (X[:N], y[:N])
test = (X[N:], y[N:])

print("SCW1")
test_scw(SCW1(C=1.0, ETA=1.0), training, test)

print("")
print("SCW2")
test_scw(SCW2(C=1.0, ETA=1.0), training, test)

from __future__ import division

import time
import unittest

import numpy as np
from sklearn.datasets import load_digits
from sklearn.metrics import accuracy_score
from sklearn.cross_validation import cross_val_score
from matplotlib import pyplot

import utils
from scw import SCW1, SCW2


def generate_dataset():
    digits = load_digits(2)
    y = np.array(digits.target, copy=True)
    y = utils.overwrite_labels(y)
    return digits.data, y


def cross_validation(scw, training, test):
    # force online fitting
    for x, y in zip(*training):
        scw.fit([x], [y])

    X, y_true = test
    y_pred = scw.predict(X)
    return accuracy_score(y_true, y_pred)


class TestAccuracy(unittest.TestCase):
    def test_accuracy(self):
        X, y = generate_dataset()  #linear separable
        N = int(len(X)*0.8)

        training = X[:N], y[:N]
        test = X[N:], y[N:]

        accuracy = cross_validation(SCW1(C=1.0, ETA=1.0), training, test)
        self.assertEqual(accuracy, 1.0)
        accuracy = cross_validation(SCW2(C=1.0, ETA=1.0), training, test)
        self.assertEqual(accuracy, 1.0)


class TestDataFormat(unittest.TestCase):
    def setUp(self):
        self.scw = SCW1()

    def test_data_shape(self):
        X = np.arange(144).reshape(4, 4, 9)  #3 dim array
        y = np.random.randint(0, 1, 144)
        self.assertRaises(ValueError, self.scw.fit, X, y)

    def test_data_label(self):
        X = np.arange(24).reshape(6, 4)
        y = [1, 1, 0, 0, 1, 0]
        self.assertRaises(ValueError, self.scw.fit, X, y)


unittest.main()

Add exception tests