Skip to content

autora.experimentalist.prediction_filter

Example Experimentalist

filter(conditions, model, filter_function)

Filter conditions based on the expected outcome io the mdeol

Parameters:

Name Type Description Default
conditions Union[DataFrame, ndarray]

The pool to filter

required
model BaseEstimator

The model to make the prediction

required
filter_function Callable

A function that returns True if a prediciton should be included

required

Returns:

Type Description
DataFrame

Filtered pool of experimental conditions

Examples:

>>> class ModelLinear:
...     def predict(self, X):
...         return 2 * X + 1
>>> model = ModelLinear()
>>> model.predict(4)
9

For the filter function, be aware of the output type of the predict function. For example, here, we expect a list with a single entry

>>> filter_fct = lambda x: 5 < x < 10
>>> pool = pd.DataFrame({'x': [1, 2, 3, 4, 5, 6]})
>>> filter(pool, model, filter_fct)
   x
0  3
1  4
>>> filter_fct_2d = lambda x: 4 < x[0] + x[1] < 10
>>> pool = np.array([[1, 0], [0, 1], [0, 1], [1 ,1], [2, 2]])
>>> model.predict(pool)
array([[3, 1],
       [1, 3],
       [1, 3],
       [3, 3],
       [5, 5]])
>>> filter(pool, model, filter_fct_2d)
array([[1, 1]])
Source code in temp_dir/prediction-filter/src/autora/experimentalist/prediction_filter/__init__.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
def filter(
    conditions: Union[pd.DataFrame, np.ndarray],
    model: BaseEstimator,
    filter_function: Callable,
) -> pd.DataFrame:
    """
    Filter conditions based on the expected outcome io the mdeol

    Args:
        conditions: The pool to filter
        model: The model to make the prediction
        filter_function: A function that returns True if a prediciton should be included

    Returns:
        Filtered pool of experimental conditions

    Examples:
        >>> class ModelLinear:
        ...     def predict(self, X):
        ...         return 2 * X + 1
        >>> model = ModelLinear()
        >>> model.predict(4)
        9

        For the filter function, be aware of the output type of the predict function. For example,
        here, we expect a list with a single entry
        >>> filter_fct = lambda x: 5 < x < 10
        >>> pool = pd.DataFrame({'x': [1, 2, 3, 4, 5, 6]})
        >>> filter(pool, model, filter_fct)
           x
        0  3
        1  4

        >>> filter_fct_2d = lambda x: 4 < x[0] + x[1] < 10
        >>> pool = np.array([[1, 0], [0, 1], [0, 1], [1 ,1], [2, 2]])
        >>> model.predict(pool)
        array([[3, 1],
               [1, 3],
               [1, 3],
               [3, 3],
               [5, 5]])

        >>> filter(pool, model, filter_fct_2d)
        array([[1, 1]])

    """
    c_array = np.array(conditions)
    if len(c_array.shape) == 1:
        c_array = c_array.reshape(-1, 1)

    _pred = model.predict(c_array)
    _filter = np.apply_along_axis(filter_function, 1, _pred)
    _filter = _filter.reshape(1, -1)

    new_conditions = c_array[list(_filter[0])]

    if isinstance(conditions, pd.DataFrame):
        new_conditions = pd.DataFrame(new_conditions, columns=conditions.columns)

    return new_conditions