diff --git a/doc/src/DimRed/Programs/PCAsimple.py b/doc/src/DimRed/Programs/PCAsimple.py new file mode 100644 index 000000000..bd168f6df --- /dev/null +++ b/doc/src/DimRed/Programs/PCAsimple.py @@ -0,0 +1,36 @@ +import numpy as np +import pandas as pd +from IPython.display import display + + +n = 100 +mean = (-1, 2) +cov = [[4, 2], [2, 2]] +X = np.random.multivariate_normal(mean, cov, n) +print(X) + +df = pd.DataFrame(X) +# Pandas does the centering for us +df = df -df.mean() +display(df) + +# we center it ourselves +X_centered = X - X.mean(axis=0) +# test that we get the same as Pandas +print(X_centered-df) +#Now we do an SVD +U, s, V = np.linalg.svd(X_centered) +c1 = V.T[:, 0] +c2 = V.T[:, 1] +W2 = V.T[:, :2] +X2D = X_centered.dot(W2) +print(X2D) +#thereafter we do a PCA with Scikit-learn +from sklearn.decomposition import PCA +pca = PCA(n_components = 2) +X2Dsl = pca.fit_transform(X) +print("Check that we get the same") +print(X2D-X2Dsl) + +print(pca.components_.T[:, 0]) +