コード例 #1
0
 def setUp(self):
     self.cpd = TabularCPD(
         'grade',
         3, [[0.1, 0.1, 0.1, 0.1, 0.1, 0.1], [0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
             [0.8, 0.8, 0.8, 0.8, 0.8, 0.8]],
         evidence=['intel', 'diff'],
         evidence_card=[3, 2])
コード例 #2
0
ファイル: test_Factor.py プロジェクト: cfm25/pgmpy
    def test_cpd_init(self):
        cpd = TabularCPD('grade', 3, [[0.1, 0.1, 0.1]])
        self.assertEqual(cpd.variable, 'grade')
        self.assertEqual(cpd.variable_card, 3)
        self.assertEqual(list(cpd.variables), ['grade'])
        np_test.assert_array_equal(cpd.cardinality, np.array([3]))
        np_test.assert_array_almost_equal(cpd.values, np.array([0.1, 0.1, 0.1]))

        cpd = TabularCPD('grade', 3, [[0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                                      [0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                                      [0.8, 0.8, 0.8, 0.8, 0.8, 0.8]],
                         evidence=['intel', 'diff'], evidence_card=[3, 2])
        self.assertEqual(cpd.variable, 'grade')
        self.assertEqual(cpd.variable_card, 3)
        np_test.assert_array_equal(cpd.cardinality, np.array([3, 2, 3]))
        self.assertListEqual(list(cpd.variables), ['grade', 'diff', 'intel'])
        np_test.assert_array_equal(cpd.values, np.array([0.1, 0.1, 0.1, 0.1, 0.1, 0.1,
                                                         0.1, 0.1, 0.1, 0.1, 0.1, 0.1,
                                                         0.8, 0.8, 0.8, 0.8, 0.8, 0.8]).reshape(3, 2, 3))

        cpd = TabularCPD('grade', 3, [[0.1, 0.1],
                                      [0.1, 0.1],
                                      [0.8, 0.8]],
                         evidence='evi1', evidence_card=2)
        self.assertEqual(cpd.variable, 'grade')
        self.assertEqual(cpd.variable_card, 3)
        np_test.assert_array_equal(cpd.cardinality, np.array([3, 2]))
        self.assertListEqual(list(cpd.variables), ['grade', 'evi1'])
        np_test.assert_array_equal(cpd.values, np.array([0.1, 0.1,
                                                         0.1, 0.1,
                                                         0.8, 0.8]).reshape(3, 2))
コード例 #3
0
 def test_normalize(self):
     cpd_un_normalized = TabularCPD(
         'grade', 2, [[0.7, 0.2, 0.6, 0.2], [0.4, 0.4, 0.4, 0.8]],
         ['intel', 'diff'], [2, 2])
     cpd_un_normalized.normalize()
     np_test.assert_array_almost_equal(
         cpd_un_normalized.values,
         np.array([
             0.63636364, 0.33333333, 0.6, 0.2, 0.36363636, 0.66666667, 0.4,
             0.8
         ]))
コード例 #4
0
ファイル: test_Factor.py プロジェクト: cfm25/pgmpy
 def test__repr__(self):
     grade_cpd = TabularCPD('grade', 3, [[0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                                         [0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                                         [0.8, 0.8, 0.8, 0.8, 0.8, 0.8]],
                            evidence=['intel', 'diff'], evidence_card=[3, 2])
     intel_cpd = TabularCPD('intel', 3, [[0.5], [0.3], [0.2]])
     diff_cpd = TabularCPD('grade', 3, [[0.1, 0.1], [0.1, 0.1],  [0.8, 0.8]], evidence=['diff'], evidence_card=[2])
     self.assertEqual(repr(grade_cpd), '<TabularCPD representing P(grade:3 | intel:3, diff:2) at {address}>'
                      .format(address=hex(id(grade_cpd))))
     self.assertEqual(repr(intel_cpd), '<TabularCPD representing P(intel:3) at {address}>'
                      .format(address=hex(id(intel_cpd))))
     self.assertEqual(repr(diff_cpd), '<TabularCPD representing P(grade:3 | diff:2) at {address}>'
                      .format(address=hex(id(diff_cpd))))
コード例 #5
0
ファイル: test_Factor.py プロジェクト: infoburp/pgmpy
 def test_normalize(self):
     cpd_un_normalized = TabularCPD('grade', 2, [[0.7, 0.2, 0.6, 0.2], [0.4, 0.4, 0.4, 0.8]],
                                    ['intel', 'diff'], [2, 2])
     cpd_un_normalized.normalize()
     np_test.assert_array_almost_equal(cpd_un_normalized.values, np.array([0.63636364, 0.33333333, 0.6, 0.2,
                                                                           0.36363636, 0.66666667, 0.4, 0.8]))
コード例 #6
0
ファイル: test_Factor.py プロジェクト: infoburp/pgmpy
 def setUp(self):
     self.cpd = TabularCPD('grade', 3, [[0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                                        [0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                                        [0.8, 0.8, 0.8, 0.8, 0.8, 0.8]],
                           evidence=['intel', 'diff'], evidence_card=[3, 2])
コード例 #7
0
ファイル: test_Factor.py プロジェクト: infoburp/pgmpy
class TestTabularCPDMethods(unittest.TestCase):
    def setUp(self):
        self.cpd = TabularCPD('grade', 3, [[0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                                           [0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                                           [0.8, 0.8, 0.8, 0.8, 0.8, 0.8]],
                              evidence=['intel', 'diff'], evidence_card=[3, 2])

    def test_marginalize_1(self):
        self.cpd.marginalize('diff')
        self.assertEqual(self.cpd.variable, 'grade')
        self.assertEqual(self.cpd.variable_card, 3)
        self.assertListEqual(list(self.cpd.variables), ['grade', 'intel'])
        np_test.assert_array_equal(self.cpd.cardinality, np.array([3, 3]))
        np_test.assert_array_equal(self.cpd.values, np.array([0.2, 0.2, 0.2,
                                                              0.2, 0.2, 0.2,
                                                              1.6, 1.6, 1.6]))

    def test_marginalize_2(self):
        self.cpd.marginalize('grade')
        self.assertEqual(self.cpd.variable, 'grade')
        self.assertListEqual(list(self.cpd.variables), ['intel', 'diff'])
        np_test.assert_array_equal(self.cpd.cardinality, np.array([3, 2]))
        np_test.assert_array_equal(self.cpd.values, np.array([1.0, 1.0, 1.0, 1.0, 1.0, 1.0]))

    def test_normalize(self):
        cpd_un_normalized = TabularCPD('grade', 2, [[0.7, 0.2, 0.6, 0.2], [0.4, 0.4, 0.4, 0.8]],
                                       ['intel', 'diff'], [2, 2])
        cpd_un_normalized.normalize()
        np_test.assert_array_almost_equal(cpd_un_normalized.values, np.array([0.63636364, 0.33333333, 0.6, 0.2,
                                                                              0.36363636, 0.66666667, 0.4, 0.8]))

    def test_reduce_1(self):
        self.cpd.reduce('diff_0')
        np_test.assert_array_equal(self.cpd.get_cpd(), np.array([[0.1, 0.1, 0.1],
                                                                 [0.1, 0.1, 0.1],
                                                                 [0.8, 0.8, 0.8]]))

    def test_reduce_2(self):
        self.cpd.reduce('intel_0')
        np_test.assert_array_equal(self.cpd.get_cpd(), np.array([[0.1, 0.1],
                                                                 [0.1, 0.1],
                                                                 [0.8, 0.8]]))

    def test_reduce_3(self):
        self.cpd.reduce(['intel_0', 'diff_0'])
        np_test.assert_array_equal(self.cpd.get_cpd(), np.array([[0.1],
                                                           [0.1],
                                                           [0.8]]))

    def test_reduce_4(self):
        self.cpd.reduce('grade_0')
        np_test.assert_array_equal(self.cpd.get_cpd(), np.array([[0.1, 0.1, 0.1, 0.1, 0.1, 0.1]]))

    def test_get_cpd(self):
        np_test.assert_array_equal(self.cpd.get_cpd(), np.array([[0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                                                                 [0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                                                                 [0.8, 0.8, 0.8, 0.8, 0.8, 0.8]]))

    def tearDown(self):
        del self.cpd
コード例 #8
0
class TestTabularCPDMethods(unittest.TestCase):
    def setUp(self):
        self.cpd = TabularCPD(
            'grade',
            3, [[0.1, 0.1, 0.1, 0.1, 0.1, 0.1], [0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                [0.8, 0.8, 0.8, 0.8, 0.8, 0.8]],
            evidence=['intel', 'diff'],
            evidence_card=[3, 2])

    def test_marginalize_1(self):
        self.cpd.marginalize('diff')
        self.assertEqual(self.cpd.variable, 'grade')
        self.assertEqual(self.cpd.variable_card, 3)
        self.assertListEqual(list(self.cpd.variables), ['grade', 'intel'])
        np_test.assert_array_equal(self.cpd.cardinality, np.array([3, 3]))
        np_test.assert_array_equal(
            self.cpd.values,
            np.array([0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.8, 0.8, 0.8]))

    def test_marginalize_2(self):
        self.cpd.marginalize('grade')
        self.assertEqual(self.cpd.variable, 'grade')
        self.assertListEqual(list(self.cpd.variables), ['diff', 'intel'])
        np_test.assert_array_equal(self.cpd.cardinality, np.array([2, 3]))
        np_test.assert_array_equal(self.cpd.values,
                                   np.array([1.0, 1.0, 1.0, 1.0, 1.0, 1.0]))

    def test_normalize(self):
        cpd_un_normalized = TabularCPD(
            'grade', 2, [[0.7, 0.2, 0.6, 0.2], [0.4, 0.4, 0.4, 0.8]],
            ['intel', 'diff'], [2, 2])
        cpd_un_normalized.normalize()
        np_test.assert_array_almost_equal(
            cpd_un_normalized.values,
            np.array([
                0.63636364, 0.33333333, 0.6, 0.2, 0.36363636, 0.66666667, 0.4,
                0.8
            ]))

    def test__repr__(self):
        grade_cpd = TabularCPD(
            'grade',
            3, [[0.1, 0.1, 0.1, 0.1, 0.1, 0.1], [0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                [0.8, 0.8, 0.8, 0.8, 0.8, 0.8]],
            evidence=['intel', 'diff'],
            evidence_card=[3, 2])
        intel_cpd = TabularCPD('intel', 3, [[0.5], [0.3], [0.2]])
        diff_cpd = TabularCPD('grade',
                              3, [[0.1, 0.1], [0.1, 0.1], [0.8, 0.8]],
                              evidence=['diff'],
                              evidence_card=[2])
        self.assertEqual(
            repr(grade_cpd),
            '<TabularCPD representing P(grade:3 | intel:3, diff:2) at {address}>'
            .format(address=hex(id(grade_cpd))))
        self.assertEqual(
            repr(intel_cpd),
            '<TabularCPD representing P(intel:3) at {address}>'.format(
                address=hex(id(intel_cpd))))
        self.assertEqual(
            repr(diff_cpd),
            '<TabularCPD representing P(grade:3 | diff:2) at {address}>'.
            format(address=hex(id(diff_cpd))))

    def test_reduce_1(self):
        self.cpd.reduce(('diff', 0))
        np_test.assert_array_equal(
            self.cpd.get_cpd(),
            np.array([[0.1, 0.1, 0.1], [0.1, 0.1, 0.1], [0.8, 0.8, 0.8]]))

    def test_reduce_2(self):
        self.cpd.reduce(('intel', 0))
        np_test.assert_array_equal(
            self.cpd.get_cpd(), np.array([[0.1, 0.1], [0.1, 0.1], [0.8, 0.8]]))

    def test_reduce_3(self):
        self.cpd.reduce([('intel', 0), ('diff', 0)])
        np_test.assert_array_equal(self.cpd.get_cpd(),
                                   np.array([[0.1], [0.1], [0.8]]))

    def test_reduce_4(self):
        self.cpd.reduce(('grade', 0))
        np_test.assert_array_equal(self.cpd.get_cpd(),
                                   np.array([[1, 1, 1, 1, 1, 1]]))

    def test_get_cpd(self):
        np_test.assert_array_equal(
            self.cpd.get_cpd(),
            np.array([[0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                      [0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                      [0.8, 0.8, 0.8, 0.8, 0.8, 0.8]]))

    def tearDown(self):
        del self.cpd
コード例 #9
0
ファイル: test_Factor.py プロジェクト: ankurankan/pgmpy
class TestTabularCPDMethods(unittest.TestCase):
    def setUp(self):
        self.cpd = TabularCPD('grade', 3, [[0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                                           [0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                                           [0.8, 0.8, 0.8, 0.8, 0.8, 0.8]],
                              evidence=['intel', 'diff'], evidence_card=[3, 2])

    def test_marginalize_1(self):
        self.cpd.marginalize(['diff'])
        self.assertEqual(self.cpd.variable, 'grade')
        self.assertEqual(self.cpd.variable_card, 3)
        self.assertListEqual(list(self.cpd.variables), ['grade', 'intel'])
        np_test.assert_array_equal(self.cpd.cardinality, np.array([3, 3]))
        np_test.assert_array_equal(self.cpd.values.ravel(), np.array([0.1, 0.1, 0.1,
                                                                      0.1, 0.1, 0.1,
                                                                      0.8, 0.8, 0.8]))

    def test_marginalize_2(self):
        self.cpd.marginalize(['grade'])
        self.assertEqual(self.cpd.variable, 'grade')
        self.assertListEqual(list(self.cpd.variables), ['diff', 'intel'])
        np_test.assert_array_equal(self.cpd.cardinality, np.array([2, 3]))
        np_test.assert_array_equal(self.cpd.values.ravel(), np.array([1.0, 1.0, 1.0, 1.0, 1.0, 1.0]))

    def test_normalize(self):
        cpd_un_normalized = TabularCPD('grade', 2, [[0.7, 0.2, 0.6, 0.2], [0.4, 0.4, 0.4, 0.8]],
                                       ['intel', 'diff'], [2, 2])
        cpd_un_normalized.normalize()
        np_test.assert_array_almost_equal(cpd_un_normalized.values.ravel(), np.array([0.63636364, 0.33333333, 0.6, 0.2,
                                                                                      0.36363636, 0.66666667, 0.4, 0.8]))

    def test__repr__(self):
        grade_cpd = TabularCPD('grade', 3, [[0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                                            [0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                                            [0.8, 0.8, 0.8, 0.8, 0.8, 0.8]],
                               evidence=['intel', 'diff'], evidence_card=[3, 2])
        intel_cpd = TabularCPD('intel', 3, [[0.5], [0.3], [0.2]])
        diff_cpd = TabularCPD('grade', 3, [[0.1, 0.1], [0.1, 0.1],  [0.8, 0.8]], evidence=['diff'], evidence_card=[2])
        self.assertEqual(repr(grade_cpd), '<TabularCPD representing P(grade:3 | intel:3, diff:2) at {address}>'
                         .format(address=hex(id(grade_cpd))))
        self.assertEqual(repr(intel_cpd), '<TabularCPD representing P(intel:3) at {address}>'
                         .format(address=hex(id(intel_cpd))))
        self.assertEqual(repr(diff_cpd), '<TabularCPD representing P(grade:3 | diff:2) at {address}>'
                         .format(address=hex(id(diff_cpd))))

    def test_reduce_1(self):
        self.cpd.reduce([('diff', 0)])
        np_test.assert_array_equal(self.cpd.get_cpd(), np.array([[0.1, 0.1, 0.1],
                                                                 [0.1, 0.1, 0.1],
                                                                 [0.8, 0.8, 0.8]]))

    def test_reduce_2(self):
        self.cpd.reduce([('intel', 0)])
        np_test.assert_array_equal(self.cpd.get_cpd(), np.array([[0.1, 0.1],
                                                                 [0.1, 0.1],
                                                                 [0.8, 0.8]]))

    def test_reduce_3(self):
        self.cpd.reduce([('intel', 0), ('diff', 0)])
        np_test.assert_array_equal(self.cpd.get_cpd(), np.array([[0.1],
                                                                 [0.1],
                                                                 [0.8]]))

    def test_reduce_4(self):
        self.cpd.reduce([('grade', 0)])
        np_test.assert_array_equal(self.cpd.get_cpd(), np.array([[1, 1, 1, 1, 1, 1]]))

    def test_get_cpd(self):
        np_test.assert_array_equal(self.cpd.get_cpd(), np.array([[0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                                                                 [0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                                                                 [0.8, 0.8, 0.8, 0.8, 0.8, 0.8]]))

    def tearDown(self):
        del self.cpd