0
$\begingroup$

I try to understand how the Python function sklearn.decomposition.PCA working. I read the documentation of the code package in Github about transform function. And I found the formula to compute the matrix it returns when you fit the original matrix(say A).

However, I found that when I use a different matrix(say B), and you apply the same formula for the coefficient you obtained from fitting matrix A. It does not work anymore.

I am super curious why this happened and hope someone can point me to the correct solution.

To prove that, I will present the code I run below.

import numpy as np
from sklearn.decomposition import PCA
A=np.array([[1,1,2],[3,4,2],[5,5,3]])
pca_model=PCA(2)
pca_model.fit_transform(A)

The result is:

array([[ 3.0818756 , -0.23999655],
       [-0.42078046,  0.61522301],
       [-2.66109514, -0.37522647]])

I can recover this result by

np.dot((A-np.mean(A,axis=0)),np.transpose(pca_model.components_))

where I got:

array([[ 3.0818756 , -0.23999655],
       [-0.42078046,  0.61522301],
       [-2.66109514, -0.37522647]])

However, if I try with a different matrix B and use the components computed from A:

B=np.array([[1,5,7],[2,4,6]])
pca_model.transform(B)

I got

array([[-0.55447918, -1.10164678],
       [-0.37055733, -1.51766277]])

When I did

np.dot((B-np.mean(B,axis=0)),np.transpose(pca_model.components_))

I got

array([[-0.09196093,  0.208008  ],
       [ 0.09196093, -0.208008  ]])

Why did this happen?

$\endgroup$
2
  • $\begingroup$ why 'try different matrix B and use the components computed from A'? what does it mean? $\endgroup$ – Haitao Du Mar 28 at 8:00
  • $\begingroup$ Hi Haitao, I was doing PCA reduction dimensionality, which the method I use is to fit one matrix A (say training matrix), pca_model.fit_transform(A), and use the same components apply to matrix B (say testing matrix) without fitting, pca_model.transform(B). $\endgroup$ – EmilyCurious Mar 28 at 17:16
0
$\begingroup$

I finally figured it out! I should use

np.dot((B-np.mean(A,axis=0)),np.transpose(pca_model.components_))

Instead of taking the mean of matrix B. Now I obtain

array([[-0.55447918, -1.10164678],
      [-0.37055733, -1.51766277]])

which is only exactly the same as pca_model.transform(B).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.