more pca
This commit is contained in:
@@ -2,28 +2,47 @@ import numpy as np
|
||||
import pandas as pd
|
||||
from IPython.display import display
|
||||
import matplotlib.pyplot as plt
|
||||
from sklearn.model_selection import train_test_split
|
||||
from sklearn.preprocessing import MinMaxScaler, StandardScaler
|
||||
|
||||
|
||||
n = 10000
|
||||
n = 1000
|
||||
mean = (-1, 2)
|
||||
cov = [[4, 2], [2, 2]]
|
||||
print(cov)
|
||||
X = np.random.multivariate_normal(mean, cov, n)
|
||||
print(np.cov(X.T))
|
||||
|
||||
df = pd.DataFrame(X)
|
||||
# Pandas does the centering for us
|
||||
df = df -df.mean()
|
||||
correlation_matrix = df.cov()
|
||||
print(correlation_matrix)
|
||||
print("Centered covariance with Pandas")
|
||||
covarianceX = df.cov()
|
||||
|
||||
print(covarianceX)
|
||||
|
||||
# we center it ourselves
|
||||
X_centered = X - X.mean(axis=0)
|
||||
print("Centered covariance using numpy")
|
||||
print(np.cov(X_centered.T))
|
||||
#print("test that we get the same as Pandas")
|
||||
#print(X_centered-X_train_scaled)
|
||||
# extract the relevant columns from the centered design matrix
|
||||
x = X_centered[:,[0]]
|
||||
y = X_centered[:,[1]]
|
||||
Cov = np.zeros((2,2))
|
||||
cov_xy = np.sum(x.T@y)/(n-1.0)
|
||||
cov_xx = np.sum(x.T@x)/(n-1.0)
|
||||
cov_yy = np.sum(y.T@y)/(n-1.0)
|
||||
|
||||
Cov[0,0]= cov_xx
|
||||
Cov[1,1]= cov_yy
|
||||
Cov[0,1]= cov_xy
|
||||
Cov[1,0]= Cov[0,1]
|
||||
print("Centered covariance using own code")
|
||||
print(Cov)
|
||||
|
||||
plt.plot(x, y, 'x')
|
||||
plt.axis('equal')
|
||||
plt.show()
|
||||
|
||||
|
||||
|
||||
|
||||
"""
|
||||
#Now we do an SVD
|
||||
U, s, V = np.linalg.svd(X_centered)
|
||||
c1 = V.T[:, 0]
|
||||
@@ -38,6 +57,6 @@ print("Check that we get the same")
|
||||
print(X2D-X2Dsl)
|
||||
|
||||
print(pca.components_.T[:, 0])
|
||||
|
||||
"""
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user