#pca svd working
def svd(D):
import time
import numpy
# Record starting time
timestart = time.time()
# Prepare and center the data for PCA
# Reshape from 3D array to 2D array
# from (pics, height, width)
# to (pics, all_pixels)
# here: from (2000, 62, 47) to (2000, 2914)
# then transpose
X = D.reshape(len(D), -1).T
X -= X.mean(axis=1, keepdims=True)
# Singular Value Decomposition
U, L, V = numpy.linalg.svd(X / X.shape[1]**0.5, full_matrices=False)
# Print time
tfinish = time.time()
print('Time in seconds: %.3f' % (tfinish - timestart))
# Visualize first two principal components
utils.scatterplot(U[:,0].dot(X), U[:,1].dot(X), xlabel='PCA 1', ylabel='PCA 2')
# Render first 60 eigenfaces (principal components)
utils.render(U[:,:60].T, 15, 4)
# call the function
svd(D)