FPCA#
- class skfda.preprocessing.dim_reduction.FPCA(n_components=None, *, centering=True, regularization=None, components_basis=None, _weights=None)[source]#
Principal component analysis.
Class that implements functional principal component analysis for both basis and grid representations of the data. The parameters are shared when fitting a FDataBasis or FDataGrid, except for
components_basis
.- Parameters:
n_components (int | None) –
Number of principal components to keep from functional principal component analysis.
Changed in version 0.9: In future versions, it will default to the maximum number of components that can be extracted. Currently, it still defaults to 3 but do not assume this behavior as it will change.
centering (bool) – Set to
False
when the functional data is already known to be centered and there is no need to center it. Otherwise, the mean of the functional data object is calculated and the data centered before fitting . Defaults toTrue
.regularization (L2Regularization[FData] | None) – Regularization object to be applied.
components_basis (Basis | None) – The basis in which we want the principal components. We can use a different basis than the basis contained in the passed FDataBasis object. This parameter is only used when fitting a FDataBasis.
_weights (ArrayLike | WeightsCallable | None) –
- Attributes:
components_ – this contains the principal components.
explained_variance_ – The amount of variance explained by each of the selected components.
explained_variance_ratio_ – this contains the percentage of variance explained by each principal component.
singular_values_ – The singular values corresponding to each of the selected components.
mean_ – mean of the train data.
Examples
Construct an artificial FDataBasis object and run FPCA with this object. The resulting principal components are not compared because there are several equivalent possibilities.
>>> import skfda >>> data_matrix = np.array([[1.0, 0.0], [0.0, 2.0]]) >>> grid_points = [0, 1] >>> fd = FDataGrid(data_matrix, grid_points) >>> basis = skfda.representation.basis.MonomialBasis( ... domain_range=(0,1), n_basis=2 ... ) >>> basis_fd = fd.to_basis(basis) >>> fpca_basis = FPCA(2) >>> fpca_basis = fpca_basis.fit(basis_fd)
In this example we apply discretized functional PCA with some simple data to illustrate the usage of this class. We initialize the FPCA object, fit the artificial data and obtain the scores. The results are not tested because there are several equivalent possibilities.
>>> data_matrix = np.array([[1.0, 0.0], [0.0, 2.0]]) >>> grid_points = [0, 1] >>> fd = FDataGrid(data_matrix, grid_points) >>> fpca_grid = FPCA(2) >>> fpca_grid = fpca_grid.fit(fd)
Methods
fit
(X[, y])Compute the n_components first principal components and saves them.
fit_transform
(X[, y])Compute the n_components first principal components and their scores.
Get metadata routing of this object.
get_params
([deep])Get parameters for this estimator.
inverse_transform
(pc_scores)Compute the recovery from the fitted principal components scores.
set_inverse_transform_request
(*[, pc_scores])Request metadata passed to the
inverse_transform
method.set_output
(*[, transform])Set output container.
set_params
(**params)Set the parameters of this estimator.
transform
(X[, y])Compute the
n_components
first principal components scores.- fit_transform(X, y=None)[source]#
Compute the n_components first principal components and their scores.
- get_metadata_routing()#
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
- Returns:
routing – A
MetadataRequest
encapsulating routing information.- Return type:
MetadataRequest
- get_params(deep=True)#
Get parameters for this estimator.
- inverse_transform(pc_scores)[source]#
Compute the recovery from the fitted principal components scores.
In other words, it maps
pc_scores
, from the fitted functional PCs’ space, back to the input functional space.pc_scores
might be an array returned bytransform
method.
- set_inverse_transform_request(*, pc_scores='$UNCHANGED$')#
Request metadata passed to the
inverse_transform
method.Note that this method is only relevant if
enable_metadata_routing=True
(seesklearn.set_config()
). Please see User Guide on how the routing mechanism works.The options for each parameter are:
True
: metadata is requested, and passed toinverse_transform
if provided. The request is ignored if metadata is not provided.False
: metadata is not requested and the meta-estimator will not pass it toinverse_transform
.None
: metadata is not requested, and the meta-estimator will raise an error if the user provides it.str
: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (
sklearn.utils.metadata_routing.UNCHANGED
) retains the existing request. This allows you to change the request for some parameters and not others.New in version 1.3.
Note
This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside a
Pipeline
. Otherwise it has no effect.
- set_output(*, transform=None)#
Set output container.
See Introducing the set_output API for an example on how to use the API.
- Parameters:
transform ({"default", "pandas"}, default=None) –
Configure output of transform and fit_transform.
”default”: Default output format of a transformer
”pandas”: DataFrame output
”polars”: Polars output
None: Transform configuration is unchanged
New in version 1.4: “polars” option was added.
- Returns:
self – Estimator instance.
- Return type:
estimator instance
- set_params(**params)#
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects (such as
Pipeline
). The latter have parameters of the form<component>__<parameter>
so that it’s possible to update each component of a nested object.- Parameters:
**params (dict) – Estimator parameters.
- Returns:
self – Estimator instance.
- Return type:
estimator instance
Examples using skfda.preprocessing.dim_reduction.FPCA
#
Functional Principal Component Analysis
Meteorological data: data visualization, clustering, and functional PCA