Scilab Website | Contribute with GitLab | Scilab Community | ATOMS toolboxes
Scilab Online Help
2027.0.0 - 日本語


pca

Computes principal component analysis for the data matrix X

Syntax

comprinc = pca(X)
[comprinc, score, lambda] = pca(X)
[comprinc, score, lambda, tsquare] = pca(X)
[comprinc, score, lambda, tsquare, explained, mu] = pca(X)

comprinc = pca(X, Name, Value)
[comprinc, score, lambda] = pca(X, Name, Value)
[comprinc, score, lambda, tsquare] = pca(X, Name, Value)
[comprinc, score, lambda, tsquare, explained, mu] = pca(X, Name, Value)

Arguments

X

is a nxp (n observations, p variables) real matrix.

Name, Value

'Algorithm': Algorithm used to compute the principal components. Possible values are: "svd" (default) and "eig"

'Centered': Boolean specifying whether variables are centered before computing the PCA. Default value: %t.

'Economy': Boolean, use to allow economy size singular value decomposition. This option is only used when Algorithm="svd". Default value: %t.

'NumComponents': Positive integer specifying the number of principal components to return. The outputs coeff, score, latent, explained and the Hotelling's T² statistic are computed using only these returned principal components. Default value: size(X, 2).

'Weights': Row vector of doubles of length size(X, 1) containing observation weights. Default value: ones(1, size(X, 1)).

'VariableWeights': A row positive vector of length size(X, 2) containing variable weights or the string "variance". Default value: [].

comprinc

Matrix of size p x p (p is equal to size(X, 2)) whose columns are the principal component coefficients (principal directions).

score

Principal component scores.

n-by-p matrix or n-by-NumComponents matrix if 'NumComponents' is specified.

Each row corresponds to the projection of an observation onto the selected principal components.

lambda

Variance explained by each principal component (eigenvalues of the covariance matrix).

p-by-1 or NumComponents-by-1 (if 'NumComponents' is specified) vector.

tsquare

a n column vector. Hotelling's T² statistic computed from the returned principal component scores.

explained

a column vector of length "number of components". The percentage of variance explained by each principal component.

mu

a row vector of length p. The estimated mean of each variable of X.

Description

pca performs Principal Component Analysis (PCA) on the observations stored in the rows of X. The variables are assumed to be stored in the columns.

By default, the variables are centered before computing the principal components. The coefficients are returned as orthonormal vectors ordered according to decreasing explained variance.

The principal component scores are obtained by projecting the centered observations onto the principal component directions.

The Hotelling's T² statistic is computed from the principal component scores returned by the function. Consequently, when the "NumComponents" option is used, the T² statistic is evaluated in the reduced principal component subspace defined by the returned components.

Two numerical algorithms are available:

  • "svd" (default): computes the principal components using the Singular Value Decomposition of the data matrix.

  • "eig": computes the principal components from the eigenvalue decomposition of the covariance matrix. When "NumComponents" is smaller than the number of variables, only the requested dominant eigenpairs are computed using eigs, which can significantly reduce the computation time for large data sets.

The "svd" and "eig" algorithms produce equivalent principal components up to numerical round-off errors. The "eig" algorithm can be advantageous when only a small number of principal components is requested.

To obtain the pca graph, use the show_pca function.

Examples

a=rand(100,10,'n');
[comprinc, scores, lambda] = pca(a);
show_pca(lambda, comprinc)
x = [
395     224     35.1     79.1     6.0     14.9
410     232     31.9     73.4     8.7     16.4
405     233     30.7     76.5     7.0     16.5
405     240     30.4     75.3     8.7     16.0
390     217     31.9     76.5     7.8     15.7
415     243     32.1     77.4     7.1     18.5
390     229     32.1     78.4     4.6     17.0
405     240     31.1     76.5     8.2     15.3
420     234     32.4     76.0     7.2     16.8
390     223     33.8     77.0     6.2     16.8
415     247     30.7     75.5     8.4     16.1
400     234     31.7     77.6     5.7     18.7
400     224     28.2     73.5     11.0    15.5
395     229     29.4     74.5     9.3     16.1
395     219     29.7     72.8     8.7     18.5
395     224     28.5     73.7     8.7     17.3
400     223     28.5     73.1     9.1     17.7
400     224     27.8     73.2     12.2    14.6
400     221     26.5     72.3     13.2    14.5
410     233     25.9     72.3     11.1    16.6
402     234     27.1     72.1     10.4    17.5
400     223     26.8     70.3     13.5    16.2
400     213     25.8     70.4     12.1    17.5
];
[comprinc, scores, lambda, tsquare, explained] = pca(wcenter(x, 1));
scf();
show_pca(lambda, comprinc)
//
// Extract the two last columns.
x = x(:,1:2);
[comprinc, scores, lambda, tsquare, explained] = pca(wcenter(x, 1));
scf();
// See how the points are perfectly on the circle.
show_pca(lambda, comprinc)
x = [1 2 1;2 1 3; 3 2 3]
[comprinc, scores, lambda, tsquare, explained, mu] = pca(x, "Economy", %t);
scores * comprinc' + ones(3, 1) * mu // == x
x = [1 2 1; 2 3 1; 0 2 1; 2 3 9;5 -2 7; -1 2 1];
[comprinc, scores, lambda, tsquare, explained, mu] = pca(x, "VariableWeights", "variance")
// comprinc is not orthonormal
newcomprinc = diag(stdev(x, 1))\comprinc
scf();
show_pca(lambda, newcomprinc)
X = [-1 -1;
     -2 -1;
     -3 -2;
      1  1;
      2  1;
      3  2];

