Esempio n. 1
0
    def probability(self, x, cols, given=None):
        """ Predictive probability of x_1, ..., x_n given y_1, ..., y_n

        Parameters
        ----------
        x : numpy.ndarray(2,)
            2-D numpy array where each row is a set of observations and each
            column corresponds to a feature.
        cols : list
            The names of each column/feature of `x`.
        given : list(tuple)
            List of (name, value,) conditional contraints for the probability

        Returns
        -------
        logps : numpy.ndarray

        Examples
        --------
        The probability that an animal is fast and agile given that it is
        bulbous.

        >>> engine = Engine.load('examples/zoo.bcmodels')
        >>> engine.probability(np.array([[1, 1]]), ['fast', 'agile'],
        ...                    given=[('bulbous': 1]))
        """
        # TODO: make sure that given goes not caontain columns rom cols
        x = du.format_query_data(x)
        col_idxs = [self._converters['col2idx'][col] for col in cols]
        x_cnv = du.convert_data(x, cols, self._dtypes, self._converters)

        if given is not None:
            given = du.convert_given(given, self._dtypes, self._converters)

        return mu.probability(x_cnv, self._models, col_idxs, given=given)
Esempio n. 2
0
    def probability(self, x, cols, given=None):
        """ Predictive probability of x_1, ..., x_n given y_1, ..., y_n

        Parameters
        ----------
        x : numpy.ndarray(2,)
            2-D numpy array where each row is a set of observations and each
            column corresponds to a feature.
        cols : list
            The names of each column/feature of `x`.
        given : list(tuple)
            List of (name, value,) conditional contraints for the probability

        Returns
        -------
        logps : numpy.ndarray

        Examples
        --------
        The probability that an animal is fast and agile given that it is
        bulbous.

        >>> engine = Engine.load('examples/zoo.bcmodels')
        >>> engine.probability(np.array([[1, 1]]), ['fast', 'agile'],
        ...                    given=[('bulbous': 1]))
        """
        # TODO: make sure that given goes not caontain columns rom cols
        x = du.format_query_data(x)
        col_idxs = [self._converters['col2idx'][col] for col in cols]
        x_cnv = du.convert_data(x, cols, self._dtypes, self._converters)

        if given is not None:
            given = du.convert_given(given, self._dtypes, self._converters)

        return mu.probability(x_cnv, self._models, col_idxs, given=given)
Esempio n. 3
0
def test_format_query_data_should_return_2d_numpy_array_2d_array():
    # 2d array
    x = du.format_query_data(np.eye(2))
    assert isinstance(x, np.ndarray)
    assert x.shape == (
        2,
        2,
    )
Esempio n. 4
0
def test_format_query_data_should_return_2d_numpy_array_list_of_array():
    # list of arrays
    x = du.format_query_data([np.array([.2, 'string']), np.array([.1, 'x'])])
    assert isinstance(x, np.ndarray)
    assert x.shape == (
        2,
        2,
    )
Esempio n. 5
0
def test_format_query_data_should_return_2d_numpy_array_obj_array():
    # single object array
    x = du.format_query_data(np.array([.2, 'string']))
    assert isinstance(x, np.ndarray)
    assert x.shape == (
        1,
        2,
    )
Esempio n. 6
0
def test_format_query_data_should_return_2d_numpy_array_float_array():
    # single float64 array
    x = du.format_query_data(np.array([1.2, 2.1]))
    assert isinstance(x, np.ndarray)
    assert x.shape == (
        1,
        2,
    )
Esempio n. 7
0
def test_format_query_data_should_return_2d_numpy_array_list():
    # single list
    x = du.format_query_data([1.2, 'string'])
    assert isinstance(x, np.ndarray)
    assert x.shape == (
        1,
        2,
    )
Esempio n. 8
0
def test_format_query_data_should_return_2d_numpy_array_str():
    # single string data
    x = du.format_query_data('string')
    assert isinstance(x, np.ndarray)
    assert x.shape == (
        1,
        1,
    )
Esempio n. 9
0
def test_format_query_data_should_return_2d_numpy_array_float():
    # single float data
    x = du.format_query_data(1.2)
    assert isinstance(x, np.ndarray)
    assert x.shape == (
        1,
        1,
    )
Esempio n. 10
0
def test_format_query_data_should_return_2d_numpy_array_list_of_array():
    # list of arrays
    x = du.format_query_data([np.array([.2, 'string']), np.array([.1, 'x'])])
    assert isinstance(x, np.ndarray)
    assert x.shape == (2, 2,)
Esempio n. 11
0
def test_format_query_data_should_return_2d_numpy_array_2d_array():
    # 2d array
    x = du.format_query_data(np.eye(2))
    assert isinstance(x, np.ndarray)
    assert x.shape == (2, 2,)
Esempio n. 12
0
def test_format_query_data_should_return_2d_numpy_array_obj_array():
    # single object array
    x = du.format_query_data(np.array([.2, 'string']))
    assert isinstance(x, np.ndarray)
    assert x.shape == (1, 2,)
Esempio n. 13
0
def test_format_query_data_should_return_2d_numpy_array_float_array():
    # single float64 array
    x = du.format_query_data(np.array([1.2, 2.1]))
    assert isinstance(x, np.ndarray)
    assert x.shape == (1, 2,)
Esempio n. 14
0
def test_format_query_data_should_return_2d_numpy_array_list():
    # single list
    x = du.format_query_data([1.2, 'string'])
    assert isinstance(x, np.ndarray)
    assert x.shape == (1, 2,)
Esempio n. 15
0
def test_format_query_data_should_return_2d_numpy_array_str():
    # single string data
    x = du.format_query_data('string')
    assert isinstance(x, np.ndarray)
    assert x.shape == (1, 1,)
Esempio n. 16
0
def test_format_query_data_should_return_2d_numpy_array_float():
    # single float data
    x = du.format_query_data(1.2)
    assert isinstance(x, np.ndarray)
    assert x.shape == (1, 1,)