class TestKnn(unittest.TestCase):
    def setUp(self):
        self.logreg_unreg = LogReg(5, 0.0, lambda x: 1.0)
        self.logreg_reg = LogReg(5, 0.25, lambda x: 1.0)

    def test_reg(self):
        print(self.logreg_reg.beta)
        print(kPOS.x)
        beta = self.logreg_reg.sg_update(kPOS, 1)
        self.assertAlmostEqual(beta[0], .5)
        self.assertAlmostEqual(beta[1], 2.0)
        self.assertAlmostEqual(beta[2], 1.5)
        self.assertAlmostEqual(beta[3], 0.5)
        self.assertAlmostEqual(beta[4], 0.0)

        print(self.logreg_reg.beta)
        print(kNEG.x)
        beta = self.logreg_reg.sg_update(kNEG, 2)
        self.assertAlmostEqual(beta[0], -0.72068776924864364)
        self.assertAlmostEqual(beta[1], 1.0)
        self.assertAlmostEqual(beta[2], -0.22068776924864364)
        self.assertAlmostEqual(beta[3], -2.6620633077459308)
        self.assertAlmostEqual(beta[4], -3.8827510769945746)

        print("Finalize")
        print(self.logreg_reg.last_update)
        beta = self.logreg_reg.finalize_lazy(2)
        self.assertAlmostEqual(beta[0], -0.72068776924864364)
        self.assertAlmostEqual(beta[1], 1.0)
        self.assertAlmostEqual(beta[2], -0.22068776924864364)
        self.assertAlmostEqual(beta[3], -2.6620633077459308)
        self.assertAlmostEqual(beta[4], -3.8827510769945746)

    def test_unreg(self):
        print(self.logreg_unreg.beta)
        print(kPOS.x)
        beta = self.logreg_unreg.sg_update(kPOS, 1)
        self.assertAlmostEqual(beta[0], .5)
        self.assertAlmostEqual(beta[1], 2.0)
        self.assertAlmostEqual(beta[2], 1.5)
        self.assertAlmostEqual(beta[3], 0.5)
        self.assertAlmostEqual(beta[4], 0.0)

        print(self.logreg_unreg.beta)
        print(kPOS.x)
        beta = self.logreg_unreg.sg_update(kNEG, 2)
        self.assertAlmostEqual(beta[0], -0.47068776924864364)
        self.assertAlmostEqual(beta[1], 2.0)
        self.assertAlmostEqual(beta[2], 0.5293122307513564)
        self.assertAlmostEqual(beta[3], -2.4120633077459308)
        self.assertAlmostEqual(beta[4], -3.8827510769945746)
Exemplo n.º 2
0
class TestKnn(unittest.TestCase):
    def setUp(self):
        self.logreg_unreg = LogReg(5, 0.0, lambda x: 1.0)
        self.logreg_reg = LogReg(5, 0.25, lambda x: 1.0)

    def test_reg(self):
    	#print("\nREGULARIZED:\n")
        print(self.logreg_reg.beta)
        print(kPOS.x)
        beta = self.logreg_reg.sg_update(kPOS, 0)
        self.assertAlmostEqual(beta[0], .25)
        self.assertAlmostEqual(beta[1], 1.0)
        self.assertAlmostEqual(beta[2], 0.75)
        self.assertAlmostEqual(beta[3], 0.25)
        self.assertAlmostEqual(beta[4], 0.0)

        #print(self.logreg_reg.beta)
        #print(kPOS.x)
        beta = self.logreg_reg.sg_update(kNEG, 1)
        self.assertAlmostEqual(beta[0], -0.30097640098415529)
        self.assertAlmostEqual(beta[1], 1.0)
        self.assertAlmostEqual(beta[2], -0.05097640098415529)
        self.assertAlmostEqual(beta[3], -1.1529292029524658)
        self.assertAlmostEqual(beta[4], -0.85195280196831058)

    def test_unreg(self):
    	#print("\nUNREGULARIZED:\n\n")
        print(self.logreg_unreg.beta)
        print(kPOS.x)
        beta = self.logreg_unreg.sg_update(kPOS, 0)
        self.assertAlmostEqual(beta[0], .5)
        self.assertAlmostEqual(beta[1], 2.0)
        self.assertAlmostEqual(beta[2], 1.5)
        self.assertAlmostEqual(beta[3], 0.5)
        self.assertAlmostEqual(beta[4], 0.0)

        print(self.logreg_unreg.beta)
        print(kPOS.x)
        beta = self.logreg_unreg.sg_update(kNEG, 1)
        self.assertAlmostEqual(beta[0], -0.47068776924864364)
        self.assertAlmostEqual(beta[1], 2.0)
        self.assertAlmostEqual(beta[2], 0.5293122307513564)
        self.assertAlmostEqual(beta[3], -2.4120633077459308)
        self.assertAlmostEqual(beta[4], -3.8827510769945746)