// PCA using SVD
[coefs, score, l, tsquare, explained] = pca(X)

// coefs = [2x2 double]
// 
//    0.8384922  -0.5449135
//    0.5449135   0.8384922
// 
//  score = [6x2 double]
// 
//   -1.3834058  -0.2935787
//   -2.221898    0.2513348
//   -3.6053038  -0.0422439
//    1.3834058   0.2935787
//    2.221898   -0.2513348
//    3.6053038   0.0422439
// 
//  l = [2x1 double]
// 
//    7.9395431
//    0.0604569
// 
//  tsquare = [6x1 double]
// 
//    1.6666667
//    1.6666667
//    1.6666667
//    1.6666667
//    1.6666667
//    1.6666667
// 
//  explained = [2x1 double]
// 
//    99.244289
//    0.7557110

score * coefs' == X

// PCA using eigenvalue decomposition
[coefs, score, l, tsquare, explained] = pca(X, "Algorithm", "eig")

// coefs = [2x2 double]
//
//   0.8384922  -0.5449135
//   0.5449135   0.8384922
//
// score = [6x2 double]
//
//  -1.3834058  -0.2935787
//  -2.221898    0.2513348
//  -3.6053038  -0.0422439
//   1.3834058   0.2935787
//   2.221898   -0.2513348
//   3.6053038   0.0422439
//
// l = [2x1 double]
//
//   7.9395431
//   0.0604569
//
// tsquare = [6x1 double]
//
//   1.6666667
//   1.6666667
//   1.6666667
//   1.6666667
//   1.6666667
//   1.6666667
//
// explained = [2x1 double]
//
//   99.244289
//   0.7557110

// Keep only the first principal component
[coefs, score, l] = pca(X, "Algorithm", "eig", "NumComponents", 1)

// coefs = [2x1 double]
//
//   0.8384922
//   0.5449135
//
// score = [6x1 double]
//
//  -1.3834058
//  -2.2218980
//  -3.6053038
//   1.3834058
//   2.2218980
//   3.6053038
//
// l = 
//
//   7.9395431

See also

  • show_pca — Visualization of principal components analysis results
  • svd — 特異値分解
  • eigs — 行列の最大固有値と固有ベクトルを計算

History

バージョン記述
2025.0.0

Improvements of the function:

  • "Economy", "Centered", "NumComponents", "Weights" and "VariableWeights" options are added.

  • Returns now the percentage of the variance explained by each principal component and the estimated mean of each variable of X.

  • Warning, there is an incompatibility with previous versions: the order of the first three output arguments have changed.

    The old [lambda, comprinc, scores] = pca(x) syntax has been replaced by [comprinc, scores, lambda] = pca(x). In previous versions, pca only computed the principal components with standardized variables. To obtain the same results, use "VariableWeights" option with "variance" value: [comprinc, scores, lambda] = pca(x, "VariableWeights", "variance"). In this case, comprinc is not orthonormal. You can compute the orthonormal coefficient thanks to diag(stdev(x))\comprinc. An other possibility is to use wcenter function i.e, [comprinc, scores, lambda] = pca(wcenter(x, 1)). A warning is displayed when pca is called with 1, 2 or 3 output arguments. To avoid the warning to be displayed, call pca with at least four output arguments (e.g. [comprinc, scores, lambda, _] = pca()).

    Note that lambda is a column vector containing the eigenvalues of the covariance matrix of x. To obtain the ratio of the corresponding eigenvalue over the sum of eigenvalues, use the following formula: lambda(:, 1) / sum(lambda(:, 1))

2027.0.0

Algorithm option added. Possible values are "svd" and "eigs".

Report an issue
<< covar Multivariate - regress correl PCA reglin >>

Copyright (c) 2022-2026 (Dassault Systèmes S.E.)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Thu Oct 15 11:21:53 CEST 2026