adaptive.LearnerND#
- class adaptive.LearnerND(func, bounds, loss_per_simplex=None, *, anisotropic=False, triangulation_backend='auto')[source]#
Bases:
BaseLearnerLearns and predicts a function βf: β^N β β^Mβ.
- Parameters:
func (callable) β The function to learn. Must take a tuple of N real parameters and return a real number or an arraylike of length M.
bounds (list of 2-tuples or
scipy.spatial.ConvexHull) β A list[(a_1, b_1), (a_2, b_2), ..., (a_n, b_n)]containing bounds, one pair per dimension. Or a ConvexHull that defines the boundary of the domain.loss_per_simplex (callable, optional) β A function that returns the loss for a simplex. If not provided, then a default is used, which uses the deviation from a linear estimate, as well as triangle area, to determine the loss.
anisotropic (bool, optional) β If True, the triangulation is stretched along the local gradient when choosing new points. Only works with scalar output.
triangulation_backend (str or type, optional) β Which triangulation implementation to use:
"auto"(default, prefers the optional Rust-accelerated adaptive-triangulation package when it is installed),"python","rust", or aTriangulation-compatible class.
- points#
Coordinates of the currently known points
- Type:
numpy array
- values#
The values of each of the known points
- Type:
numpy array
Notes
The sample points are chosen by estimating the point where the gradient is maximal. This is based on the currently known points.
In practice, this sampling protocol results to sparser sampling of flat regions, and denser sampling of regions where the function has a high gradient, which is useful if the function is expensive to compute.
This sampling procedure is not fast, so to benefit from it, your function needs to be slow enough to compute.
This class keeps track of all known points. It triangulates these points and with every simplex it associates a loss. Then if you request points that you will compute in the future, it will subtriangulate a real simplex with the pending points inside it and distribute the loss among itβs children based on volume.
- property bounds_are_done#
- load_dataframe(df: DataFrame, with_default_function_args: bool = True, function_prefix: str = 'function.', point_names: tuple[str, ...] = ('x', 'y', 'z'), value_name: str = 'value')[source]#
Load data from a
pandas.DataFrame.If
with_default_function_argsis True, thenlearner.functionβs default arguments are set (usingfunctools.partial) from the values in thepandas.DataFrame.- Parameters:
df (pandas.DataFrame) β The data to load.
with_default_function_args (bool, optional) β The
with_default_function_argsused into_dataframe(), by default Truefunction_prefix (str, optional) β The
function_prefixused into_dataframe, by default βfunction.βpoint_names (str, optional) β The
point_namesused into_dataframe, by default (βxβ, βyβ, βzβ)value_name (str, optional) β The
value_nameused into_dataframe, by default βvalueβ
- loss(real=True)[source]#
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).
- property npoints#
Number of evaluated points.
- plot_3D(with_triangulation=False, return_fig=False)[source]#
Plot the learnerβs data in 3D using plotly.
Does not work with the
adaptive.notebook_integration.live_plotfunctionality.- Parameters:
- Returns:
plot β The 3D plot of
learner.data.- Return type:
plotly.offline.iplotobject
- plot_isoline(level=0.0, n=None, tri_alpha=0)[source]#
Plot the isoline at a specific level, only works in 2D.
- Parameters:
- Returns:
The plot of the isoline(s). This overlays a
plotwith aholoviews.element.Path.- Return type:
- plot_isosurface(level=0.0, hull_opacity=0.2)[source]#
Plots a linearly interpolated isosurface.
This is the 3D analog of an isoline. Does not work with the
adaptive.notebook_integration.live_plotfunctionality.
- plot_slice(cut_mapping, n=None)[source]#
Plot a 1D or 2D interpolated slice of a N-dimensional function.
- property points#
Get the points from data as a numpy array.
- tell(point, value)[source]#
Tell the learner about a single value.
- Parameters:
x (A value from the function domain) β
y (A value from the function image) β
- tell_pending(point, *, simplex=None)[source]#
Tell the learner that βxβ has been requested such that itβs not suggested again.
- to_dataframe(with_default_function_args: bool = True, function_prefix: str = 'function.', point_names: tuple[str, ...] = ('x', 'y', 'z'), value_name: str = 'value') DataFrame[source]#
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 Truefunction_prefix (str, optional) β Prefix to the
learner.functionβs default argumentsβ names, by default βfunction.βpoint_names (tuple[str, ...], optional) β Names of the input points, should be the same length as number of input parameters by default (βxβ, βyβ, βzβ )
value_name (str, optional) β Name of the output value, by default βvalueβ
- Return type:
pandas.DataFrame
- Raises:
ImportError β If
pandasis not installed.
- to_numpy()[source]#
Data as NumPy array of size
(npoints, dim+vdim), wheredimis the size of the input dimension andvdimis the length of the return value oflearner.function.
- property tri#
An
adaptive.learner.triangulation.Triangulationinstance with all the points of the learner.
- property values#
Get the values from data as a numpy array.
- property vdim#
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.
Custom loss functions#
- adaptive.learner.learnerND.default_loss(simplex, values, value_scale)[source]#
Computes the average of the volumes of the simplex.