2
$\begingroup$

I am trying to do PCA from sklearn with n_components = 5. I apply the dimensionality reduction on my data using fit_transform(data) as defined here: http://scikit-learn.org/stable/modules/generated/sklearn.decomposition.PCA.html.

Initially I tried to do the classical matrix multiplication between pca.components_ values and my x_features data, but the results are different. So I am whether doing my multiplication incorrectly or I did not understand how fit_transform work.

Below is a mock-up example to compare classic matrix multiplication and fit_transform:

import numpy as np
from sklearn import decomposition
np.random.seed(0)
my_matrix = np.random.randn(100, 5)

mdl = decomposition.PCA(n_components=5)
mdl_FitTrans = mdl.fit_transform(my_matrix)
pca_components = mdl.components_
mdl_FitTrans_manual = np.dot(pca_components, my_matrix.transpose())
mdl_FitTrans_manualT = mdl_FitTrans_manual.transpose()

I am expecting mdl_FitTrans == mdl_FitTrans_manual but the result is False.

$\endgroup$
  • 1
    $\begingroup$ You need to center my_matrix before multiplying it with PCA eigenvectors. $\endgroup$ – amoeba Dec 6 '16 at 15:32
  • $\begingroup$ since I cannot edit comments. these are the extra operations needed. my_matrix_centered = my_matrix - np.mean(my_matrix,axis=0) , mdl_FitTrans_manual = np.dot(my_matrix_centered, pca_components.T) $\endgroup$ – RMS Dec 7 '16 at 13:16
1
$\begingroup$

I added the answer from the comments here. Data needed centering as suggested by @amoeba. According to the documentation also (https://github.com/scikit-learn/scikit-learn/blob/a5ab948/sklearn/decomposition/base.py#L101)

import numpy as np
from sklearn import decomposition
np.random.seed(0)
my_matrix = np.random.randn(100, 5)

mdl = decomposition.PCA(n_components=5)
mdl_FitTrans = mdl.fit_transform(my_matrix)
pca_components = mdl.components_
my_matrix_centered = my_matrix - np.mean(my_matrix,axis=0) 
mdl_FitTrans_manual = np.dot(my_matrix_centered, pca_components.T)

(mdl_FitTrans.all() ==  mdl_FitTrans_manual.all())
True

EDIT: because I am not consistent with my variable naming

| cite | improve this answer | |
$\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.