Database Reference
In-Depth Information
import breeze.linalg.DenseMatrix
val pcBreeze = new DenseMatrix(rows, cols, pc.toArray)
Breeze provides a useful function within the linalg package to write the matrix out as a
CSV file. We will use this to save the principal components to a temporary CSV file:
import breeze.linalg.csvwrite
csvwrite(new File("/tmp/pc.csv"), pcBreeze)
Next, we will load the matrix in IPython and visualize the principal components as im-
ages. Fortunately, numpy provides a utility function to read the matrix from the CSV file
we created:
pcs = np.loadtxt("/tmp/pc.csv", delimiter=",")
print(pcs.shape)
You should see the following output, confirming that the matrix we read has the same di-
mensions as the one we saved:
(2500, 10)
We will need a utility function to display the images, which we define here:
def plot_gallery(images, h, w, n_row=2, n_col=5):
"""Helper function to plot a gallery of portraits"""
plt.figure(figsize=(1.8 * n_col, 2.4 * n_row))
plt.subplots_adjust(bottom=0, left=.01, right=.99,
top=.90, hspace=.35)
for i in range(n_row * n_col):
plt.subplot(n_row, n_col, i + 1)
plt.imshow(images[:, i].reshape((h, w)),
cmap=plt.cm.gray)
plt.title("Eigenface %d" % (i + 1), size=12)
plt.xticks(())
plt.yticks(())
Search WWH ::




Custom Search