# How to resize an array using Principal Component Analysis?

Consider an array (arr) as

arr = RandomReal[1,{50,20}];


The PrincipalComponents applied on arr gives me an array of the same dimension.

However, in matlab pca gives me the coefficient matrix of 20 X 20.

How can I do so in Mathematica?

• @kglr It's giving me an array of 20X19. May 3, 2018 at 12:34

It seems that Mathematica and MATLAB are doing different things. Mathematica's PrincipalComponents returns the principal components or "scores". MATLAB's pca returns "the principal component coefficients, also known as loadings." These are related to the singular value decomposition (see https://stats.stackexchange.com/questions/134282/relationship-between-svd-and-pca-how-to-use-svd-to-perform-pca).

Here is a MATLAB/Mathematica example. The built-in MATLAB data set hald defines a variable ingredients, which is given as ing in the Mathematica code below. If {u, σ, v} is the SVD of the centered data ing, then the coefficients are given by v and the principal components by u.σ:

% MATLAB code
[V, S, L] = pca(ingredients)  % returns coefficients V, scores S, component variances L

(* Mathematica code *)
ing = N@{  (* copied from MATLAB's ingredients *)
{7,  26,  6, 60},
{1,  29, 15, 52},
{11, 56,  8, 20},
{11, 31,  8, 47},
{7,  52,  6, 33},
{11, 55,  9, 22},
{3,  71, 17,  6},
{1,  31, 22, 44},
{2,  54, 18, 22},
{21, 47,  4, 26},
{1,  40, 23, 34},
{11, 66,  9, 12},
{10, 68,  8, 12}};
s = PrincipalComponents[ing];  (* equals scores S in MATLAB *)
{u, σ, v} = SingularValueDecomposition[Transpose[Transpose@ing - Mean[ing]]];
u.σ == s
(*  True  *)

v  (* equals the coefficients V in MATLAB *)
(*
{{-0.0678,     0.646018,  -0.567315,  0.50618},
{-0.678516,   0.0199933,  0.543969,  0.493268},
{ 0.0290208, -0.75531,   -0.403553,  0.515567},
{ 0.730874,   0.10848,    0.468398,  0.484416}}
*)

• the scores returned by Mathematica is not the same as returned by Matlab. The second and third elements in each row (in Mathematica) have the opposite signs compared to Matlab. May 9, 2018 at 10:24
• and for the coefficients as well. May 9, 2018 at 10:28
• @Majis The columns of $U$ and $V$ in the SVD $A = U\Sigma V^T$ are defined up to sign. (E.g. the columns of $V$ are normalized eigenvectors of $A^T A$ and each is unique up to sign, except when the eigen/singular values are repeated.) May 9, 2018 at 11:59