Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- """
- Created on Tue Dec 3 15:48:32 2019
- @author: Eric
- """
- import numpy as np
- import seaborn as sns
- import matplotlib
- import matplotlib.pyplot as plt
- sns.set_style("whitegrid")
- blue, = sns.color_palette("muted", 1)
- def plot_mnist(elts, m, n, name = -99):
- """Plot MNIST images in an m by n table. Note that we crop the images
- so that they appear reasonably close together. Note that we are
- passed raw MNIST data and it is reshaped.
- Example: plot_mnist(X_train, 10, 10)
- """
- fig = plt.figure()
- images = [elt.reshape(28, 28) for elt in elts]
- img = np.concatenate([np.concatenate([images[m*y+x] for x in range(m)], axis=1)
- for y in range(n)], axis=0)
- ax = fig.add_subplot(1, 1, 1)
- ax.matshow(img, cmap = matplotlib.cm.binary)
- for spine in plt.gca().spines.values():
- spine.set_visible(False)
- plt.xticks(np.array([]))
- plt.yticks(np.array([]))
- plt.show()
- if (name > -2):
- title = "fig_"
- name = str(name)
- title = title + name + ".png"
- plt.savefig(title)
- def PCA(X):
- n,m = X.shape
- #assert np.allclose(X.mean(axis=0),np.zeros(m))
- #Data is centered, Get Covariance
- coVar = np.dot(X.T, X) / (n -1)
- #coVar = np.cov(X)
- #get eigens
- eVal, eVec = np.linalg.eig(coVar)
- xPca = np.dot(X, eVec)
- return xPca, eVal, eVec
- def main():
- X_train = np.load('../Data/X_train.npy')
- X_val = np.load('../Data/X_val.npy')
- Y_train = np.load('../Data/Y_train.npy')
- #X_train = X_train.astype(np.float64)
- m = X_train.mean(axis = 0)
- X = X_train - m
- #print(X_train.shape)
- #print(Y_train.shape)
- """
- meanImage = m.reshape(28,28)
- plt.axis('off')
- plt.imshow(meanImage,cmap = matplotlib.cm.binary)
- """
- avgImages = []
- """
- for i in range(10):
- meanDig = X_train[Y_train == i].mean(axis=0)
- print (meanDig.shape)
- meanDig = meanDig.reshape(28,28)
- avgImages.append(meanDig)
- plot_mnist(avgImages,5,2)
- """
- xPca, eVal, eVec = PCA(X)
- for i in range(5):
- eigImg = eVec[i].reshape(28,28)
- avgImages.append(eigImg)
- plot_mnist(avgImages,5,1)
- """
- print(eVal.shape)
- numPC = np.arange(eVal.shape[0])[:25]
- largest = eVal[:25]
- fig, ax = plt.subplots()
- ax.plot(numPC, largest, color = blue, lw = 3)
- ax.set_ylabel("Eigen Values")
- ax.set_xlabel("PCA Number")
- ax.fill_between(numPC,0,largest, alpha = .25)
- ax.set(xlim=(0, len(numPC) - 1), ylim=(0, None))
- """
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement