lp_distance#
- skfda.misc.metrics.lp_distance(fdata1, fdata2, *, p, vector_norm=None)[source]#
Lp distance for FDataGrid objects.
Calculates the distance between two functional objects.
For each pair of observations f and g the distance between them is defined as:
\[d(f, g) = d(g, f) = \| f - g \|_p\]where \(\| {}\cdot{} \|_p\) denotes the
Lp norm
.Note
This function is a wrapper of
LpDistance
, available only for convenience. As the parameterp
is mandatory, it cannot be used where a fully-defined metric is required: use an instance ofLpDistance
in those cases.- Parameters:
fdata1 (T) – First FData object.
fdata2 (T) – Second FData object.
p (float) – p of the lp norm. Must be greater or equal than 1. If
p=math.inf
it is used the L infinity metric. Defaults to 2.vector_norm (Norm[ndarray[Any, dtype[float64]]] | float | None) – vector norm to apply. If it is a float, is the index of the multivariate lp norm. Defaults to the same as
p
.
- Returns:
Numpy vector where the i-th coordinate has the distance between the i-th element of the first object and the i-th element of the second one.
- Return type:
Examples
Computes the distances between an object containing functional data corresponding to the functions y = 1 and y = x defined over the interval [0, 1] and another ones containing data of the functions y = 0 and y = x/2. The result then is an array of size 2 with the computed l2 distance between the functions in the same position in both.
>>> import skfda >>> import numpy as np >>> >>> x = np.linspace(0, 1, 1001) >>> fd = skfda.FDataGrid([np.ones(len(x)), x], x) >>> fd2 = skfda.FDataGrid([np.zeros(len(x)), x/2], x) >>> >>> skfda.misc.metrics.lp_distance(fd, fd2, p=2).round(2) array([ 1. , 0.29])
If the functional data are defined over a different set of points of discretisation the functions returns an exception.
>>> x = np.linspace(0, 2, 1001) >>> fd2 = skfda.FDataGrid([np.zeros(len(x)), x/2 + 0.5], x) >>> skfda.misc.metrics.lp_distance(fd, fd2, p=2) Traceback (most recent call last): ... ValueError: ...
See also