from unittest import TestCase

from ppp_nlp_ml_standalone import Linearclassifier, utils, config
import numpy


class LinearClassifierTest(TestCase):

    def testUtils(self):
        self.assertEquals(utils.sigmoid(0), 0.5)
        self.assertEquals(utils.softmax([1,0,0]).shape[0], 3)

    def testLinearClassifier(self):

        x = numpy.array([[1,1,1,0,0,0],
                         [1,0,1,0,0,0],
                         [1,1,1,0,0,0],
                         [0,0,1,1,1,0],
                         [0,0,1,1,0,0],
                         [0,0,1,1,1,0]])
        y = numpy.array([[1, 0],
                        [1, 0],
                        [1, 0],
                        [0, 1],
                        [0, 1],
                        [0, 1]])

        classifier = Linearclassifier.LogisticRegression(x, y, 6, 2)

        for i in range(1, 100):
            classifier.train(lr=0.01)

        value = numpy.array([[1, 1, 0, 0, 0, 0],
                             [0, 0, 0, 1, 1, 0],
                             [0, 0, 1, 1, 1, 1]])


        predicted_values = classifier.predict(value)
        sum_prob = numpy.sum(predicted_values, axis=1)

        self.assertTrue(numpy.linalg.norm(sum_prob-numpy.array([1, 1, 1])) < 0.01)

        self.assertTrue(numpy.array_equal(numpy.argmax(predicted_values, axis=1), numpy.array([0, 1, 1])))

        self.assertTrue(classifier.negative_log_likelihood() < 5)

    def testLearnModel(self):
        trainModel = Linearclassifier.TrainLinearClassifier(config.get_data('questions.train.txt'),
                                                            config.get_data('answers.train.txt'),
                                                            debug=False)

        trainModel.train(n_epochs=5500, learning_rate=0.001, l2_reg=0.001)
        ratio_train = trainModel.train_evaluation()
        ratio_test = trainModel.test_evaluation(config.get_data('questions.test.txt'),
                                   config.get_data('answers.test.txt'))
        trainModel.save_model()

        self.assertTrue(ratio_train > 0.7)
        self.assertTrue(ratio_test > 0.5)
@@ -185,33 +185,32 @@
 self.assertEqual
-s
 (utils.sigmoid(0
@@ -241,17 +241,16 @@
 ertEqual
-s
 (utils.s
@@ -1167,28 +1167,28 @@
  self.assert
-True
+Less
 (numpy.linal
@@ -1226,18 +1226,17 @@
  1, 1%5D))
- %3C
+,
  0.01)%0A%0A
@@ -1362,20 +1362,20 @@
 f.assert
-True
+Less
 (classif
@@ -1407,10 +1407,8 @@
 od()
- %3C
  5)%0A
@@ -2040,36 +2040,39 @@
      self.assert
-True
+Greater
 (ratio_train %3E 0
@@ -2067,18 +2067,17 @@
 io_train
- %3E
+,
  0.7)%0A  
@@ -2093,20 +2093,23 @@
 f.assert
-True
+Greater
 (ratio_t
@@ -2115,11 +2115,11 @@
 test
- %3E
+,
  0.5)
+%0A

Use better unittest assertion methods