Skip to content

Instantly share code, notes, and snippets.

@biggzlar
Created May 3, 2018 14:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save biggzlar/dfedf54bb622115c41cf4ca5db2236b5 to your computer and use it in GitHub Desktop.
Save biggzlar/dfedf54bb622115c41cf4ca5db2236b5 to your computer and use it in GitHub Desktop.
import numpy as np
from matplotlib import pyplot as plt
mean = [0, 0]
cov = [[1., .5], [.5, 1.]]
m = 128
X = np.random.multivariate_normal(mean, cov, m)
cov = np.cov(X.T)
eig_val, eig_vec = np.linalg.eigh(cov)
eig_vec[0,:] = eig_vec[0,:] * eig_val[0]
eig_vec[1,:] = eig_vec[1,:] * eig_val[1]
plt.figure(figsize=(8, 8))
plt.plot(X[:,0], X[:,1], 'o')
plt.arrow(mean[0], mean[1], eig_vec[0, 0], eig_vec[0, 1])
plt.arrow(mean[0], mean[1], eig_vec[1, 0], eig_vec[1, 1])
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment