FDataBasis#
- class skfda.representation.basis.FDataBasis(basis, coefficients, *, dataset_name=None, argument_names=None, coordinate_names=None, sample_names=None, extrapolation=None)[source]#
Basis representation of functional data.
Class representation for functional data in the form of a set of basis functions multplied by a set of coefficients.
\[f(x) = \sum_{k=1}{K}c_k\phi_k\]Where n is the number of basis functions, \(c = (c_1, c_2, ..., c_K)\) the vector of coefficients and \(\phi = (\phi_1, \phi_2, ..., \phi_K)\) the basis function system.
- Attributes:
basis – Basis function system.
coefficients – List or matrix of coefficients. Has to have the same length or number of columns as the number of basis function in the basis. If a matrix, each row contains the coefficients that multiplied by the basis functions produce each functional datum.
domain_range – 2 dimension matrix where each row contains the bounds of the interval in which the functional data is considered to exist for each one of the axes.
dataset_name – name of the dataset.
argument_names – tuple containing the names of the different arguments.
coordinate_names – tuple containing the names of the different coordinate functions.
extrapolation – defines the default type of extrapolation. By default None, which does not apply any type of extrapolation. See Extrapolation for detailled information of the types of extrapolation.
- Parameters:
Examples
>>> from skfda.representation.basis import FDataBasis, MonomialBasis >>> >>> basis = MonomialBasis(n_basis=4) >>> coefficients = [1, 1, 3, .5] >>> FDataBasis(basis, coefficients) FDataBasis( basis=MonomialBasis(domain_range=((0.0, 1.0),), n_basis=4), coefficients=[[ 1. 1. 3. 0.5]], ...)
Methods
argmax([skipna])Return the index of maximum value.
argmin([skipna])Return the index of minimum value.
argsort(*[, ascending, kind, na_position])Return the indices that would sort this array.
astype(dtype[, copy])Cast to a new dtype.
compose(fd, *[, eval_points])Composition of functions.
concatenate(*others[, as_coordinates])Join samples from a similar FDataBasis object.
copy(*[, deep, basis, coefficients, ...])Copy the FDataBasis.
count()Count the number of non-NA values in the array.
cov(-> ~numpy.ndarray[tuple[~typing.Any, ...)Compute the covariance of the functional data object.
delete(loc)derivative(*[, order])Differentiate a FData object.
dropna()Return ExtensionArray without NA values.
duplicated([keep])Return boolean ndarray denoting duplicate values.
equals(other)Equality of FDataBasis.
evaluate(eval_points, *[, derivative, ...])Evaluate the object at a list of values or a grid.
factorize([use_na_sentinel])Encode the extension array as an enumerated type.
fillna(value[, limit, copy])Fill NA/NaN values using the specified method.
from_data(data_matrix, *, basis[, ...])Transform raw data to a smooth functional form.
insert(loc, item)Insert an item at the given position.
integrate(*[, domain])Integration of the FData object.
interpolate(*, method, axis, index, limit, ...)Fill NaN values using an interpolation method.
isin(values)Pointwise comparison for set containment in the given values.
isna()Return a 1-D array indicating if each value is missing.
item([index])Return the array element at the specified position as a Python scalar.
map(mapper[, na_action])Map values using an input mapping or function.
mean(*[, axis, dtype, out, keepdims, skipna])Compute the mean of all the samples.
plot(*args, **kwargs)Plot the FDatGrid object.
ravel([order])Return a flattened view on this array.
repeat(repeats[, axis])Repeat elements of an ExtensionArray.
round([decimals])Round each value in the array to the given number of decimals.
searchsorted(value[, side, sorter])Find indices where elements should be inserted to maintain order.
shift(shifts, *[, restrict_domain, ...])Perform a shift of the curves.
sort(*[, ascending, kind, na_position])Sort the array in-place.
sum(*[, axis, out, keepdims, skipna, min_count])Compute the sum of all the samples in a FDataBasis object.
take(indices[, allow_fill, fill_value, axis])Take elements from an array.
to_basis([basis, eval_points])Return the basis representation of the object.
to_grid([grid_points, sample_points])Return the discrete representation of the object.
to_numpy([dtype, copy, na_value])Convert to a NumPy ndarray.
tolist()Return a list of the values.
transpose(*axes)Return a transposed view on this array.
unique()Compute the ExtensionArray of unique values.
value_counts([dropna])Return a Series containing counts of unique values.
var([eval_points, correction])Compute the variance of the functional data object.
view([dtype])Return a view on the array.
- argmax(skipna=True)#
Return the index of maximum value.
In case of multiple occurrences of the maximum value, the index corresponding to the first occurrence is returned.
See also
ExtensionArray.argminReturn the index of the minimum value.
Examples
>>> arr = pd.array([3, 1, 2, 5, 4]) >>> arr.argmax() np.int64(3)
- argmin(skipna=True)#
Return the index of minimum value.
In case of multiple occurrences of the minimum value, the index corresponding to the first occurrence is returned.
See also
ExtensionArray.argmaxReturn the index of the maximum value.
Examples
>>> arr = pd.array([3, 1, 2, 5, 4]) >>> arr.argmin() np.int64(1)
- argsort(*, ascending=True, kind='quicksort', na_position='last', **kwargs)#
Return the indices that would sort this array.
This method computes the indices that would produce a sorted version of the array when used with indexing.
- Parameters:
ascending (bool, default True) – Whether the indices should result in an ascending or descending sort.
kind ({'quicksort', 'mergesort', 'heapsort', 'stable'}, optional) – Sorting algorithm.
na_position ({'first', 'last'}, default 'last') – If
'first', putNaNvalues at the beginning. If'last', putNaNvalues at the end.**kwargs – Passed through to
numpy.argsort().
- Returns:
Array of indices that sort
self. If NaN values are contained, NaN values are placed at the end.- Return type:
np.ndarray[np.intp]
See also
numpy.argsortSorting implementation used internally.
Examples
>>> arr = pd.array([3, 1, 2, 5, 4]) >>> arr.argsort() array([1, 2, 0, 4, 3])
- compose(fd, *, eval_points=None, **kwargs)[source]#
Composition of functions.
Performs the composition of functions. The basis is discretized to compute the composition.
- Parameters:
- Returns:
Function resulting from the composition.
- Return type:
- concatenate(*others, as_coordinates=False)[source]#
Join samples from a similar FDataBasis object.
Joins samples from another FDataBasis object if they have the same basis.
- Parameters:
others (T) – Objects to be concatenated.
as_coordinates (bool) – If False concatenates as new samples, else, concatenates the other functions as new components of the image. Defaults to False.
self (T)
- Returns:
FDataBasis object with the samples from the original objects.
- Return type:
Todo
By the moment, only unidimensional objects are supported in basis representation.
- copy(*, deep=False, basis=None, coefficients=None, dataset_name=None, argument_names=None, coordinate_names=None, sample_names=None, extrapolation=None)[source]#
Copy the FDataBasis.
- Parameters:
- Return type:
T
- count()#
Count the number of non-NA values in the array.
This method returns the number of elements in the
ExtensionArraythat are not missing (i.e., not NA/null).- Returns:
Number of non-NA values in the Series.
- Return type:
scalar
See also
Series.countCount the number of non-NA values in a Series.
Examples
>>> s = pd.array([1, pd.NA, 3]) >>> s.count() np.int64(2)
- cov(s_points: ndarray[tuple[Any, ...], dtype[floating[Any]]], t_points: ndarray[tuple[Any, ...], dtype[floating[Any]]], /, correction: int = 0) ndarray[tuple[Any, ...], dtype[floating[Any]]][source]#
- cov(correction: int = 0) Callable[[ndarray[tuple[Any, ...], dtype[floating[Any]]], ndarray[tuple[Any, ...], dtype[floating[Any]]]], ndarray[tuple[Any, ...], dtype[floating[Any]]]]
Compute the covariance of the functional data object.
Calculates the unbiased sample covariance function of the data. This is expected to be only defined for univariate functions. This is a function defined over the basis consisting of the tensor product of the original basis with itself. The resulting covariance function is then represented as a callable object.
If s_points or t_points are not provided, this method returns a callable object representing the covariance function. If s_points and t_points are provided, this method returns the evaluation of the covariance function at the grid formed by the cartesian product of the points in s_points and t_points.
- Parameters:
s_points (ndarray[tuple[Any, ...], dtype[floating[Any]]] | None) – Points where the covariance function is evaluated.
t_points (ndarray[tuple[Any, ...], dtype[floating[Any]]] | None) – Points where the covariance function is evaluated.
correction (int) – degrees of freedom adjustment. The divisor used in the calculation is N - correction, where N represents the number of elements. Default: 0.
self (T)
- Returns:
Covariance function.
- Return type:
Callable[[ndarray[tuple[Any, …], dtype[floating[Any]]], ndarray[tuple[Any, …], dtype[floating[Any]]]], ndarray[tuple[Any, …], dtype[floating[Any]]]] | ndarray[tuple[Any, …], dtype[floating[Any]]]
- derivative(*, order=1)[source]#
Differentiate a FData object.
- Parameters:
order (int) – Order of the derivative. Defaults to one.
self (T)
- Returns:
Functional object containg the derivative.
- Return type:
T
- dropna()#
Return ExtensionArray without NA values.
This method removes all missing values from the array, returning a new ExtensionArray containing only non-NA values.
- Returns:
An ExtensionArray of the same type as the original but with all NA values removed.
- Return type:
Self
See also
Series.dropnaRemove missing values from a Series.
DataFrame.dropnaRemove missing values from a DataFrame.
api.extensions.ExtensionArray.isnaCheck for missing values in an ExtensionArray.
Examples
>>> pd.array([1, 2, np.nan]).dropna() <IntegerArray> [1, 2] Length: 2, dtype: Int64
- duplicated(keep='first')#
Return boolean ndarray denoting duplicate values.
This method identifies duplicate values in the array. The keep parameter controls which occurrences are marked as duplicates.
- Parameters:
keep ({'first', 'last', False}, default 'first') –
first: Mark duplicates asTrueexcept for the first occurrence.last: Mark duplicates asTrueexcept for the last occurrence.False : Mark all duplicates as
True.
- Returns:
With true in indices where elements are duplicated and false otherwise.
- Return type:
ndarray[bool]
See also
DataFrame.duplicatedReturn boolean Series denoting duplicate rows.
Series.duplicatedIndicate duplicate Series values.
api.extensions.ExtensionArray.uniqueCompute the ExtensionArray of unique values.
Examples
>>> pd.array([1, 1, 2, 3, 3], dtype="Int64").duplicated() array([False, True, False, False, True])
- evaluate(eval_points, *, derivative=0, extrapolation=None, grid=False, aligned=True)[source]#
Evaluate the object at a list of values or a grid.
Deprecated since version 0.8: Use normal calling notation instead.
- Parameters:
eval_points (_Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str] | Sequence[_Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str]] | Iterable[_Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str] | Sequence[_Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str]]]) – List of points where the functions are evaluated. If
gridisTrue, a list of axes, one per domain dimension, must be passed instead. IfalignedisTrue, then a list of lists (of points or axes, as explained) must be passed, with one list per sample.derivative (int) – Deprecated. Order of the derivative to evaluate.
extrapolation (Evaluator | Literal['bounds', 'exception', 'nan', 'none', 'periodic', 'zeros'] | None) – Controls the extrapolation mode for elements outside the domain range. By default it is used the mode defined during the instance of the object.
grid (bool) – Whether to evaluate the results on a grid spanned by the input arrays, or at points specified by the input arrays. If true the eval_points should be a list of size dim_domain with the corresponding times for each axis. The return matrix has shape n_samples x len(t1) x len(t2) x … x len(t_dim_domain) x dim_codomain. If the domain dimension is 1 the parameter has no efect. Defaults to False.
aligned (bool) – Whether the input points are the same for each sample, or an array of points per sample is passed.
- Returns:
Matrix whose rows are the values of the each function at the values specified in eval_points.
- Return type:
- factorize(use_na_sentinel=True)#
Encode the extension array as an enumerated type.
This method encodes the array values as integer codes that index into an array of unique values. Missing values can be encoded with a sentinel value of -1.
- Parameters:
use_na_sentinel (bool, default True) – If True, the sentinel -1 will be used for NaN values. If False, NaN values will be encoded as non-negative integers and will not drop the NaN from the uniques of the values.
- Returns:
codes (ndarray) – An integer NumPy array that’s an indexer into the original ExtensionArray.
uniques (ExtensionArray) – An ExtensionArray containing the unique values of self.
Note
uniques will not contain an entry for the NA value of the ExtensionArray if there are any missing values present in self.
- Return type:
See also
factorizeTop-level factorize method that dispatches here.
Notes
pandas.factorize()offers a sort keyword as well.Examples
>>> idx1 = pd.PeriodIndex( ... ["2014-01", "2014-01", "2014-02", "2014-02", "2014-03", "2014-03"], ... freq="M", ... ) >>> arr, idx = idx1.factorize() >>> arr array([0, 0, 1, 1, 2, 2]) >>> idx PeriodIndex(['2014-01', '2014-02', '2014-03'], dtype='period[M]')
- fillna(value, limit=None, copy=True)#
Fill NA/NaN values using the specified method.
This method replaces missing values in the array with a scalar value or corresponding values from an array-like object.
- Parameters:
value (scalar, array-like) – If a scalar value is passed it is used to fill all missing values. Alternatively, an array-like “value” can be given. It’s expected that the array-like have the same length as ‘self’.
limit (int, default None) – The maximum number of entries where NA values will be filled.
copy (bool, default True) – Whether to make a copy of the data before filling. If False, then the original should be modified and no new memory should be allocated. For ExtensionArray subclasses that cannot do this, it is at the author’s discretion whether to ignore “copy=False” or to raise.
- Returns:
With NA/NaN filled.
- Return type:
ExtensionArray
See also
api.extensions.ExtensionArray.dropnaReturn ExtensionArray without NA values.
api.extensions.ExtensionArray.isnaA 1-D array indicating if each value is missing.
Examples
>>> arr = pd.array([np.nan, np.nan, 2, 3, np.nan, np.nan]) >>> arr.fillna(0) <IntegerArray> [0, 0, 2, 3, 0, 0] Length: 6, dtype: Int64
- classmethod from_data(data_matrix, *, basis, grid_points=None, sample_points=None, method='cholesky')[source]#
Transform raw data to a smooth functional form.
Takes functional data in a discrete form and makes an approximates it to the closest function that can be generated by the basis. This function does not attempt to smooth the original data. If smoothing is desired, it is better to use
BasisSmoother.The fit is made so as to reduce the sum of squared errors [RS05-5-2-5]:
\[SSE(c) = (y - \Phi c)' (y - \Phi c)\]where \(y\) is the vector or matrix of observations, \(\Phi\) the matrix whose columns are the basis functions evaluated at the sampling points and \(c\) the coefficient vector or matrix to be estimated.
By deriving the first formula we obtain the closed formed of the estimated coefficients matrix:
\[\hat{c} = \left( \Phi' \Phi \right)^{-1} \Phi' y\]The solution of this matrix equation is done using the cholesky method for the resolution of a LS problem. If this method throughs a rounding error warning you may want to use the QR factorisation that is more numerically stable despite being more expensive to compute. [RS05-5-2-7]
- Parameters:
data_matrix (Union[NDArrayFloat, NDArrayInt]) – List or matrix containing the observations. If a matrix each row represents a single functional datum and the columns the different observations.
grid_points (Optional[GridPointsLike]) – Values of the domain where the previous data were taken.
basis (Basis) – Basis used.
method (str) – Algorithm used for calculating the coefficients using the least squares method. The values admitted are ‘cholesky’ and ‘qr’ for Cholesky and QR factorisation methods respectively.
sample_points (Optional[GridPointsLike]) –
Old name for grid_points. New code should use grid_points instead.
Deprecated since version 0.5.
- Returns:
- Represention of the data in a functional form as
product of coefficients by basis functions.
- Return type:
Examples
>>> import numpy as np >>> t = np.linspace(0, 1, 5) >>> x = np.sin(2 * np.pi * t) + np.cos(2 * np.pi * t) + 2 >>> x array([ 3., 3., 1., 1., 3.])
>>> from skfda.representation.basis import FDataBasis, FourierBasis >>> basis = FourierBasis((0, 1), n_basis=3) >>> fd = FDataBasis.from_data(x, grid_points=t, basis=basis) >>> fd.coefficients.round(2) array([[ 2. , 0.71, 0.71]])
References
[RS05-5-2-5]Ramsay, J., Silverman, B. W. (2005). How spline smooths are computed. In Functional Data Analysis (pp. 86-87). Springer.
[RS05-5-2-7]Ramsay, J., Silverman, B. W. (2005). HSpline smoothing as an augmented least squares problem. In Functional Data Analysis (pp. 86-87). Springer.
- insert(loc, item)#
Insert an item at the given position.
This method creates a new ExtensionArray with the specified item inserted at the given index. The original array is not modified.
- Parameters:
loc (int) – Index where the item needs to be inserted.
item (scalar-like) – Value to be inserted.
- Returns:
With item inserted at loc.
- Return type:
ExtensionArray
See also
Index.insertMake new Index inserting new item at location.
Notes
This method should be both type and dtype-preserving. If the item cannot be held in an array of this type/dtype, either ValueError or TypeError should be raised.
The default implementation relies on _from_sequence to raise on invalid items.
Examples
>>> arr = pd.array([1, 2, 3]) >>> arr.insert(2, -1) <IntegerArray> [1, 2, -1, 3] Length: 4, dtype: Int64
- integrate(*, domain=None)[source]#
Integration of the FData object.
The integration is performed over the whole domain. Thus, for a function of several variables this will be a multiple integral.
For a vector valued function the vector of integrals will be returned.
- Parameters:
- Returns:
NumPy array of size (
n_samples,dim_codomain) with the integrated data.- Return type:
Examples
- We first create the data basis.
>>> from skfda.representation.basis import FDataBasis >>> from skfda.representation.basis import MonomialBasis >>> basis = MonomialBasis(n_basis=4) >>> coefficients = [1, 1, 3, .5] >>> fdata = FDataBasis(basis, coefficients)
- Then we can integrate on the whole domain.
>>> fdata.integrate() array([[ 2.625]])
- Or we can do it on a given domain.
>>> fdata.integrate(domain=((0.5, 1),)) array([[ 1.8671875]])
- interpolate(*, method, axis, index, limit, limit_direction, limit_area, copy, **kwargs)#
Fill NaN values using an interpolation method.
This method fills missing values in the array by interpolating between existing values using the specified interpolation technique.
- Parameters:
method (str, default 'linear') – Interpolation technique to use. One of: * ‘linear’: Ignore the index and treat the values as equally spaced. This is the only method supported on MultiIndexes. * ‘time’: Works on daily and higher resolution data to interpolate given length of interval. * ‘index’, ‘values’: use the actual numerical values of the index. * ‘nearest’, ‘zero’, ‘slinear’, ‘quadratic’, ‘cubic’, ‘barycentric’, ‘polynomial’: Passed to scipy.interpolate.interp1d, whereas ‘spline’ is passed to scipy.interpolate.UnivariateSpline. These methods use the numerical values of the index. Both ‘polynomial’ and ‘spline’ require that you also specify an order (int), e.g. arr.interpolate(method=’polynomial’, order=5). * ‘krogh’, ‘piecewise_polynomial’, ‘spline’, ‘pchip’, ‘akima’, ‘cubicspline’: Wrappers around the SciPy interpolation methods of similar names. See Notes. * ‘from_derivatives’: Refers to scipy.interpolate.BPoly.from_derivatives.
axis (int) – Axis to interpolate along. For 1-dimensional data, use 0.
index (Index) – Index to use for interpolation.
limit (int or None) – Maximum number of consecutive NaNs to fill. Must be greater than 0.
limit_direction ({'forward', 'backward', 'both'}) – Consecutive NaNs will be filled in this direction.
limit_area ({'inside', 'outside'} or None) – If limit is specified, consecutive NaNs will be filled with this restriction. * None: No fill restriction. * ‘inside’: Only fill NaNs surrounded by valid values (interpolate). * ‘outside’: Only fill NaNs outside valid values (extrapolate).
copy (bool) – If True, a copy of the object is returned with interpolated values.
**kwargs (optional) – Keyword arguments to pass on to the interpolating function.
- Returns:
An ExtensionArray with interpolated values.
- Return type:
ExtensionArray
See also
Series.interpolateInterpolate values in a Series.
DataFrame.interpolateInterpolate values in a DataFrame.
Notes
All parameters must be specified as keyword arguments.
The ‘krogh’, ‘piecewise_polynomial’, ‘spline’, ‘pchip’ and ‘akima’ methods are wrappers around the respective SciPy implementations of similar names. These use the actual numerical values of the index.
Examples
Interpolating values in a NumPy array:
>>> arr = pd.arrays.NumpyExtensionArray(np.array([0, 1, np.nan, 3])) >>> arr.interpolate( ... method="linear", ... limit=3, ... limit_direction="forward", ... index=pd.Index(range(len(arr))), ... fill_value=1, ... copy=False, ... axis=0, ... limit_area="inside", ... ) <NumpyExtensionArray> [0.0, 1.0, 2.0, 3.0] Length: 4, dtype: float64
Interpolating values in a FloatingArray:
>>> arr = pd.array([1.0, pd.NA, 3.0, 4.0, pd.NA, 6.0], dtype="Float64") >>> arr.interpolate( ... method="linear", ... axis=0, ... index=pd.Index(range(len(arr))), ... limit=None, ... limit_direction="both", ... limit_area=None, ... copy=True, ... ) <FloatingArray> [1.0, 2.0, 3.0, 4.0, 5.0, 6.0] Length: 6, dtype: Float64
- isin(values)#
Pointwise comparison for set containment in the given values.
Roughly equivalent to np.array([x in values for x in self])
- Parameters:
values (np.ndarray or ExtensionArray) – Values to compare every element in the array against.
- Returns:
With true at indices where value is in values.
- Return type:
np.ndarray[bool]
See also
DataFrame.isinWhether each element in the DataFrame is contained in values.
Index.isinReturn a boolean array where the index values are in values.
Series.isinWhether elements in Series are contained in values.
Examples
>>> arr = pd.array([1, 2, 3]) >>> arr.isin([1]) <BooleanArray> [True, False, False] Length: 3, dtype: boolean
- isna()[source]#
Return a 1-D array indicating if each value is missing.
- Returns:
Positions of NA.
- Return type:
na_values (np.ndarray)
- item(index=None)#
Return the array element at the specified position as a Python scalar.
Analogous to
numpy.ndarray.item(), this converts a single element to a native Python object.- Parameters:
index (int, optional) – Position of the element. If not provided, the array must contain exactly one element.
- Returns:
The element at the specified position.
- Return type:
scalar
- Raises:
ValueError – If no index is provided and the array does not have exactly one element.
IndexError – If the specified position is out of bounds.
See also
numpy.ndarray.itemReturn the item of an array as a scalar.
Examples
>>> arr = pd.array([1], dtype="Int64") >>> arr.item() np.int64(1)
>>> arr = pd.array([1, 2, 3], dtype="Int64") >>> arr.item(0) np.int64(1) >>> arr.item(2) np.int64(3)
- map(mapper, na_action=None)#
Map values using an input mapping or function.
- Parameters:
mapper (function, dict, or Series) – Mapping correspondence.
na_action ({None, 'ignore'}, default None) – If ‘ignore’, propagate NA values, without passing them to the mapping correspondence. If ‘ignore’ is not supported, a
NotImplementedErrorshould be raised.
- Returns:
The output of the mapping function applied to the array. If the function returns a tuple with more than one element a MultiIndex will be returned.
- Return type:
Union[ndarray, Index, ExtensionArray]
- mean(*, axis=None, dtype=None, out=None, keepdims=False, skipna=False)[source]#
Compute the mean of all the samples.
- Parameters:
axis (int | None) – Used for compatibility with numpy. Must be None or 0.
dtype (None) – Used for compatibility with numpy. Must be None.
out (None) – Used for compatibility with numpy. Must be None.
keepdims (bool) – Used for compatibility with numpy. Must be False.
skipna (bool) – Wether the NaNs are ignored or not.
self (T)
- Returns:
A FData object with just one sample representing the mean of all the samples in the original object.
- Return type:
T
- ravel(order='C')#
Return a flattened view on this array.
Since ExtensionArrays are 1-dimensional, this method returns the array itself. The
orderparameter is accepted for compatibility with NumPy but is ignored.- Parameters:
order ({None, 'C', 'F', 'A', 'K'}, default 'C')
- Returns:
A flattened view on the array.
- Return type:
ExtensionArray
See also
ExtensionArray.tolistReturn a list of the values.
Notes
Because ExtensionArrays are 1D-only, this is a no-op.
The “order” argument is ignored, is for compatibility with NumPy.
Examples
>>> pd.array([1, 2, 3]).ravel() <IntegerArray> [1, 2, 3] Length: 3, dtype: Int64
- repeat(repeats, axis=None)#
Repeat elements of an ExtensionArray.
Returns a new ExtensionArray where each element of the current ExtensionArray is repeated consecutively a given number of times.
- Parameters:
repeats (int or array of ints) – The number of repetitions for each element. This should be a non-negative integer. Repeating 0 times will return an empty ExtensionArray.
axis (None) – Must be
None. Has no effect but is accepted for compatibility with numpy.
- Returns:
Newly created ExtensionArray with repeated elements.
- Return type:
ExtensionArray
See also
Series.repeatEquivalent function for Series.
Index.repeatEquivalent function for Index.
numpy.repeatSimilar method for
numpy.ndarray.ExtensionArray.takeTake arbitrary positions.
Examples
>>> cat = pd.Categorical(["a", "b", "c"]) >>> cat ['a', 'b', 'c'] Categories (3, str): ['a', 'b', 'c'] >>> cat.repeat(2) ['a', 'a', 'b', 'b', 'c', 'c'] Categories (3, str): ['a', 'b', 'c'] >>> cat.repeat([1, 2, 3]) ['a', 'b', 'b', 'c', 'c', 'c'] Categories (3, str): ['a', 'b', 'c']
- round(decimals=0)#
Round each value in the array to the given number of decimals.
- Parameters:
decimals (int, default 0) – Number of decimal places to round to. If decimals is negative, it specifies the number of positions to the left of the decimal point.
- Returns:
Rounded values of the ExtensionArray.
- Return type:
ExtensionArray
See also
DataFrame.roundRound values of a DataFrame.
Series.roundRound values of a Series.
Notes
This is a non-performant default implementation. Subclasses are encouraged to override it to avoid the elementwise loop.
Examples
>>> arr = pd.array([1.234, 5.678, pd.NA], dtype="Float64") >>> arr.round(1) <FloatingArray> [1.2, 5.7, <NA>] Length: 3, dtype: Float64
- searchsorted(value, side='left', sorter=None)#
Find indices where elements should be inserted to maintain order.
Find the indices into a sorted array self (a) such that, if the corresponding elements in value were inserted before the indices, the order of self would be preserved.
Assuming that self is sorted:
side
returned index i satisfies
left
self[i-1] < value <= self[i]right
self[i-1] <= value < self[i]- Parameters:
value (array-like, list or scalar) – Value(s) to insert into self.
side ({'left', 'right'}, optional) – If ‘left’, the index of the first suitable location found is given. If ‘right’, return the last such index. If there is no suitable index, return either 0 or N (where N is the length of self).
sorter (1-D array-like, optional) – Optional array of integer indices that sort array a into ascending order. They are typically the result of argsort.
- Returns:
If value is array-like, array of insertion points. If value is scalar, a single integer.
- Return type:
array of ints or int
See also
numpy.searchsortedSimilar method from NumPy.
Examples
>>> arr = pd.array([1, 2, 3, 5]) >>> arr.searchsorted([4]) array([3])
- shift(shifts, *, restrict_domain=False, extrapolation=None, grid_points=None)[source]#
Perform a shift of the curves.
The i-th shifted function \(y_i\) has the form
\[y_i(t) = x_i(t + \delta_i)\]where \(x_i\) is the i-th original function and \(delta_i\) is the shift performed for that function, that must be a vector in the domain space.
Note that a positive shift moves the graph of the function in the negative direction and vice versa.
- Parameters:
shifts (Union[ArrayLike, float]) – List with the shifts corresponding for each sample or numeric with the shift to apply to all samples.
restrict_domain (bool) – If True restricts the domain to avoid the evaluation of points outside the domain using extrapolation. Defaults uses extrapolation.
extrapolation (Optional[ExtrapolationLike]) – Controls the extrapolation mode for elements outside the domain range. By default uses the method defined in fd. See extrapolation to more information.
grid_points (Optional[GridPointsLike]) – Grid of points where the functions are evaluated to obtain the discrete representation of the object to operate. If
Nonethe current grid_points are used to unificate the domain of the shifted data.
- Returns:
Shifted functions.
- Return type:
- sort(*, ascending=True, kind='quicksort', na_position='last')#
Sort the array in-place.
Reorders the elements of the array using
argsort()and writes the sorted result back throughself[:]. Subclasses backed by immutable storage may override this method to raiseNotImplementedError.- Parameters:
ascending (bool, default True) – Whether to sort in ascending order.
kind ({'quicksort', 'mergesort', 'heapsort', 'stable'}, default 'quicksort') – Sorting algorithm.
na_position ({'first', 'last'}, default 'last') – If ‘first’, put NaN values at the beginning. If ‘last’, put NaN values at the end.
- Returns:
This method mutates the array in place and does not return a value.
- Return type:
None
See also
ExtensionArray.argsortReturn the indices that would sort this array.
Examples
>>> arr = pd.array([3, 1, 2, 5, 4]) >>> arr.sort() >>> arr <IntegerArray> [1, 2, 3, 4, 5] Length: 5, dtype: Int64
- sum(*, axis=None, out=None, keepdims=False, skipna=False, min_count=0)[source]#
Compute the sum of all the samples in a FDataBasis object.
- Parameters:
axis (int | None) – Used for compatibility with numpy. Must be None or 0.
out (None) – Used for compatibility with numpy. Must be None.
keepdims (bool) – Used for compatibility with numpy. Must be False.
skipna (bool) – Wether the NaNs are ignored or not.
min_count (int) – Number of valid (non NaN) data to have in order for the a variable to not be NaN when skipna is True.
self (T)
- Returns:
A FDataBais object with just one sample representing the sum of all the samples in the original FDataBasis object.
- Return type:
T
Examples
>>> from skfda.representation.basis import ( ... FDataBasis, ... MonomialBasis, ... ) >>> basis = MonomialBasis(n_basis=4) >>> coefficients = [[0.5, 1, 2, .5], [1.5, 1, 4, .5]] >>> FDataBasis(basis, coefficients).sum() FDataBasis( basis=MonomialBasis(domain_range=((0.0, 1.0),), n_basis=4), coefficients=[[ 2. 2. 6. 1.]], ...)
- take(indices, allow_fill=False, fill_value=None, axis=0)[source]#
Take elements from an array.
- Parameters:
indices (int | Sequence[int] | ndarray[tuple[Any, ...], dtype[integer[Any]]]) – Indices to be taken.
allow_fill (bool) –
How to handle negative values in indices.
False: negative values in indices indicate positional indices from the right (the default). This is similar to
numpy.take().True: negative values in indices indicate missing values. These values are set to fill_value. Any other negative values raise a
ValueError.
fill_value (T | None) – Fill value to use for NA-indices when allow_fill is True. This may be
None, in which case the default NA value for the type,self.dtype.na_value, is used. For many ExtensionArrays, there will be two representations of fill_value: a user-facing “boxed” scalar, and a low-level physical NA value. fill_value should be the user-facing version, and the implementation should handle translating that to the physical version for processing the take if necessary.axis (int) – Parameter for compatibility with numpy. Must be always 0.
self (T)
- Returns:
FData
- Raises:
IndexError – When the indices are out of bounds for the array.
ValueError – When indices contains negative values other than
-1and allow_fill is True.
- Return type:
T
Notes
ExtensionArray.take is called by
Series.__getitem__,.loc,iloc, when indices is a sequence of values. Additionally, it’s called bySeries.reindex(), or any other method that causes realignment, with a fill_value.See also
numpy.take pandas.api.extensions.take
- to_basis(basis=None, eval_points=None, **kwargs)[source]#
Return the basis representation of the object.
- Parameters:
basis (Optional[Basis]) – Basis object in which the functional data are going to be represented.
eval_points (Optional[NDArrayFloat]) – Evaluation points used to discretize the function if the basis is going to be changed.
kwargs (Any) – Keyword arguments to be passed to FDataBasis.from_data().
- Returns:
Basis representation of the funtional data object.
- Return type:
- to_grid(grid_points=None, *, sample_points=None)[source]#
Return the discrete representation of the object.
- Parameters:
grid_points (array_like, optional) – Points per axis where the functions are evaluated. If none are passed it calls numpy.linspace with bounds equal to the ones defined in self.domain_range and the number of points the maximum between 501 and 10 times the number of basis.
sample_points (Optional[GridPointsLike]) –
Old name for grid_points. New code should use grid_points instead.
Deprecated since version 0.5.
- Returns:
Discrete representation of the functional data object.
- Return type:
Examples
>>> from skfda.representation.basis import( ... FDataBasis, ... MonomialBasis, ... ) >>> fd = FDataBasis( ... coefficients=[[1, 1, 1], [1, 0, 1]], ... basis=MonomialBasis(domain_range=(0,5), n_basis=3), ... ) >>> fd.to_grid([0, 1, 2]) FDataGrid( array([[[ 1.], [ 3.], [ 7.]], [[ 1.], [ 2.], [ 5.]]]), grid_points=(array([ 0., 1., 2.]),), domain_range=((0.0, 5.0),), ...)
- to_numpy(dtype=None, copy=False, na_value=<no_default>)#
Convert to a NumPy ndarray.
This is similar to
numpy.asarray(), but may provide additional control over how the conversion is done.- Parameters:
dtype (str or numpy.dtype, optional) – The dtype to pass to
numpy.asarray().copy (bool, default False) – Whether to ensure that the returned value is a not a view on another array. Note that
copy=Falsedoes not ensure thatto_numpy()is no-copy. Rather,copy=Trueensure that a copy is made, even if not strictly necessary.na_value (Any, optional) – The value to use for missing values. The default value depends on dtype and the type of the array.
- Return type:
- tolist()#
Return a list of the values.
These are each a scalar type, which is a Python scalar (for str, int, float) or a pandas scalar (for Timestamp/Timedelta/Interval/Period)
- Returns:
Python list of values in array.
- Return type:
See also
Index.to_listReturn a list of the values in the Index.
Series.to_listReturn a list of the values in the Series.
Examples
>>> arr = pd.array([1, 2, 3]) >>> arr.tolist() [1, 2, 3]
- transpose(*axes)#
Return a transposed view on this array.
Because ExtensionArrays are always 1D, this is a no-op. It is included for compatibility with np.ndarray.
- Return type:
ExtensionArray
- Parameters:
axes (int)
Examples
>>> pd.array([1, 2, 3]).transpose() <IntegerArray> [1, 2, 3] Length: 3, dtype: Int64
- unique()#
Compute the ExtensionArray of unique values.
This method returns a new ExtensionArray containing the distinct values from the original array, preserving the order of first appearance.
- Returns:
With unique values from the input array.
- Return type:
See also
Index.uniqueReturn unique values in the index.
Series.uniqueReturn unique values of Series object.
uniqueReturn unique values based on a hash table.
Examples
>>> arr = pd.array([1, 2, 3, 1, 2, 3]) >>> arr.unique() <IntegerArray> [1, 2, 3] Length: 3, dtype: Int64
- value_counts(dropna=True)#
Return a Series containing counts of unique values.
This method returns a Series with unique values as the index and their counts as the values.
- Parameters:
dropna (bool, default True) – Don’t include counts of NA values.
- Returns:
A Series with unique values as index and counts as values.
- Return type:
Series
See also
Series.value_countsEquivalent method on Series.
DataFrame.value_countsEquivalent method on DataFrame.
Index.value_countsEquivalent method on Index.
Examples
>>> from pandas.core.arrays import IntegerArray >>> arr = IntegerArray._from_sequence([3, 3, 3, 1, 2, 2]) >>> arr.value_counts() 3 3 1 1 2 2 Name: count, dtype: Int64
- var(eval_points=None, correction=0)[source]#
Compute the variance of the functional data object.
A numerical approach its used. The object its transformed into its discrete representation and then the variance is computed and then the object is taken back to the basis representation.
- Parameters:
eval_points (ndarray[tuple[Any, ...], dtype[floating[Any]]] | None) – Set of points where the functions are evaluated to obtain the discrete representation of the object. If none are passed it calls numpy.linspace with bounds equal to the ones defined in self.domain_range and the number of points the maximum between 501 and 10 times the number of basis.
correction (int) – degrees of freedom adjustment. The divisor used in the calculation is N - correction, where N represents the number of elements. Default: 0.
self (T)
- Returns:
Variance of the original object.
- Return type:
T
- view(dtype=None)#
Return a view on the array.
This method returns a new object referencing the same underlying data, without copying. Modifications to either the view or the original array will be reflected in both.
- Parameters:
dtype (str, np.dtype, or ExtensionDtype, optional) – Default None.
- Returns:
A view on the
ExtensionArray’s data.- Return type:
ExtensionArray or np.ndarray
See also
api.extensions.ExtensionArray.ravelReturn a flattened view on input array.
Index.viewEquivalent function for Index.
ndarray.viewNew view of array with the same data.
Examples
This gives view on the underlying data of an
ExtensionArrayand is not a copy. Modifications on either the view or the originalExtensionArraywill be reflected on the underlying data:>>> arr = pd.array([1, 2, 3]) >>> arr2 = arr.view() >>> arr[0] = 2 >>> arr2 <IntegerArray> [2, 2, 3] Length: 3, dtype: Int64