Skip to content

adaptive.BaseLearner

BaseLearner

Bases: ABC

Base class for algorithms for learning a function 'f: X → Y'.

Attributes:

Name Type Description
function callable: X → Y

The function to learn. A subclass of BaseLearner might modify the user's supplied function.

data dict: X → Y

function evaluated at certain points.

pending_points set

Points that have been requested but have not been evaluated yet.

npoints int

The number of evaluated points that have been added to the learner.

Notes

Subclasses may define a plot method that takes no parameters and returns a holoviews plot.

data instance-attribute

data: DataType

npoints instance-attribute

npoints: int

pending_points instance-attribute

pending_points: set

function instance-attribute

function: Callable[..., Any]

tell

tell(x, y)

Tell the learner about a single value.

Parameters:

Name Type Description Default
x A value from the function domain
required
y A value from the function image
required
Source code in adaptive/learner/base_learner.py
def tell(self, x, y):
    """Tell the learner about a single value.

    Parameters
    ----------
    x : A value from the function domain
    y : A value from the function image
    """
    self.tell_many([x], [y])

tell_many

tell_many(xs, ys)

Tell the learner about some values.

Parameters:

Name Type Description Default
xs Iterable of values from the function domain
required
ys Iterable of values from the function image
required
Source code in adaptive/learner/base_learner.py
def tell_many(self, xs, ys):
    """Tell the learner about some values.

    Parameters
    ----------
    xs : Iterable of values from the function domain
    ys : Iterable of values from the function image
    """
    for x, y in zip(xs, ys):
        self.tell(x, y)

tell_pending abstractmethod

tell_pending(x)

Tell the learner that 'x' has been requested such that it's not suggested again.

Source code in adaptive/learner/base_learner.py
@abc.abstractmethod
def tell_pending(self, x):
    """Tell the learner that 'x' has been requested such
    that it's not suggested again."""

remove_unfinished abstractmethod

remove_unfinished()

Remove uncomputed data from the learner.

Source code in adaptive/learner/base_learner.py
@abc.abstractmethod
def remove_unfinished(self):
    """Remove uncomputed data from the learner."""

loss abstractmethod

loss(real=True)

Return the loss for the current state of the learner.

Parameters:

Name Type Description Default
real bool

If False, return the "expected" loss, i.e. the loss including the as-yet unevaluated points (possibly by interpolation).

True
Source code in adaptive/learner/base_learner.py
@abc.abstractmethod
def loss(self, real=True):
    """Return the loss for the current state of the learner.

    Parameters
    ----------
    real : bool, default: True
        If False, return the "expected" loss, i.e. the
        loss including the as-yet unevaluated points
        (possibly by interpolation).
    """

ask abstractmethod

ask(n, tell_pending=True)

Choose the next 'n' points to evaluate.

Parameters:

Name Type Description Default
n int

The number of points to choose.

required
tell_pending bool

If True, add the chosen points to this learner's pending_points. Set this to False if you do not want to modify the state of the learner.

True
Source code in adaptive/learner/base_learner.py
@abc.abstractmethod
def ask(self, n, tell_pending=True):
    """Choose the next 'n' points to evaluate.

    Parameters
    ----------
    n : int
        The number of points to choose.
    tell_pending : bool, default: True
        If True, add the chosen points to this learner's
        `pending_points`. Set this to False if you do not
        want to modify the state of the learner.
    """

new abstractmethod

new()

Return a new learner with the same function and parameters.

Source code in adaptive/learner/base_learner.py
@abc.abstractmethod
def new(self):
    """Return a new learner with the same function and parameters."""
    pass

copy_from

copy_from(other)

Copy over the data from another learner.

Parameters:

Name Type Description Default
other BaseLearner object

The learner from which the data is copied.

required
Source code in adaptive/learner/base_learner.py
def copy_from(self, other):
    """Copy over the data from another learner.

    Parameters
    ----------
    other : BaseLearner object
        The learner from which the data is copied.
    """
    self._set_data(other._get_data())

save

save(fname, compress=True)

Save the data of the learner into a pickle file.

Parameters:

Name Type Description Default
fname str

The filename into which to save the learner's data.

required
compress bool

Compress the data upon saving using 'gzip'. When saving using compression, one must load it with compression too.

True
Source code in adaptive/learner/base_learner.py
def save(self, fname, compress=True):
    """Save the data of the learner into a pickle file.

    Parameters
    ----------
    fname : str
        The filename into which to save the learner's data.
    compress : bool, default True
        Compress the data upon saving using 'gzip'. When saving
        using compression, one must load it with compression too.
    """
    data = self._get_data()
    save(fname, data, compress)

load

load(fname, compress=True)

Load the data of a learner from a pickle file.

Parameters:

Name Type Description Default
fname str

The filename from which to load the learner's data.

required
compress bool

If the data is compressed when saved, one must load it with compression too.

True
Source code in adaptive/learner/base_learner.py
def load(self, fname, compress=True):
    """Load the data of a learner from a pickle file.

    Parameters
    ----------
    fname : str
        The filename from which to load the learner's data.
    compress : bool, default True
        If the data is compressed when saved, one must load it
        with compression too.
    """
    with suppress(FileNotFoundError, EOFError):
        data = load(fname, compress)
        self._set_data(data)

to_dataframe abstractmethod

to_dataframe(with_default_function_args: bool = True, function_prefix: str = 'function.', **kwargs: Any) -> DataFrame

Return the data as a pandas.DataFrame.

Parameters:

Name Type Description Default
with_default_function_args bool

Include the learner.function's default arguments as a column, by default True

True
function_prefix str

Prefix to the learner.function's default arguments' names, by default "function."

'function.'
x_name str

Name of the input value, by default "x"

required
y_name str

Name of the output value, by default "y"

required

Returns:

Type Description
DataFrame
Source code in adaptive/learner/base_learner.py
@abc.abstractmethod
def to_dataframe(
    self,
    with_default_function_args: bool = True,
    function_prefix: str = "function.",
    **kwargs: Any,
) -> pandas.DataFrame:
    """Return the data as a `pandas.DataFrame`.

    Parameters
    ----------
    with_default_function_args : bool, optional
        Include the ``learner.function``'s default arguments as a
        column, by default True
    function_prefix : str, optional
        Prefix to the ``learner.function``'s default arguments' names,
        by default "function."
    x_name : str, optional
        Name of the input value, by default "x"
    y_name : str, optional
        Name of the output value, by default "y"

    Returns
    -------
    pandas.DataFrame
    """

load_dataframe abstractmethod

load_dataframe(df: DataFrame, with_default_function_args: bool = True, function_prefix: str = 'function.', **kwargs: Any) -> None

Load data from a pandas.DataFrame.

If with_default_function_args is True, then learner.function's default arguments are set (using functools.partial) from the values in the pandas.DataFrame.

Source code in adaptive/learner/base_learner.py
@abc.abstractmethod
def load_dataframe(
    self,
    df: pandas.DataFrame,
    with_default_function_args: bool = True,
    function_prefix: str = "function.",
    **kwargs: Any,
) -> None:
    """Load data from a `pandas.DataFrame`.

    If ``with_default_function_args`` is True, then ``learner.function``'s
    default arguments are set (using `functools.partial`) from the values
    in the `pandas.DataFrame`.
    """