Documentation
Classes
PCA

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.

It can also use the scipy.sparse.linalg ARPACK implementation of the truncated SVD.

Notice that this class does not support sparse input. See TruncatedSVD for an alternative with sparse data.

Read more in the User Guide.

Python Reference (opens in a new tab)

Constructors

constructor()

Signature

new PCA(opts?: object): PCA;

Parameters

NameTypeDescription
opts?object-
opts.copy?booleanIf false, data passed to fit are overwritten and running fit(X).transform(X) will not yield the expected results, use fit_transform(X) instead. Default Value true
opts.iterated_power?number | "auto"Number of iterations for the power method computed by svd_solver == ‘randomized’. Must be of range [0, infinity). Default Value 'auto'
opts.n_components?number | "mle"Number of components to keep. if n_components is not set all components are kept:
opts.n_oversamples?numberThis 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. Default Value 10
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. Default Value 'auto'
opts.random_state?numberUsed when the ‘arpack’ or ‘randomized’ solvers are used. Pass an int for reproducible results across multiple function calls. See Glossary.
opts.svd_solver?"auto" | "full" | "randomized" | "arpack"The solver is selected by a default policy based on X.shape and n\_components: 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 enabled. Otherwise the exact full SVD is computed and optionally truncated afterwards. Default Value 'auto'
opts.tol?numberTolerance for singular values computed by svd_solver == ‘arpack’. Must be of range [0.0, infinity). Default Value 0
opts.whiten?booleanWhen 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. Default Value false

Returns

PCA

Defined in: generated/decomposition/PCA.ts:31 (opens in a new tab)

Properties

_isDisposed

boolean = false

Defined in: generated/decomposition/PCA.ts:29 (opens in a new tab)

_isInitialized

boolean = false

Defined in: generated/decomposition/PCA.ts:28 (opens in a new tab)

_py

PythonBridge

Defined in: generated/decomposition/PCA.ts:27 (opens in a new tab)

id

string

Defined in: generated/decomposition/PCA.ts:24 (opens in a new tab)

opts

any

Defined in: generated/decomposition/PCA.ts:25 (opens in a new tab)

Accessors

components_

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\_.

Signature

components_(): Promise<ArrayLike[]>;

Returns

Promise<ArrayLike[]>

Defined in: generated/decomposition/PCA.ts:536 (opens in a new tab)

explained_variance_

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.

Signature

explained_variance_(): Promise<ArrayLike>;

Returns

Promise<ArrayLike>

Defined in: generated/decomposition/PCA.ts:561 (opens in a new tab)

explained_variance_ratio_

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.

Signature

explained_variance_ratio_(): Promise<ArrayLike>;

Returns

Promise<ArrayLike>

Defined in: generated/decomposition/PCA.ts:588 (opens in a new tab)

feature_names_in_

Names of features seen during fit. Defined only when X has feature names that are all strings.

Signature

feature_names_in_(): Promise<ArrayLike>;

Returns

Promise<ArrayLike>

Defined in: generated/decomposition/PCA.ts:776 (opens in a new tab)

mean_

Per-feature empirical mean, estimated from the training set.

Equal to X.mean(axis=0).

Signature

mean_(): Promise<ArrayLike>;

Returns

Promise<ArrayLike>

Defined in: generated/decomposition/PCA.ts:638 (opens in a new tab)

n_components_

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.

Signature

n_components_(): Promise<number>;

Returns

Promise<number>

Defined in: generated/decomposition/PCA.ts:660 (opens in a new tab)

n_features_

Number of features in the training data.

Signature

n_features_(): Promise<number>;

Returns

Promise<number>

Defined in: generated/decomposition/PCA.ts:683 (opens in a new tab)

n_features_in_

Number of features seen during fit.

Signature

n_features_in_(): Promise<number>;

Returns

Promise<number>

Defined in: generated/decomposition/PCA.ts:753 (opens in a new tab)

n_samples_

Number of samples in the training data.

Signature

n_samples_(): Promise<number>;

Returns

Promise<number>

Defined in: generated/decomposition/PCA.ts:706 (opens in a new tab)

noise_variance_

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 (opens in a new tab). 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.

Signature

noise_variance_(): Promise<number>;

Returns

Promise<number>

Defined in: generated/decomposition/PCA.ts:730 (opens in a new tab)

py

Signature

py(): PythonBridge;

Returns

PythonBridge

Defined in: generated/decomposition/PCA.ts:97 (opens in a new tab)

Signature

py(pythonBridge: PythonBridge): void;

Parameters

NameType
pythonBridgePythonBridge

Returns

void

Defined in: generated/decomposition/PCA.ts:101 (opens in a new tab)

singular_values_

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.

Signature

singular_values_(): Promise<ArrayLike>;

Returns

Promise<ArrayLike>

Defined in: generated/decomposition/PCA.ts:613 (opens in a new tab)

Methods

dispose()

Disposes of the underlying Python resources.

Once dispose() is called, the instance is no longer usable.

Signature

dispose(): Promise<void>;

Returns

Promise<void>

Defined in: generated/decomposition/PCA.ts:159 (opens in a new tab)

fit()

Fit the model with X.

Signature

fit(opts: object): Promise<any>;

Parameters

NameTypeDescription
optsobject-
opts.X?ArrayLike[]Training data, where n\_samples is the number of samples and n\_features is the number of features.
opts.y?anyIgnored.

Returns

Promise<any>

Defined in: generated/decomposition/PCA.ts:176 (opens in a new tab)

fit_transform()

Fit the model with X and apply the dimensionality reduction on X.

Signature

fit_transform(opts: object): Promise<ArrayLike[]>;

Parameters

NameTypeDescription
optsobject-
opts.X?ArrayLike[]Training data, where n\_samples is the number of samples and n\_features is the number of features.
opts.y?anyIgnored.

Returns

Promise<ArrayLike[]>

Defined in: generated/decomposition/PCA.ts:213 (opens in a new tab)

get_covariance()

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.

Signature

get_covariance(opts: object): Promise<any>;

Parameters

NameTypeDescription
optsobject-
opts.cov?anyEstimated covariance of data.

Returns

Promise<any>

Defined in: generated/decomposition/PCA.ts:253 (opens in a new tab)

get_feature_names_out()

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"\].

Signature

get_feature_names_out(opts: object): Promise<any>;

Parameters

NameTypeDescription
optsobject-
opts.input_features?anyOnly used to validate feature names with the names seen in fit.

Returns

Promise<any>

Defined in: generated/decomposition/PCA.ts:288 (opens in a new tab)

get_precision()

Compute data precision matrix with the generative model.

Equals the inverse of the covariance but computed with the matrix inversion lemma for efficiency.

Signature

get_precision(opts: object): Promise<any>;

Parameters

NameTypeDescription
optsobject-
opts.precision?anyEstimated precision of data.

Returns

Promise<any>

Defined in: generated/decomposition/PCA.ts:323 (opens in a new tab)

init()

Initializes the underlying Python resources.

This instance is not usable until the Promise returned by init() resolves.

Signature

init(py: PythonBridge): Promise<void>;

Parameters

NameType
pyPythonBridge

Returns

Promise<void>

Defined in: generated/decomposition/PCA.ts:110 (opens in a new tab)

inverse_transform()

Transform data back to its original space.

In other words, return an input X\_original whose transform would be X.

Signature

inverse_transform(opts: object): Promise<any>;

Parameters

NameTypeDescription
optsobject-
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:358 (opens in a new tab)

score()

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 (opens in a new tab)

Signature

score(opts: object): Promise<number>;

Parameters

NameTypeDescription
optsobject-
opts.X?ArrayLike[]The data.
opts.y?anyIgnored.

Returns

Promise<number>

Defined in: generated/decomposition/PCA.ts:393 (opens in a new tab)

score_samples()

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 (opens in a new tab)

Signature

score_samples(opts: object): Promise<ArrayLike>;

Parameters

NameTypeDescription
optsobject-
opts.X?ArrayLike[]The data.

Returns

Promise<ArrayLike>

Defined in: generated/decomposition/PCA.ts:433 (opens in a new tab)

set_output()

Set output container.

See Introducing the set_output API for an example on how to use the API.

Signature

set_output(opts: object): Promise<any>;

Parameters

NameTypeDescription
optsobject-
opts.transform?"default" | "pandas"Configure output of transform and fit\_transform.

Returns

Promise<any>

Defined in: generated/decomposition/PCA.ts:468 (opens in a new tab)

transform()

Apply dimensionality reduction to X.

X is projected on the first principal components previously extracted from a training set.

Signature

transform(opts: object): Promise<ArrayLike[]>;

Parameters

NameTypeDescription
optsobject-
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:503 (opens in a new tab)