data-science-from-scratch icon indicating copy to clipboard operation
data-science-from-scratch copied to clipboard

PCA example

Open paulaceccon opened this issue 6 years ago • 1 comments

Why does the PCA example returns components with the opposite sign of the ones from sklearn PCA? Also, when I try to standardize the data and use the code, the components obtained through PCA are the same, which doesn't make sense. Notebook with examples attached. PrincipalComponentAnalysis.ipynb.zip

paulaceccon avatar Apr 17 '19 17:04 paulaceccon

Using the updated code, the same incorrect result is obtained.

From the code, after scaling the data:

from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()
scaler.fit(pca_data)
data_scaled_test = scaler.transform(pca_data)
fpc = pca(data_scaled_test, 2)
fpc 

[[0.7071067811865476, 0.7071067811865476], [0.7071067811865475, 0.7071067811865475]]

From sklearn API:

from sklearn.decomposition import PCA

pca = PCA(n_components=2)
pca.fit(data_scaled_test)
pca.components_

array([[-0.70710678, -0.70710678], [ 0.70710678, -0.70710678]])

PCA_tests.ipynb.zip

paulaceccon avatar Apr 24 '19 13:04 paulaceccon