Class: PCA
Principal component analysis (PCA).
Linear dimensionality reduction using Singular Value Decomposition of the data to project it to a lower dimensional space. The input data is centered but not scaled for each feature before applying the SVD.
It uses the LAPACK implementation of the full SVD or a randomized truncated SVD by the method of Halko et al. 2009, depending on the shape of the input data and the number of components to extract.
With sparse inputs, the ARPACK implementation of the truncated SVD can be used (i.e. through scipy.sparse.linalg.svds
). Alternatively, one may consider TruncatedSVD
where the data are not centered.
Notice that this class only supports sparse inputs for some solvers such as “arpack” and “covariance_eigh”. See TruncatedSVD
for an alternative with sparse data.
For a usage example, see PCA example with Iris Data-set
Read more in the User Guide.
Constructors
new PCA()
new PCA(
opts
?):PCA
Parameters
Parameter | Type | Description |
---|---|---|
opts ? | object | - |
opts.copy ? | boolean | If false , data passed to fit are overwritten and running fit(X).transform(X) will not yield the expected results, use fit_transform(X) instead. |
opts.iterated_power ? | number | "auto" | Number of iterations for the power method computed by svd_solver == ‘randomized’. Must be of range [0, infinity). |
opts.n_components ? | number | "mle" | Number of components to keep. if n_components is not set all components are kept: |
opts.n_oversamples ? | number | This parameter is only relevant when svd_solver="randomized" . It corresponds to the additional number of random vectors to sample the range of X so as to ensure proper conditioning. See randomized_svd for more details. |
opts.power_iteration_normalizer ? | "auto" | "QR" | "LU" | "none" | Power iteration normalizer for randomized SVD solver. Not used by ARPACK. See randomized_svd for more details. |
opts.random_state ? | number | Used when the ‘arpack’ or ‘randomized’ solvers are used. Pass an int for reproducible results across multiple function calls. See Glossary. |
opts.svd_solver ? | "auto" | "randomized" | "arpack" | "full" | "covariance_eigh" | The solver is selected by a default ‘auto’ policy is based on X.shape and n_components : if the input data has fewer than 1000 features and more than 10 times as many samples, then the “covariance_eigh” solver is used. Otherwise, if the input data is larger than 500x500 and the number of components to extract is lower than 80% of the smallest dimension of the data, then the more efficient “randomized” method is selected. Otherwise the exact “full” SVD is computed and optionally truncated afterwards. |
opts.tol ? | number | Tolerance for singular values computed by svd_solver == ‘arpack’. Must be of range [0.0, infinity). |
opts.whiten ? | boolean | When true (false by default) the components_ vectors are multiplied by the square root of n_samples and then divided by the singular values to ensure uncorrelated outputs with unit component-wise variances. Whitening will remove some information from the transformed signal (the relative variance scales of the components) but can sometime improve the predictive accuracy of the downstream estimators by making their data respect some hard-wired assumptions. |
Returns PCA
Defined in generated/decomposition/PCA.ts:33
Properties
Property | Type | Default value | Defined in |
---|---|---|---|
_isDisposed | boolean | false | generated/decomposition/PCA.ts:31 |
_isInitialized | boolean | false | generated/decomposition/PCA.ts:30 |
_py | PythonBridge | undefined | generated/decomposition/PCA.ts:29 |
id | string | undefined | generated/decomposition/PCA.ts:26 |
opts | any | undefined | generated/decomposition/PCA.ts:27 |
Accessors
components_
Get Signature
get components_():
Promise
<ArrayLike
[]>
Principal axes in feature space, representing the directions of maximum variance in the data. Equivalently, the right singular vectors of the centered input data, parallel to its eigenvectors. The components are sorted by decreasing explained_variance_
.
Returns Promise
<ArrayLike
[]>
Defined in generated/decomposition/PCA.ts:551
explained_variance_
Get Signature
get explained_variance_():
Promise
<ArrayLike
>
The amount of variance explained by each of the selected components. The variance estimation uses n_samples \- 1
degrees of freedom.
Equal to n_components largest eigenvalues of the covariance matrix of X.
Returns Promise
<ArrayLike
>
Defined in generated/decomposition/PCA.ts:576
explained_variance_ratio_
Get Signature
get explained_variance_ratio_():
Promise
<ArrayLike
>
Percentage of variance explained by each of the selected components.
If n_components
is not set then all components are stored and the sum of the ratios is equal to 1.0.
Returns Promise
<ArrayLike
>
Defined in generated/decomposition/PCA.ts:603
feature_names_in_
Get Signature
get feature_names_in_():
Promise
<ArrayLike
>
Names of features seen during fit. Defined only when X
has feature names that are all strings.
Returns Promise
<ArrayLike
>
Defined in generated/decomposition/PCA.ts:768
mean_
Get Signature
get mean_():
Promise
<ArrayLike
>
Per-feature empirical mean, estimated from the training set.
Equal to X.mean(axis=0)
.
Returns Promise
<ArrayLike
>
Defined in generated/decomposition/PCA.ts:653
n_components_
Get Signature
get n_components_():
Promise
<number
>
The estimated number of components. When n_components is set to ‘mle’ or a number between 0 and 1 (with svd_solver == ‘full’) this number is estimated from input data. Otherwise it equals the parameter n_components, or the lesser value of n_features and n_samples if n_components is undefined
.
Returns Promise
<number
>
Defined in generated/decomposition/PCA.ts:675
n_features_in_
Get Signature
get n_features_in_():
Promise
<number
>
Number of features seen during fit.
Returns Promise
<number
>
Defined in generated/decomposition/PCA.ts:745
n_samples_
Get Signature
get n_samples_():
Promise
<number
>
Number of samples in the training data.
Returns Promise
<number
>
Defined in generated/decomposition/PCA.ts:698
noise_variance_
Get Signature
get noise_variance_():
Promise
<number
>
The estimated noise covariance following the Probabilistic PCA model from Tipping and Bishop 1999. See “Pattern Recognition and Machine Learning” by C. Bishop, 12.2.1 p. 574 or http://www.miketipping.com/papers/met-mppca.pdf. It is required to compute the estimated data covariance and score samples.
Equal to the average of (min(n_features, n_samples) - n_components) smallest eigenvalues of the covariance matrix of X.
Returns Promise
<number
>
Defined in generated/decomposition/PCA.ts:722
py
Get Signature
get py():
PythonBridge
Returns PythonBridge
Set Signature
set py(
pythonBridge
):void
Parameters
Parameter | Type |
---|---|
pythonBridge | PythonBridge |
Returns void
Defined in generated/decomposition/PCA.ts:99
singular_values_
Get Signature
get singular_values_():
Promise
<ArrayLike
>
The singular values corresponding to each of the selected components. The singular values are equal to the 2-norms of the n_components
variables in the lower-dimensional space.
Returns Promise
<ArrayLike
>
Defined in generated/decomposition/PCA.ts:628
Methods
dispose()
dispose():
Promise
<void
>
Disposes of the underlying Python resources.
Once dispose()
is called, the instance is no longer usable.
Returns Promise
<void
>
Defined in generated/decomposition/PCA.ts:150
fit()
fit(
opts
):Promise
<any
>
Fit the model with X.
Parameters
Parameter | Type | Description |
---|---|---|
opts | object | - |
opts.X ? | ArrayLike | Training data, where n_samples is the number of samples and n_features is the number of features. |
opts.y ? | any | Ignored. |
Returns Promise
<any
>
Defined in generated/decomposition/PCA.ts:167
fit_transform()
fit_transform(
opts
):Promise
<ArrayLike
[]>
Fit the model with X and apply the dimensionality reduction on X.
Parameters
Parameter | Type | Description |
---|---|---|
opts | object | - |
opts.X ? | ArrayLike | Training data, where n_samples is the number of samples and n_features is the number of features. |
opts.y ? | any | Ignored. |
Returns Promise
<ArrayLike
[]>
Defined in generated/decomposition/PCA.ts:203
get_covariance()
get_covariance(
opts
):Promise
<any
>
Compute data covariance with the generative model.
cov \= components_.T \* S\*\*2 \* components_ + sigma2 \* eye(n_features)
where S**2 contains the explained variances, and sigma2 contains the noise variances.
Parameters
Parameter | Type | Description |
---|---|---|
opts | object | - |
opts.cov ? | any | Estimated covariance of data. |
Returns Promise
<any
>
Defined in generated/decomposition/PCA.ts:242
get_feature_names_out()
get_feature_names_out(
opts
):Promise
<any
>
Get output feature names for transformation.
The feature names out will prefixed by the lowercased class name. For example, if the transformer outputs 3 features, then the feature names out are: \["class_name0", "class_name1", "class_name2"\]
.
Parameters
Parameter | Type | Description |
---|---|---|
opts | object | - |
opts.input_features ? | any | Only used to validate feature names with the names seen in fit . |
Returns Promise
<any
>
Defined in generated/decomposition/PCA.ts:276
get_metadata_routing()
get_metadata_routing(
opts
):Promise
<any
>
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
Parameters
Parameter | Type | Description |
---|---|---|
opts | object | - |
opts.routing ? | any | A MetadataRequest encapsulating routing information. |
Returns Promise
<any
>
Defined in generated/decomposition/PCA.ts:310
get_precision()
get_precision(
opts
):Promise
<any
>
Compute data precision matrix with the generative model.
Equals the inverse of the covariance but computed with the matrix inversion lemma for efficiency.
Parameters
Parameter | Type | Description |
---|---|---|
opts | object | - |
opts.precision ? | any | Estimated precision of data. |
Returns Promise
<any
>
Defined in generated/decomposition/PCA.ts:344
init()
init(
py
):Promise
<void
>
Initializes the underlying Python resources.
This instance is not usable until the Promise
returned by init()
resolves.
Parameters
Parameter | Type |
---|---|
py | PythonBridge |
Returns Promise
<void
>
Defined in generated/decomposition/PCA.ts:112
inverse_transform()
inverse_transform(
opts
):Promise
<any
>
Transform data back to its original space.
In other words, return an input X_original
whose transform would be X.
Parameters
Parameter | Type | Description |
---|---|---|
opts | object | - |
opts.X ? | ArrayLike [] | New data, where n_samples is the number of samples and n_components is the number of components. |
Returns Promise
<any
>
Defined in generated/decomposition/PCA.ts:378
score()
score(
opts
):Promise
<number
>
Return the average log-likelihood of all samples.
See. “Pattern Recognition and Machine Learning” by C. Bishop, 12.2.1 p. 574 or http://www.miketipping.com/papers/met-mppca.pdf
Parameters
Parameter | Type | Description |
---|---|---|
opts | object | - |
opts.X ? | ArrayLike [] | The data. |
opts.y ? | any | Ignored. |
Returns Promise
<number
>
Defined in generated/decomposition/PCA.ts:412
score_samples()
score_samples(
opts
):Promise
<ArrayLike
>
Return the log-likelihood of each sample.
See. “Pattern Recognition and Machine Learning” by C. Bishop, 12.2.1 p. 574 or http://www.miketipping.com/papers/met-mppca.pdf
Parameters
Parameter | Type | Description |
---|---|---|
opts | object | - |
opts.X ? | ArrayLike [] | The data. |
Returns Promise
<ArrayLike
>
Defined in generated/decomposition/PCA.ts:451
set_output()
set_output(
opts
):Promise
<any
>
Set output container.
See Introducing the set_output API for an example on how to use the API.
Parameters
Parameter | Type | Description |
---|---|---|
opts | object | - |
opts.transform ? | "default" | "pandas" | "polars" | Configure output of transform and fit_transform . |
Returns Promise
<any
>
Defined in generated/decomposition/PCA.ts:485
transform()
transform(
opts
):Promise
<ArrayLike
[]>
Apply dimensionality reduction to X.
X is projected on the first principal components previously extracted from a training set.
Parameters
Parameter | Type | Description |
---|---|---|
opts | object | - |
opts.X ? | ArrayLike | New data, where n_samples is the number of samples and n_features is the number of features. |
Returns Promise
<ArrayLike
[]>
Defined in generated/decomposition/PCA.ts:519