Restarted venv (Python 3.12.3)

In [ ]:
import numpy as np
import numpy.linalg as LA
import matplotlib.pyplot as plt
In [ ]:
# random oscillating 2D time-series in cigar shape
t = np.linspace(0, 2*np.pi, 100)
xs = np.cos(t)
ys = xs + np.sin(t)
dada = np.stack([xs, ys], axis=1)
#plt.scatter(dada[:, 0], dada[:, 1])
plt.xlim([-1.5, 1.5])
plt.ylim([-1.5, 1.5])
# color points by time
plt.scatter(dada[:, 0], dada[:, 1], c=t, cmap='viridis')
Out[ ]:
<matplotlib.collections.PathCollection at 0x10b3f3bc0>
No description has been provided for this image
In [ ]:
# Visualize eigenvectors of X^T X (so Features x Features)
covar = dada.T @ dada
print('covar shape', covar.shape)
covar shape (2, 2)
In [ ]:
# Both return the eigenvecors in vecs[:, i] **so they are column vectors** unlike
# anywhere else in numpy or any other library, where usually its [samples, features]
#vals, vecs = LA.eig(covar) # works just as well
vals, vecs = LA.eigh(covar)

vecs = vecs[:, np.argsort(-vals)]
sorted_eigvals = vals[np.argsort(-vals)]
vecs = vecs * (sorted_eigvals / sorted_eigvals.max()) 
In [ ]:
plt.scatter(dada[:, 0], dada[:, 1], c=t)
for i in range(2):
    plt.arrow(0, 0, vecs[0, i], vecs[1, i], color='red', head_width=0.1)
plt.xlim([-1.5, 1.5])
plt.ylim([-1.5, 1.5])
Out[ ]:
(-1.5, 1.5)
No description has been provided for this image
In [ ]:
covar = dada @ dada.T # now [samples x samples] covariance
print('covar shape', covar.shape)
covar shape (100, 100)
In [ ]:
vals, vecs = LA.eigh(covar)
vecs = vecs[:, np.argsort(-vals)]
print('vecs shape', vecs.shape) # 
vecs shape (100, 100)
In [ ]:
# Now we want to plot a single eigenvector completely as a function of time
# And since `vecs` is [features, eigenvector], we want [:, i] to get the i-th eigenvector
plt.plot(vecs[:, 0], label='PC1')
plt.plot(vecs[:, 1], label='PC2')  
# plt.plot(vecs[:, 2], label='PC3')   # just noise
plt.legend()
Out[ ]:
<matplotlib.legend.Legend at 0x10b4e1400>
No description has been provided for this image