# Manual computation of principal components disagrees with the fit_transform() output in Python

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.

• You need to center my_matrix before multiplying it with PCA eigenvectors. – amoeba Dec 6 '16 at 15:32
• 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) – RMS Dec 7 '16 at 13:16

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