-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.py
33 lines (25 loc) · 832 Bytes
/
metrics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import numpy as np
from sklearn.decomposition import PCA, TruncatedSVD
import matplotlib.pyplot as plt
def graph_reconstruction(X, delta = 10, max_components = 1000, print_progress = False):
frobenii = [np.linalg.norm(X.toarray())]
rnge = [i for i in range(delta, max_components + delta, delta)]
for i in rnge:
dim_reducer = TruncatedSVD(n_components=i)
shrunk = dim_reducer.fit_transform(X)
X_reconst = dim_reducer.inverse_transform(shrunk)
frobenii.append(np.linalg.norm(X - X_reconst))
if (print_progress):
print("{}% complete".format(100 * i / max_components))
components = [0]
components.extend(rnge)
plt.plot(components, frobenii)
plt.show()
return components, frobenii
def graph_eigenvalues(X):
X = X.toarray()
_, s, _ = np.linalg.svd(X)
values = s
plt.plot(values)
plt.show()
return values