Пример #1
0
def test_convert_continuous_given_should_change_cols_not_vals():
    given_in = [(
        'x',
        1.2,
    ), (
        'y',
        3.4,
    ), (
        'y',
        2,
    )]
    dtypes = ['continuous'] * 2
    converters = {'col2idx': {'x': 0, 'y': 1}, 'valmaps': {}}

    given_out = du.convert_given(given_in, dtypes, converters)

    for (k1, v1), (k2, v2) in zip(given_in, given_out):
        assert k1 != k2
        assert v1 == v2
        assert isinstance(k1, str)
        assert isinstance(k2, int)

    assert given_out[0] == (
        0,
        1.2,
    )
    assert given_out[1] == (
        1,
        3.4,
    )
    assert given_out[2] == (
        1,
        2,
    )
Пример #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)
Пример #3
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)
Пример #4
0
def test_convert_categorical_given_should_change_cols_and_vals():
    given_in = [(
        'x',
        'a',
    ), (
        'y',
        'bb',
    ), (
        'y',
        'cc',
    )]
    dtypes = ['categorical'] * 2
    converters = {
        'col2idx': {
            'x': 0,
            'y': 1
        },
        'valmaps': {
            'x': {
                'val2idx': {
                    'a': 0,
                    'b': 1,
                    'c': 2
                }
            },
            'y': {
                'val2idx': {
                    'aa': 0,
                    'bb': 1,
                    'cc': 2
                }
            }
        }
    }

    given_out = du.convert_given(given_in, dtypes, converters)

    assert given_out[0] == (
        0,
        0,
    )
    assert given_out[1] == (
        1,
        1,
    )
    assert given_out[2] == (
        1,
        2,
    )
Пример #5
0
def test_convert_mixed_given_output():
    given_in = [('x', 1.2,), ('y', 'bb',), ('y', 'cc',)]
    dtypes = ['continuous', 'categorical']
    converters = {
        'col2idx':
            {'x': 0, 'y': 1},
        'valmaps': {
            'y': {
                'val2idx': {'aa': 0, 'bb': 1, 'cc': 2}}}}

    given_out = du.convert_given(given_in, dtypes, converters)

    assert given_out[0] == (0, 1.2,)
    assert given_out[1] == (1, 1,)
    assert given_out[2] == (1, 2,)
Пример #6
0
def test_convert_categorical_given_should_change_cols_and_vals():
    given_in = [('x', 'a',), ('y', 'bb',), ('y', 'cc',)]
    dtypes = ['categorical']*2
    converters = {
        'col2idx':
            {'x': 0, 'y': 1},
        'valmaps': {
            'x': {
                'val2idx': {'a': 0, 'b': 1, 'c': 2}},
            'y': {
                'val2idx': {'aa': 0, 'bb': 1, 'cc': 2}}}}

    given_out = du.convert_given(given_in, dtypes, converters)

    assert given_out[0] == (0, 0,)
    assert given_out[1] == (1, 1,)
    assert given_out[2] == (1, 2,)
Пример #7
0
def test_convert_continuous_given_should_change_cols_not_vals():
    given_in = [('x', 1.2,), ('y', 3.4,), ('y', 2,)]
    dtypes = ['continuous']*2
    converters = {
        'col2idx': {'x': 0, 'y': 1},
        'valmaps': {}}

    given_out = du.convert_given(given_in, dtypes, converters)

    for (k1, v1), (k2, v2) in zip(given_in, given_out):
        assert k1 != k2
        assert v1 == v2
        assert isinstance(k1, str)
        assert isinstance(k2, int)

    assert given_out[0] == (0, 1.2,)
    assert given_out[1] == (1, 3.4,)
    assert given_out[2] == (1, 2,)
Пример #8
0
def test_convert_mixed_given_output():
    given_in = [(
        'x',
        1.2,
    ), (
        'y',
        'bb',
    ), (
        'y',
        'cc',
    )]
    dtypes = ['continuous', 'categorical']
    converters = {
        'col2idx': {
            'x': 0,
            'y': 1
        },
        'valmaps': {
            'y': {
                'val2idx': {
                    'aa': 0,
                    'bb': 1,
                    'cc': 2
                }
            }
        }
    }

    given_out = du.convert_given(given_in, dtypes, converters)

    assert given_out[0] == (
        0,
        1.2,
    )
    assert given_out[1] == (
        1,
        1,
    )
    assert given_out[2] == (
        1,
        2,
    )
Пример #9
0
    def sample(self, cols, given=None, n=1):
        """ Draw samples from cols

        Parameters
        ----------
        cols : list(index)
            List of columns from which to jointly draw.
        given : list(tuple(int, value))
            List of column-value tuples that specify conditions
        n : int
            The number of samples to draw

        Examples
        --------
        Draw whether an animal is fast and agile given that it is bulbous.

        >>> engine = Engine.load('examples/zoo.bcmodels')
        >>> engine.sample(['fast', 'agile'], given=[('bulbous': 1]))
        """
        # FIXME: make sure that given does not contain columns from cols
        if given is not None:
            given = du.convert_given(given, self._dtypes, self._converters)

        col_idxs = [self._converters['col2idx'][col] for col in cols]

        data_out = mu.sample(self._models, col_idxs, given=given, n=n)

        x = du.convert_data(data_out,
                            cols,
                            self._dtypes,
                            self._converters,
                            to_val=True)

        if x.shape == (
                1,
                1,
        ):
            return x[0, 0]
        elif x.shape[0] == 1:
            return x[0, :]
        else:
            return x
Пример #10
0
    def sample(self, cols, given=None, n=1):
        """ Draw samples from cols

        Parameters
        ----------
        cols : list(index)
            List of columns from which to jointly draw.
        given : list(tuple(int, value))
            List of column-value tuples that specify conditions
        n : int
            The number of samples to draw

        Examples
        --------
        Draw whether an animal is fast and agile given that it is bulbous.

        >>> engine = Engine.load('examples/zoo.bcmodels')
        >>> engine.sample(['fast', 'agile'], given=[('bulbous': 1]))
        """
        # FIXME: make sure that given does not contain columns from cols
        if given is not None:
            given = du.convert_given(given, self._dtypes, self._converters)

        col_idxs = [self._converters['col2idx'][col] for col in cols]

        data_out = mu.sample(self._models, col_idxs, given=given, n=n)

        x = du.convert_data(data_out, cols, self._dtypes, self._converters,
                            to_val=True)

        if x.shape == (1, 1,):
            return x[0, 0]
        elif x.shape[0] == 1:
            return x[0, :]
        else:
            return x