示例#1
0
    def convert_to_table(self, evidence):
        """
        This function evaluates a tabular CPT using observed evidence,
        by taking 'slices' of the CPT. Returns the 'sliced' CPT and a list
        of the observed nodes.

        Parameters
        ----------
        evidence: List
            A list of any observed evidence, where evidence[i] = [] if
            node i is hidden, or evidence[i] = SomeValue if node i has been
            observed as having SomeValue.
        """
        odom = []
        vals = []
        positions = []
        count = 0
        for i in self.domain:
            if type(evidence[i]) != list:
                odom.append(i)
                vals.append(evidence[i])
                positions.append(count)
            count = count + 1
        """
        The following code has the effect of 'slicing' the table. The idea is to
        select a certain slice out of each dimension, in this case, select slice
        vals[i] out of dimension positions[i]. So if positions = [0, 2, 3], and
        we were slicing 4-D array T, the resulting slice would be equal to
        T[vals[0], :, vals[1], vals[2]].
        """
        index = mk_multi_index(len(self.domain), positions, vals)
        self.T = self.T[index]
        self.T = self.T.squeeze()

        return odom
def test_mk_multi_index():
    """
    FUNCTION: mk_multi_index, in general.py.
    """
    """Execute function"""
    index = general.mk_multi_index(3, [1], [1])
    """Assert the function executed correctly"""
    assert index == [slice(None, None, None), slice(1, 2, None), \
                     slice(None, None, None)]
def test_mk_multi_index():
    """
    FUNCTION: mk_multi_index, in general.py.
    """
    """Execute function"""
    index = general.mk_multi_index(3, [1], [1])

    """Assert the function executed correctly"""
    assert index == [slice(None, None, None), slice(1, 2, None), slice(None, None, None)]
示例#4
0
    def convert_to_table(self, domain, evidence=[]):
        """
        This function evaluates a tabular CPD's CPT using observed evidence,
        by taking 'slices' of the CPT. Returns the 'sliced' CPT and a list
        of the observed nodes.

        Parameters
        ----------
        domain: List
            The domain over which the CPD is defined, which is a list of
            indices of the nodes that the CPD encompasses.

        evidence: List
            A list of any observed evidence, where evidence[i] = [] if
            node i is hidden, or evidence[i] = SomeValue if node i has been
            observed as having SomeValue.
        """
        odom = []
        vals = []
        positions = []
        count = 0
        
        """If there is any observed evidence"""
        if len(evidence) != 0:
            """For every node in the CPDs domain"""
            for i in domain:
                """If this node has been observed"""
                if evidence[i] is not None:
                    """Add it to the list of observed nodes"""
                    odom.append(i)
                    vals.append(evidence[i])
                    positions.append(count)
                count = count + 1

        """
        The following code has the effect of 'slicing' the table. The idea is to
        select a certain slice out of each dimension, in this case, select slice
        vals[i] out of dimension positions[i]. So if positions = [0, 2, 3], and
        we were slicing 4-D array T, the resulting slice would be equal to
        T[vals[0], :, vals[1], vals[2]]. This has the effect of clamping
        observed variables at their observed values.
        """
        index = general.mk_multi_index(len(domain), positions, vals)
        T = self.CPT[index]
        T = T.squeeze()

        return [T, odom]
示例#5
0
    def convert_to_table(self, domain, evidence=[]):
        """
        This function evaluates a tabular CPD's CPT using observed evidence,
        by taking 'slices' of the CPT. Returns the 'sliced' CPT and a list
        of the observed nodes.

        Parameters
        ----------
        domain: List
            The domain over which the CPD is defined, which is a list of
            indices of the nodes that the CPD encompasses.

        evidence: List
            A list of any observed evidence, where evidence[i] = [] if
            node i is hidden, or evidence[i] = SomeValue if node i has been
            observed as having SomeValue.
        """
        odom = []
        vals = []
        positions = []
        count = 0
        
        """If there is any observed evidence"""
        if len(evidence) != 0:
            """For every node in the CPDs domain"""
            for i in domain:
                """If this node has been observed"""
                if evidence[i] != []:
                    """Add it to the list of observed nodes"""
                    odom.append(i)
                    vals.append(evidence[i])
                    positions.append(count)
                count = count + 1

        """
        The following code has the effect of 'slicing' the table. The idea is to
        select a certain slice out of each dimension, in this case, select slice
        vals[i] out of dimension positions[i]. So if positions = [0, 2, 3], and
        we were slicing 4-D array T, the resulting slice would be equal to
        T[vals[0], :, vals[1], vals[2]]. This has the effect of clamping
        observed variables at their observed values.
        """
        index = general.mk_multi_index(len(domain), positions, vals)
        T = self.CPT[index]
        T = T.squeeze()

        return [T, odom]
    def convert_to_table(self, evidence):
        """
        This function evaluates a tabular CPT using observed evidence,
        by taking 'slices' of the CPT. Returns the 'sliced' CPT and a list
        of the observed nodes.

        Parameters
        ----------
        evidence: List
            A list of any observed evidence, where evidence[i] = [] if
            node i is hidden, or evidence[i] = SomeValue if node i has been
            observed as having SomeValue.
        """
        odom = []
        vals = []
        positions = []
        count = 0
        for i in self.domain:
            if evidence[i] is not None:
                odom.append(i)
                vals.append(evidence[i])
                positions.append(count)
            count = count + 1

        """
        The following code has the effect of 'slicing' the table. The idea is to
        select a certain slice out of each dimension, in this case, select slice
        vals[i] out of dimension positions[i]. So if positions = [0, 2, 3], and
        we were slicing 4-D array T, the resulting slice would be equal to
        T[vals[0], :, vals[1], vals[2]].
        """
        index = mk_multi_index(len(self.domain), positions, vals)
        self.T = self.T[index]
        self.T = self.T.squeeze()

        return odom