Skip to content

Commit 06f6616

Browse files
committed
Added figure files illustrating why pca sometimes doesn't work so well
1 parent 3962955 commit 06f6616

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

fig/chap6/mnist_pca.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
mnist_pca
3+
~~~~~~~~~
4+
5+
Plot graphs to illustrate the limitations of PCA.
6+
"""
7+
8+
# Third-party libraries
9+
from mpl_toolkits.mplot3d import Axes3D
10+
import matplotlib.pyplot as plt
11+
import numpy as np
12+
13+
# Plot just the data
14+
fig = plt.figure()
15+
ax = fig.gca(projection='3d')
16+
z = np.linspace(-2, 2, 20)
17+
theta = np.linspace(-4 * np.pi, 4 * np.pi, 20)
18+
x = np.sin(theta)+0.03*np.random.randn(20)
19+
y = np.cos(theta)+0.03*np.random.randn(20)
20+
ax.plot(x, y, z, 'ro')
21+
plt.show()
22+
23+
# Plot the data and the helix together
24+
fig = plt.figure()
25+
ax = fig.gca(projection='3d')
26+
z_helix = np.linspace(-2, 2, 100)
27+
theta_helix = np.linspace(-4 * np.pi, 4 * np.pi, 100)
28+
x_helix = np.sin(theta_helix)
29+
y_helix = np.cos(theta_helix)
30+
ax.plot(x, y, z, 'ro')
31+
ax.plot(x_helix, y_helix, z_helix, '')
32+
plt.show()

fig/chap6/pca_hard_data.png

52.2 KB
Loading

fig/chap6/pca_hard_data_fit.png

80.2 KB
Loading

0 commit comments

Comments
 (0)