Esempio n. 1
0
  def test1(self):
    t1 = self.tree
    bv = ExplicitBitVect(5)
    
    ex = ['nm',bv]
    self.assertFalse(t1.ClassifyExample(ex))
    bv.SetBit(1)
    self.assertTrue(t1.ClassifyExample(ex))
    
    bv.SetBit(0)
    self.assertTrue(t1.ClassifyExample(ex))

    bv.SetBit(2)
    self.assertFalse(t1.ClassifyExample(ex))
Esempio n. 2
0
    def test1(self):
        t1 = self.tree
        bv = ExplicitBitVect(5)

        ex = ['nm', bv]
        self.failIf(t1.ClassifyExample(ex))
        bv.SetBit(1)
        self.failUnless(t1.ClassifyExample(ex))

        bv.SetBit(0)
        self.failUnless(t1.ClassifyExample(ex))

        bv.SetBit(2)
        self.failIf(t1.ClassifyExample(ex))
Esempio n. 3
0
    def test4(self):
        examples = [
            ['a', 1, 0, 1, 0, 1],
            ['b', 1, 0, 0, 0, 1],
            ['c', 1, 0, 1, 0, 0],
            ['d', 0, 1, 1, 0, 0],
            ['e', 0, 1, 1, 1, 0],
        ]

        nvars = len(examples[0]) - 2
        attrs = range(1, nvars + 1)
        origNVars = nvars
        nvars = 10
        npvals = [0] + [2] * nvars + [2]
        qBounds = [0] + [0] * nvars + [0]

        bitEx = []
        for eg in examples:
            newEg = [eg[0], None, eg[-1]]
            bv = ExplicitBitVect(nvars)
            for i in range(origNVars):
                if eg[i + 1]:
                    bv.SetBit(i)

            # this bit will yield perfect accuracy if
            #  the attrs argument isn't being used properly:
            if eg[-1]:
                bv.SetBit(origNVars)
            newEg[1] = bv
            bitEx.append(newEg)

        attrs = range(origNVars)
        mdl2 = CrossValidate.makeNBClassificationModel(bitEx,
                                                       attrs,
                                                       npvals,
                                                       qBounds,
                                                       useSigs=True)
        nWrong = 0
        for eg in bitEx:
            p = mdl2.ClassifyExample(eg)
            if p != eg[-1]:
                nWrong += 1
        self.assertEqual(nWrong, 1)
Esempio n. 4
0
    def test3(self):
        examples = [
            ['a', 1, 0, 1, 0, 1],
            ['b', 1, 0, 0, 0, 1],
            ['c', 1, 0, 1, 0, 0],
            ['d', 0, 1, 1, 0, 0],
            ['e', 0, 1, 1, 1, 0],
        ]

        nvars = len(examples[0]) - 2
        attrs = list(range(1, nvars + 1))
        npvals = [0] + [2] * nvars + [2]
        qBounds = [0] + [0] * nvars + [0]
        mdl = CrossValidate.makeNBClassificationModel(examples, attrs, npvals,
                                                      qBounds)
        nWrong = 0
        for eg in examples:
            p = mdl.ClassifyExample(eg)
            if p != eg[-1]:
                nWrong += 1
        self.assertEqual(nWrong, 1)

        bitEx = []
        for eg in examples:
            newEg = [eg[0], None, eg[-1]]
            bv = ExplicitBitVect(nvars)
            for i in range(nvars):
                if eg[i + 1]:
                    bv.SetBit(i)
            newEg[1] = bv
            bitEx.append(newEg)

        attrs = list(range(nvars))
        mdl2 = CrossValidate.makeNBClassificationModel(bitEx,
                                                       attrs,
                                                       npvals,
                                                       qBounds,
                                                       useSigs=True)
        nWrong = 0
        for eg in bitEx:
            p = mdl2.ClassifyExample(eg)
            if p != eg[-1]:
                nWrong += 1
        self.assertEqual(nWrong, 1)

        # now compare:
        for i in range(len(bitEx)):
            eg = examples[i]
            p1 = mdl.ClassifyExample(eg)
            bitEg = bitEx[i]
            p2 = mdl2.ClassifyExample(bitEg)
            self.assertEqual(p1, p2)
            v1 = mdl.GetClassificationDetails()
            v2 = mdl.GetClassificationDetails()
            self.assertAlmostEqual(v1, v2, 4)
Esempio n. 5
0
    def _test5(self):  # disabled because CMIM was removed # pragma: nocover
        examples = [
            ['a', 1, 0, 1, 0, 1, 1, 0, 1],
            ['b', 1, 0, 0, 0, 1, 0, 0, 1],
            ['c', 1, 0, 1, 0, 1, 1, 0, 0],
            ['d', 0, 1, 1, 0, 1, 0, 0, 0],
            ['e', 0, 1, 1, 1, 0, 1, 0, 0],
        ]

        nvars = len(examples[0]) - 2
        npvals = [0] + [2] * nvars + [2]
        qBounds = [0] + [0] * nvars + [0]

        bitEx = []
        for eg in examples:
            newEg = [eg[0], None, eg[-1]]
            bv = ExplicitBitVect(nvars)
            for i in range(nvars):
                if eg[i + 1]:
                    bv.SetBit(i)

            # this bit will yield perfect accuracy if
            #  the attrs argument isn't being used properly:
            newEg[1] = bv
            bitEx.append(newEg)

        attrs = list(range(nvars))
        mdl2 = CrossValidate.makeNBClassificationModel(bitEx,
                                                       attrs,
                                                       npvals,
                                                       qBounds,
                                                       useSigs=True,
                                                       useCMIM=2)
        nWrong = 0
        for eg in bitEx:
            p = mdl2.ClassifyExample(eg)
            if p != eg[-1]:
                nWrong += 1
        self.assertEqual(nWrong, 1)
Esempio n. 6
0
  def test3(self):
    from BuildSigTree import BuildSigTree
    examples = []

    bv = ExplicitBitVect(2)
    vc = VectCollection()
    vc.AddVect(1,bv)
    examples.append(['a',vc,1])

    bv = ExplicitBitVect(2)
    bv.SetBit(1)
    vc = VectCollection()
    vc.AddVect(1,bv)
    examples.append(['c',vc,0])

    bv = ExplicitBitVect(2)
    bv.SetBit(1)
    vc = VectCollection()
    vc.AddVect(1,bv)
    examples.append(['c2',vc,0])

    bv = ExplicitBitVect(2)
    bv.SetBit(0)
    vc = VectCollection()
    vc.AddVect(1,bv)
    examples.append(['d',vc,0])

    bv = ExplicitBitVect(2)
    bv.SetBit(0)
    vc = VectCollection()
    vc.AddVect(1,bv)
    bv = ExplicitBitVect(2)
    bv.SetBit(1)
    vc.AddVect(2,bv)
    examples.append(['d2',vc,0])

    bv = ExplicitBitVect(2)
    bv.SetBit(0)
    bv.SetBit(1)
    vc = VectCollection()
    vc.AddVect(1,bv)
    examples.append(['d',vc,1])

    bv = ExplicitBitVect(2)
    bv.SetBit(0)
    bv.SetBit(1)
    vc = VectCollection()
    vc.AddVect(1,bv)
    examples.append(['e',vc,1])

    t = BuildSigTree(examples,2,metric=InfoTheory.InfoType.ENTROPY,
                     maxDepth=2,verbose=0)

    self.assertEqual(t.GetName(),'Bit-0')
    self.assertEqual(t.GetLabel(),0)
    c0 = t.GetChildren()[0]
    self.assertEqual(c0.GetName(),'Bit-1')
    self.assertEqual(c0.GetLabel(),1)
    c1 = t.GetChildren()[1]
    self.assertEqual(c1.GetName(),'Bit-1')
    self.assertEqual(c1.GetLabel(),1)

    bv = ExplicitBitVect(2)
    bv.SetBit(0)
    vc = VectCollection()
    vc.AddVect(1,bv)
    bv = ExplicitBitVect(2)
    bv.SetBit(1)
    vc.AddVect(2,bv)
    r = t.ClassifyExample(['t',vc,0])
    self.assertEqual(r,0)
Esempio n. 7
0
    def test3(self):
        examples = []

        bv = ExplicitBitVect(2)
        vc = VectCollection()
        vc.AddVect(1, bv)
        examples.append(['a', vc, 1])

        bv = ExplicitBitVect(2)
        bv.SetBit(1)
        vc = VectCollection()
        vc.AddVect(1, bv)
        examples.append(['c', vc, 0])

        bv = ExplicitBitVect(2)
        bv.SetBit(1)
        vc = VectCollection()
        vc.AddVect(1, bv)
        examples.append(['c2', vc, 0])

        bv = ExplicitBitVect(2)
        bv.SetBit(0)
        vc = VectCollection()
        vc.AddVect(1, bv)
        examples.append(['d', vc, 0])

        bv = ExplicitBitVect(2)
        bv.SetBit(0)
        vc = VectCollection()
        vc.AddVect(1, bv)
        bv = ExplicitBitVect(2)
        bv.SetBit(1)
        vc.AddVect(2, bv)
        examples.append(['d2', vc, 0])

        bv = ExplicitBitVect(2)
        bv.SetBit(0)
        bv.SetBit(1)
        vc = VectCollection()
        vc.AddVect(1, bv)
        examples.append(['d', vc, 1])

        bv = ExplicitBitVect(2)
        bv.SetBit(0)
        bv.SetBit(1)
        vc = VectCollection()
        vc.AddVect(1, bv)
        examples.append(['e', vc, 1])

        f = StringIO()
        with redirect_stdout(f):
            t = BuildSigTree(examples,
                             2,
                             metric=InfoTheory.InfoType.ENTROPY,
                             maxDepth=2,
                             verbose=True)
        self.assertIn('Build', f.getvalue())

        self.assertEqual(t.GetName(), 'Bit-0')
        self.assertEqual(t.GetLabel(), 0)
        c0 = t.GetChildren()[0]
        self.assertEqual(c0.GetName(), 'Bit-1')
        self.assertEqual(c0.GetLabel(), 1)
        c1 = t.GetChildren()[1]
        self.assertEqual(c1.GetName(), 'Bit-1')
        self.assertEqual(c1.GetLabel(), 1)

        bv = ExplicitBitVect(2)
        bv.SetBit(0)
        vc = VectCollection()
        vc.AddVect(1, bv)
        bv = ExplicitBitVect(2)
        bv.SetBit(1)
        vc.AddVect(2, bv)
        r = t.ClassifyExample(['t', vc, 0])
        self.assertEqual(r, 0)