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
.
my_matrix
before multiplying it with PCA eigenvectors. $\endgroup$my_matrix_centered = my_matrix - np.mean(my_matrix,axis=0)
,mdl_FitTrans_manual = np.dot(my_matrix_centered, pca_components.T)
$\endgroup$