adaptive.Learner1D¶
Learner1D
¶
Learner1D(function: Callable[[Real], Float | ndarray], bounds: tuple[Real, Real], loss_per_interval: Callable[[XsTypeN, YsTypeN], Float] | None = None)
Bases: BaseLearner
Learns and predicts a function 'f:ℝ → ℝ^N'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
function
|
callable
|
The function to learn. Must take a single real parameter and return a real number or 1D array. |
required |
bounds
|
pair of reals
|
The bounds of the interval on which to learn 'function'. |
required |
loss_per_interval
|
Callable[[XsTypeN, YsTypeN], Float] | None
|
A function that returns the loss for a single interval of the domain. If not provided, then a default is used, which uses the scaled distance in the x-y plane as the loss. See the notes for more details. |
None
|
Attributes:
| Name | Type | Description |
|---|---|---|
data |
dict
|
Sampled points and values. |
pending_points |
set
|
Points that still have to be evaluated. |
Notes
loss_per_interval takes 2 parameters: xs and ys, and returns a
scalar; the loss over the interval.
xs : tuple of floats
The x values of the interval, if nth_neighbors is greater than zero it
also contains the x-values of the neighbors of the interval, in ascending
order. The interval we want to know the loss of is then the middle
interval. If no neighbor is available (at the edges of the domain) then
None will take the place of the x-value of the neighbor.
ys : tuple of function values
The output values of the function when evaluated at the xs. This is
either a float or a tuple of floats in the case of vector output.
The loss_per_interval function may also have an attribute nth_neighbors
that indicates how many of the neighboring intervals to interval are used.
If loss_per_interval doesn't have such an attribute, it's assumed that is
uses no neighboring intervals. Also see the uses_nth_neighbors
decorator for more information.
Source code in adaptive/learner/learner1D.py
vdim
property
¶
vdim: int
Length of the output of learner.function.
If the output is unsized (when it's a scalar)
then vdim = 1.
As long as no data is known vdim = 1.
to_numpy
¶
Data as NumPy array of size (npoints, 2) if learner.function returns a scalar
and (npoints, 1+vdim) if learner.function returns a vector of length vdim.
Source code in adaptive/learner/learner1D.py
to_dataframe
¶
to_dataframe(with_default_function_args: bool = True, function_prefix: str = 'function.', x_name: str = 'x', y_name: str = 'y') -> DataFrame
Return the data as a pandas.DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
with_default_function_args
|
bool
|
Include the |
True
|
function_prefix
|
str
|
Prefix to the |
'function.'
|
x_name
|
str
|
Name of the input value, by default "x" |
'x'
|
y_name
|
str
|
Name of the output value, by default "y" |
'y'
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
|
Raises:
| Type | Description |
|---|---|
ImportError
|
If |
Source code in adaptive/learner/learner1D.py
load_dataframe
¶
load_dataframe(df: DataFrame, with_default_function_args: bool = True, function_prefix: str = 'function.', x_name: str = 'x', y_name: str = 'y') -> 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.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
The data to load. |
required |
with_default_function_args
|
bool
|
The |
True
|
function_prefix
|
str
|
The |
'function.'
|
x_name
|
str
|
The |
'x'
|
y_name
|
str
|
The |
'y'
|
Source code in adaptive/learner/learner1D.py
loss
¶
Source code in adaptive/learner/learner1D.py
tell
¶
Source code in adaptive/learner/learner1D.py
tell_many
¶
tell_many(xs: Sequence[Float] | ndarray, ys: Sequence[Float] | Sequence[Sequence[Float]] | Sequence[ndarray] | ndarray, *, force: bool = False) -> None
Source code in adaptive/learner/learner1D.py
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 | |
ask
¶
Return 'n' points that are expected to maximally reduce the loss.
Source code in adaptive/learner/learner1D.py
plot
¶
plot(*, scatter_or_line: str = 'scatter')
Returns a plot of the evaluated data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
scatter_or_line
|
str
|
Plot as a scatter plot ("scatter") or a line plot ("line"). |
"scatter"
|
Returns:
| Name | Type | Description |
|---|---|---|
plot |
Overlay
|
Plot of the evaluated data. |
Source code in adaptive/learner/learner1D.py
Custom loss functions¶
default_loss
¶
Calculate loss on a single interval.
Currently returns the rescaled length of the interval. If one of the y-values is missing, returns 0 (so the intervals with missing data are never touched. This behavior should be improved later.
Source code in adaptive/learner/learner1D.py
uniform_loss
¶
Loss function that samples the domain uniformly.
Works with Learner1D only.
Examples:
>>> def f(x):
... return x**2
>>>
>>> learner = adaptive.Learner1D(f,
... bounds=(-1, 1),
... loss_per_interval=uniform_sampling_1d)
>>>
Source code in adaptive/learner/learner1D.py
uses_nth_neighbors
¶
uses_nth_neighbors(n: int)
Decorator to specify how many neighboring intervals the loss function uses.
Wraps loss functions to indicate that they expect intervals together
with n nearest neighbors
The loss function will then receive the data of the N nearest neighbors
(nth_neighbors) along with the data of the interval itself in a dict.
The Learner1D will also make sure that the loss is updated
whenever one of the nth_neighbors changes.
Examples:
The next function is a part of the curvature_loss_function function.
>>> @uses_nth_neighbors(1)
... def triangle_loss(xs, ys):
... xs = [x for x in xs if x is not None]
... ys = [y for y in ys if y is not None]
...
... if len(xs) == 2: # we do not have enough points for a triangle
... return xs[1] - xs[0]
...
... N = len(xs) - 2 # number of constructed triangles
... if isinstance(ys[0], Iterable):
... pts = [(x, *y) for x, y in zip(xs, ys)]
... vol = simplex_volume_in_embedding
... else:
... pts = [(x, y) for x, y in zip(xs, ys)]
... vol = volume
... return sum(vol(pts[i:i+3]) for i in range(N)) / N
Or you may define a loss that favours the (local) minima of a function, assuming that you know your function will have a single float as output.
>>> @uses_nth_neighbors(1)
... def local_minima_resolving_loss(xs, ys):
... dx = xs[2] - xs[1] # the width of the interval of interest
...
... if not ((ys[0] is not None and ys[0] > ys[1])
... or (ys[3] is not None and ys[3] > ys[2])):
... return loss * 100
...
... return loss
Source code in adaptive/learner/base_learner.py
triangle_loss
¶
Source code in adaptive/learner/learner1D.py
curvature_loss_function
¶
curvature_loss_function(area_factor: Real = 1, euclid_factor: Real = 0.02, horizontal_factor: Real = 0.02) -> Callable[[XsType1, YsType1], Float]
Source code in adaptive/learner/learner1D.py
abs_min_log_loss
¶
Calculate loss of a single interval that prioritizes the absolute minimum.
Source code in adaptive/learner/learner1D.py
resolution_loss_function
¶
resolution_loss_function(min_length: Real = 0, max_length: Real = 1) -> Callable[[XsType0, YsType0], Float]
Loss function that is similar to the default_loss function, but you
can set the maximum and minimum size of an interval.
Works with Learner1D only.
The arguments min_length and max_length should be in between 0 and 1
because the total size is normalized to 1.
Returns:
| Name | Type | Description |
|---|---|---|
loss_function |
callable
|
|
Examples:
>>> def f(x):
... return x**2
>>>
>>> loss = resolution_loss_function(min_length=0.01, max_length=1)
>>> learner = adaptive.Learner1D(f, bounds=(-1, -1), loss_per_interval=loss)