Esempio n. 1
0
    def test_larger(self):
        x = load_and_center_dataset(mnist_path)
        S = get_covariance(x)

        Lambda, U = get_eig(S, 20)
        self.check_eigen(S, Lambda, U, 20)

        Lambda, U = get_eig(S, 784)
        self.check_eigen(S, Lambda, U, 784)
Esempio n. 2
0
	def test_shape(self):
		x = load_and_center_dataset(mnist_path)
		S = get_covariance(x)
		_, U = get_eig(S, 2)
		# This is the image of the "9" in the spec
		projected = project_image(x[3], U)

		self.assertEqual(np.shape(projected), (784,))
		self.assertAlmostEqual(np.min(projected), -113.79455198736488)
		self.assertAlmostEqual(np.max(projected), 120.0658469887994)
Esempio n. 3
0
	def test_small(self):
		x = load_and_center_dataset(mnist_path)
		S = get_covariance(x)
		Lambda, U = get_eig(S, 2)

		self.assertEqual(np.shape(Lambda), (2, 2))
		self.assertTrue(np.all(np.isclose(
				Lambda, [[350880.76329673, 0], [0, 245632.27295307]])))

		# The eigenvectors should be the columns
		self.assertEqual(np.shape(U), (784, 2))
		self.assertTrue(np.all(np.isclose(S @ U, U @ Lambda)))
Esempio n. 4
0
	def test_large(self):
		x = load_and_center_dataset(mnist_path)
		S = get_covariance(x)
		Lambda, U = get_eig(S, 784)

		self.assertEqual(np.shape(Lambda), (784, 784))
		# Check that Lambda is diagonal
		self.assertEqual(np.count_nonzero(
				Lambda - np.diag(np.diagonal(Lambda))), 0)
		# Check that Lambda is sorted in decreasing order
		self.assertTrue(np.all(np.equal(np.diagonal(Lambda),
				sorted(np.diagonal(Lambda), reverse=True))))

		# The eigenvectors should be the columns
		self.assertEqual(np.shape(U), (784, 784))
		self.assertTrue(np.all(np.isclose(S @ U, U @ Lambda)))
Esempio n. 5
0
    a1 = axs[0].imshow(orig.reshape((28, 28)), aspect='equal', cmap='gray')
    axs[0].set_title("Original")

    for i in range(len(projs)):
        a = axs[i + 1].imshow(projs[i].reshape((28, 28)),
                              aspect='equal',
                              cmap='gray')
        axs[i + 1].set_title(titles[i])

    plt.show()


if __name__ == '__main__':
    data = pca.load_and_center_dataset("mnist.npy")
    cov = pca.get_covariance(data)

    _, eig10 = pca.get_eig(cov, 10)
    _, eig25 = pca.get_eig(cov, 25)
    _, eig100 = pca.get_eig(cov, 100)
    _, eig250 = pca.get_eig(cov, 250)
    _, eig500 = pca.get_eig(cov, 500)

    image = data[782]
    projs = (pca.project_image(image,
                               eig500), pca.project_image(image, eig250),
             pca.project_image(image, eig100), pca.project_image(image, eig25),
             pca.project_image(image, eig10))
    titles = ("n_components=500", "n_components=250", "n_components=100",
              "n_components=25", "n_components=10")

    display_multiple_images(image, projs, titles)