Exemplo n.º 3
0
class TestLogReg(unittest.TestCase):
    def setUp(self):
        self.logreg_unreg = LogReg(5, 0.0, lambda x: 1.0)
        self.logreg_reg = LogReg(5, 0.25, lambda x: 1.0)

    def test_unreg(self):
        print(self.logreg_unreg.w)
        print(kPOS.x)
        w = self.logreg_unreg.sg_update(kPOS, 0)
        self.assertAlmostEqual(w[0], .5)
        self.assertAlmostEqual(w[1], 2.0)
        self.assertAlmostEqual(w[2], 1.5)
        self.assertAlmostEqual(w[3], 0.5)
        self.assertAlmostEqual(w[4], 0.0)

        print(self.logreg_unreg.w)
        print(kNEG.x)
        w = self.logreg_unreg.sg_update(kNEG, 1)
        self.assertAlmostEqual(w[0], -0.49330714907571527)
        self.assertAlmostEqual(w[1], 2.0)
        self.assertAlmostEqual(w[2], -0.48661429815143054)
        self.assertAlmostEqual(w[3], -2.479921447227146)
        self.assertAlmostEqual(w[4], -0.99330714907571527)

    def test_reg(self):
        print(self.logreg_reg.w)
        print(kPOS.x)
        w = self.logreg_reg.sg_update(kPOS, 0)
        self.assertAlmostEqual(w[0], .5)
        self.assertAlmostEqual(w[1], 1.0)
        self.assertAlmostEqual(w[2], 0.75)
        self.assertAlmostEqual(w[3], 0.25)
        self.assertAlmostEqual(w[4], 0.0)

        print(self.logreg_reg.w)
        print(kNEG.x)
        w = self.logreg_reg.sg_update(kNEG, 1)
        print w
        self.assertAlmostEqual(w[0], -0.43991334982599239)
        self.assertAlmostEqual(w[1], 1.0)
        self.assertAlmostEqual(w[2], -0.56491334982599239)
        self.assertAlmostEqual(w[3], -1.2848700247389886)
        self.assertAlmostEqual(w[4], -0.2349783374564981)
Exemplo n.º 4
0
class TestKnn(unittest.TestCase):
    def setUp(self):
        self.logreg_unreg = LogReg(5, 0.0, lambda x: 1.0)
        self.logreg_reg = LogReg(5, 0.25, lambda x: 1.0)

    def test_reg(self):
        print(self.logreg_reg.beta)
        print(kPOS.x)
        beta = self.logreg_reg.sg_update(kPOS, 0)
        self.assertAlmostEqual(beta[0], .25)
        self.assertAlmostEqual(beta[1], 1.0)
        self.assertAlmostEqual(beta[2], 0.75)
        self.assertAlmostEqual(beta[3], 0.25)
        self.assertAlmostEqual(beta[4], 0.0)

        print(self.logreg_reg.beta)
        print(kPOS.x)
        beta = self.logreg_reg.sg_update(kNEG, 1)
        self.assertAlmostEqual(beta[0], -0.30097640098415529)
        self.assertAlmostEqual(beta[1], 1.0)
        self.assertAlmostEqual(beta[2], -0.05097640098415529)
        self.assertAlmostEqual(beta[3], -1.1529292029524658)
        self.assertAlmostEqual(beta[4], -0.85195280196831058)

    def test_unreg(self):
        print(self.logreg_unreg.beta)
        print(kPOS.x)
        beta = self.logreg_unreg.sg_update(kPOS, 0)
        self.assertAlmostEqual(beta[0], .5)
        self.assertAlmostEqual(beta[1], 2.0)
        self.assertAlmostEqual(beta[2], 1.5)
        self.assertAlmostEqual(beta[3], 0.5)
        self.assertAlmostEqual(beta[4], 0.0)

        print(self.logreg_unreg.beta)
        print(kPOS.x)
        beta = self.logreg_unreg.sg_update(kNEG, 1)
        self.assertAlmostEqual(beta[0], -0.47068776924864364)
        self.assertAlmostEqual(beta[1], 2.0)
        self.assertAlmostEqual(beta[2], 0.5293122307513564)
        self.assertAlmostEqual(beta[3], -2.4120633077459308)
        self.assertAlmostEqual(beta[4], -3.8827510769945746)
Exemplo n.º 5
0
class TestKnn(unittest.TestCase):
    def setUp(self):
        self.logreg_unreg = LogReg(5, 1.0)

    def test_unreg(self):
        print(self.logreg_unreg.beta)
        print(kPOS.x)
        beta = self.logreg_unreg.sg_update(kPOS)
        self.assertAlmostEqual(beta[0], .5)
        self.assertAlmostEqual(beta[1], 2.0)
        self.assertAlmostEqual(beta[2], 1.5)
        self.assertAlmostEqual(beta[3], 0.5)
        self.assertAlmostEqual(beta[4], 0.0)

        print(self.logreg_unreg.beta)
        print(kPOS.x)
        beta = self.logreg_unreg.sg_update(kNEG)
        self.assertAlmostEqual(beta[0], -0.47068776924864364)
        self.assertAlmostEqual(beta[1], 2.0)
        self.assertAlmostEqual(beta[2], 0.5293122307513564)
        self.assertAlmostEqual(beta[3], -2.4120633077459308)
        self.assertAlmostEqual(beta[4], -3.8827510769945746)
Exemplo n.º 6
0
class TestKnn(unittest.TestCase):
    def setUp(self):
        self.logreg_unreg = LogReg(5, 1.0)

    def test_unreg(self):
        print(self.logreg_unreg.beta)
        print(kPOS.x)
        beta = self.logreg_unreg.sg_update(kPOS)
        self.assertAlmostEqual(beta[0], .5)
        self.assertAlmostEqual(beta[1], 2.0)
        self.assertAlmostEqual(beta[2], 1.5)
        self.assertAlmostEqual(beta[3], 0.5)
        self.assertAlmostEqual(beta[4], 0.0)

        print(self.logreg_unreg.beta)
        print(kPOS.x)
        beta = self.logreg_unreg.sg_update(kNEG)
        self.assertAlmostEqual(beta[0], -0.47068776924864364)
        self.assertAlmostEqual(beta[1], 2.0)
        self.assertAlmostEqual(beta[2], 0.5293122307513564)
        self.assertAlmostEqual(beta[3], -2.4120633077459308)
        self.assertAlmostEqual(beta[4], -3.8827510769945746)
Exemplo n.º 7
0
class TestLogReg(unittest.TestCase):
    def setUp(self):
        self.logreg_learnrate = LogReg(5, 0.0, lambda x: 0.5)
        self.logreg_unreg = LogReg(5, 0.0, lambda x: 1.0)
        self.logreg_reg = LogReg(5, 0.25, lambda x: 1.0)

    def test_learnrate(self):
        print("\nTesting: Learning Rate Usage")
        print(self.logreg_learnrate.w)
        print(kPOS.x)
        w = self.logreg_learnrate.sg_update(kPOS, 0)
        self.assertAlmostEqual(w[0], 0.25)
        self.assertAlmostEqual(w[1], 1.00)
        self.assertAlmostEqual(w[2], 0.75)
        self.assertAlmostEqual(w[3], 0.25)
        self.assertAlmostEqual(w[4], 0.0)

        print(self.logreg_learnrate.w)
        print(kNEG.x)
        w = self.logreg_learnrate.sg_update(kNEG, 1)
        self.assertAlmostEqual(w[0], -0.21207090998937828)
        self.assertAlmostEqual(w[1], 1.0)
        self.assertAlmostEqual(w[2], -0.17414181997875655)
        self.assertAlmostEqual(w[3], -1.1362127299681348)
        self.assertAlmostEqual(w[4], -0.46207090998937828)

    def test_unreg(self):
        print("\nTesting: Unregularized Update")
        print(self.logreg_unreg.w)
        print(kPOS.x)
        w = self.logreg_unreg.sg_update(kPOS, 0)
        self.assertAlmostEqual(w[0], .5)
        self.assertAlmostEqual(w[1], 2.0)
        self.assertAlmostEqual(w[2], 1.5)
        self.assertAlmostEqual(w[3], 0.5)
        self.assertAlmostEqual(w[4], 0.0)

        print(self.logreg_unreg.w)
        print(kNEG.x)
        w = self.logreg_unreg.sg_update(kNEG, 1)
        self.assertAlmostEqual(w[0], -0.49330714907571527)
        self.assertAlmostEqual(w[1], 2.0)
        self.assertAlmostEqual(w[2], -0.48661429815143054)
        self.assertAlmostEqual(w[3], -2.479921447227146)
        self.assertAlmostEqual(w[4], -0.99330714907571527)

    def test_reg(self):
        print("\nTesting: Regularized Update")
        print(self.logreg_reg.w)
        print(kPOS.x)
        w = self.logreg_reg.sg_update(kPOS, 0)
        self.assertAlmostEqual(w[0], .5)
        self.assertAlmostEqual(w[1], 1.0)
        self.assertAlmostEqual(w[2], 0.75)
        self.assertAlmostEqual(w[3], 0.25)
        self.assertAlmostEqual(w[4], 0.0)

        print(self.logreg_reg.w)
        print(kNEG.x)
        w = self.logreg_reg.sg_update(kNEG, 1)
        self.assertAlmostEqual(w[0], -0.43991334982599239)
        self.assertAlmostEqual(w[1], 1.0)
        self.assertAlmostEqual(w[2], -0.56491334982599239)
        self.assertAlmostEqual(w[3], -1.2848700247389886)
        self.assertAlmostEqual(w[4], -0.2349783374564981)