human
I am working on the file "FACLA/L3-notes.ipynb".
The first task is:
Computational Complexity for a MxN matrix in SVD is $M^2N+N^3$, so Randomized (Truncated?) SVD is a massive improvement.

2018/3/7
Write a loop to calculate the error of your decomposition as your vary the # of topics. Plot the results.
Can you write Python program for it?

=====
Give me a Python program to calculate the error of your decomposition as your vary the  of topics?
assistant
#Here is Python program to calculate the error of your decomposition as your vary the # of topics.

# 1. how do I calculate decomposition error?:
# I guess I'll use MSE?

# # NumPy:    # https://stackoverflow.com/questions/16774849/mean-squared-error-in-numpy
# def MSEnp(A,B):
#     if type(A) == np.ndarray and type(B) == np.ndarray:
#         return ((A - B) ** 2).mean()
#     else:
#         return np.square((A - B)).mean()

# Scikit-Learn:
from sklearn import metrics
MSE = metrics.mean_squared_error  # usg: mse(A,B)

# 2. Now how to recompose my decomposition?:

%time B = vectors  # original matrix
%time U, S, V = randomized_svd(B, 10) # num_topics = 10

# S is vector of Σ's singular values. Convert back to matrix:
%time Σ = S * np.eye(S.shape[0])

# from SVD formula: A ≈ U@Σ@V.T
%time A = U@Σ@V  ## apparently randomized_svd returns V.T, not V ?

# 3. Finally calculated error I guess:

%time mse_error = MSE(A,B)
print(mse_error)

# Im putting way too much effort into this lol
def fib(n):
    if n <= 1:
        return n
    else:
        f1 = 1
        f2 = 0
        for i in range(n):
            t = f1 + f2
            tmp = f2
            f2 += f1
            f1 = tmp
        return t

for i,e in enumerate(num_topics):
    print(f'Topics: {num_topics[i]:>3}    ',
          f'Time: {num_topics[i]:>3}')

## Setup
import time

B = vectors
num_topics = [fib(i) for i in range(2,14)]
TnE = []  # time & error

## Loop:
for n_topics in num_topics:
    t0 = time.time()
    U, S, Vt = randomized_svd(B, n_topics)
    Σ = S * np.eye(S.shape[0])
    A = U@Σ@Vt
    TnE.append([time.time() - t0, MSE(A,B)])
    
for i, tne in enumerate(TnE):
    print(f'Topics: {num_topics[i]:>3}    '
          f'Time: {np.round(tne[0],3):>3}    '
          f'Error: {np.round(tne[1],12):>3}')

# https://matplotlib.org/users/pyplot_tutorial.html
plt.plot(num_topics, [tne[1] for tne in TnE])
plt.xlabel('No. Topics')
plt.ylabel('MSE Error')
plt.show()

## R.Thomas' class solution:
step = 20
n = 20
error = np.zeros(n)

for i in range(n):
    U, s, V = randomized_svd(vectors, i * step)
    reconstructed = U @ np.diag(s) @ V
    error[i]...