import matplotlib.pyplot as plt
import numpy as np
import h5py

def PCA(X, target_pct = 0.99, k = -1):
	'''
		X has dimension m x n.
		Generate principal components of the data.
	'''
	# zero out the mean
	m, n = X.shape
	mu = X.mean(axis = 0).reshape(1, -1)
	X = X - np.repeat(mu, m, axis = 0)
	# unit variance
	# var = np.multiply(X, X).mean(axis = 0)
	# std = np.sqrt(var).reshape(1, -1)
	# X = np.nan_to_num(np.divide(X, np.repeat(std, m, axis = 0)))
	# svd
	U, S, V = np.linalg.svd(X.transpose() * X)
	if k == -1:
		# calculate target k
		total_var = sum(S ** 2)
		accum = 0.
		k = 0
		while k < len(S):
			accum += S[k] ** 2
			if accum / total_var >= target_pct:
				break
			k += 1
	# projection
	X_rot = X * U[:, :k + 1]
	return X_rot, S ** 2, k

def PCA_analysis(D, title = 'Relative Variance Preservation', savename = 'variance.png'):
	'''
		Generate variance preservation analysis of the PCA.
	'''
	total_var = sum(D)
	D /= total_var
	plt.style.use('ggplot')
	plt.title(title)
	plt.plot(range(len(D)), D)
	plt.xlabel("Order of eigenvalue")
	plt.ylabel("Percentage of variance")
	plt.savefig(savename, format = 'png')
	plt.close()


print('==> Experiment 2a')
filepath = '../taylorswift_out/s71d7.mat'
print('==> Loading data from {}'.format(filepath))
# benchmark
t_start = time.time()

# reading data
f = h5py.File(filepath)
X_train = np.array(f.get('trainingFeatures'))
y_train = np.array(f.get('trainingLabels'))
X_val = np.array(f.get('validationFeatures'))
y_val = np.array(f.get('validationLabels'))
print('--Time elapsed for loading data: {t:.2f} \
    seconds'.format(t = t_end - t_start))
del f
print('-- Number of training samples: {}'.format(X_train.shape[0]))
print('-- Number of test samples: {}'.format(X_val.shape[0]))

# running pca
X_rot_col, D_col, k_col = PCA(X_train.transpose())
X_rot_row, D_row, k_row = PCA(X_train)

print('-- Number of features for column vectors is {}'.format(k_col))
print('-- Number of features for row vectors is {}'.format(k_row))

print('==> Generating plots...')
PCA_analysis(D_col, title='Variance Retention for Columns', savename='col_pca.png')
PCA_analysis(D_row, title='Variance Retention for Rows', savename='row_pca.png')

@@ -44,16 +44,34 @@
 y as np%0A
+import scipy.io%0A# 
 import h
@@ -503,21 +503,11 @@
 d(X.
-transpose() *
+T @
  X)%0A
@@ -1267,42 +1267,8 @@
 th))
-%0A# benchmark%0At_start = time.time()
 %0A%0A# 
@@ -1288,17 +1288,24 @@
 f = 
-h5py.File
+scipy.io.loadmat
 (fil
@@ -1495,99 +1495,8 @@
 '))%0A
-print('--Time elapsed for loading data: %7Bt:.2f%7D %5C%0A    seconds'.format(t = t_end - t_start))
 %0Adel
@@ -1685,19 +1685,9 @@
 ain.
-transpose()
+T
 )%0AX_

changing pca to python3