In [1]:
import numpy as np
import matplotlib.pyplot as plt
In [16]:
N=10
x = np.zeros((N,2))
mean = np.zeros(2)
x = np.random.uniform(0,1,(N,2))
mean[0] = (1/float(N))*np.sum(x[:,0])
mean[1] = (1/float(N))*np.sum(x[:,1])
plt.scatter(x[:, 0], x[:, 1])
plt.scatter(mean[0], mean[1])
plt.show()
In [36]:
xc = x - mean
Covariance = np.matmul(xc.T,xc)
value, vector = np.linalg.eig(Covariance)

plt.scatter(xc[:, 0], xc[:, 1])
plt.scatter(0, 0)
t = np.linspace(min(xc[:,0]),max(xc[:,0]),20)
plt.plot(t,(vector[1,0]/vector[0,0])*t)
plt.plot(t,(vector[1,1]/vector[0,1])*t)
plt.show()
In [61]:
u = np.array(vector[:,1])
z = np.matmul(xc,u)
xhat = np.zeros((N,2))
xhat[:,0] =  z*u[0]
xhat[:,1] =  z*u[1]

plt.scatter(xc[:, 0], xc[:, 1])
plt.scatter(xhat[:, 0], xhat[:, 1])
plt.scatter(0, 0)
t = np.linspace(min(xc[:,0]),max(xc[:,0]),20)
plt.plot(t,(vector[1,0]/vector[0,0])*t)
plt.plot(t,(vector[1,1]/vector[0,1])*t)
plt.show()
In [ ]:
 
In [ ]: