Skip to content

Instantly share code, notes, and snippets.

@eickenberg
Last active August 29, 2015 14:10
Show Gist options
  • Save eickenberg/52a1e781ce2f36af5064 to your computer and use it in GitHub Desktop.
Save eickenberg/52a1e781ce2f36af5064 to your computer and use it in GitHub Desktop.
Comparing decompositions of X and X.T.dot(X)
import numpy as np
rng = np.random.RandomState(42)
n_samples, n_features = 100, 50
X = rng.randn(n_samples, n_features)
V1, S1, VT1 = np.linalg.svd(X.T.dot(X), full_matrices=True)
U2, S2, VT2 = np.linalg.svd(X, full_matrices=True)
S3, V3 = np.linalg.eigh(X.T.dot(X))
S3, V3 = S3[::-1], V3[:, ::-1]
import matplotlib.pyplot as plt
plt.figure()
plt.subplot(3, 4, 1)
plt.imshow(VT1 - V1.T, interpolation="nearest")
plt.colorbar()
plt.title("VT1 - V1.T")
plt.subplot(3, 4, 2)
plt.imshow(VT1 - VT2, interpolation="nearest")
plt.colorbar()
plt.title("VT1 - VT2")
plt.subplot(3, 4, 3)
plt.imshow(VT1 - V3.T, interpolation="nearest")
plt.colorbar()
plt.title("VT1 - V3.T")
plt.subplot(3, 4, 4)
plt.imshow(VT2 - V3.T, interpolation="nearest")
plt.colorbar()
plt.title("VT2 - V3.T")
plt.subplot(3, 4, 6)
plt.imshow(VT1 + VT2, interpolation="nearest")
plt.colorbar()
plt.title("VT1 + VT2")
plt.subplot(3, 4, 7)
plt.imshow(VT1 + V3.T, interpolation="nearest")
plt.colorbar()
plt.title("VT1 + V3.T")
plt.subplot(3, 4, 8)
plt.imshow(VT2 + V3.T, interpolation="nearest")
plt.colorbar()
plt.title("VT2 + V3.T")
plt.subplot(3, 4, 10)
plt.plot(S1 - S2 ** 2)
plt.title("S1 - S2 ** 2")
plt.subplot(3, 4, 11)
plt.plot(S1 - S3)
plt.title("S1 - S3")
plt.subplot(3, 4, 12)
plt.plot(S2 ** 2 - S3)
plt.title("S2 ** 2 - S3")
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment