2013-05-28 14:27:20 +08:00
|
|
|
"""Benchmarks of Singular Value Decomposition (Exact and Approximate)
|
2010-12-06 03:21:08 +08:00
|
|
|
|
|
|
|
|
The data is mostly low rank but is a fat infinite tail.
|
|
|
|
|
"""
|
2010-11-28 23:27:47 +08:00
|
|
|
import gc
|
|
|
|
|
from time import time
|
|
|
|
|
import numpy as np
|
|
|
|
|
from collections import defaultdict
|
|
|
|
|
|
|
|
|
|
from scipy.linalg import svd
|
2011-12-31 21:31:55 +08:00
|
|
|
from sklearn.utils.extmath import randomized_svd
|
2019-10-28 05:17:23 +08:00
|
|
|
from sklearn.datasets import make_low_rank_matrix
|
2010-11-28 23:27:47 +08:00
|
|
|
|
|
|
|
|
|
2012-10-07 21:13:29 +08:00
|
|
|
def compute_bench(samples_range, features_range, n_iter=3, rank=50):
|
2010-11-28 23:27:47 +08:00
|
|
|
|
|
|
|
|
it = 0
|
|
|
|
|
|
|
|
|
|
results = defaultdict(lambda: [])
|
|
|
|
|
|
|
|
|
|
max_it = len(samples_range) * len(features_range)
|
|
|
|
|
for n_samples in samples_range:
|
|
|
|
|
for n_features in features_range:
|
|
|
|
|
it += 1
|
2013-02-12 06:11:57 +08:00
|
|
|
print("====================")
|
|
|
|
|
print("Iteration %03d of %03d" % (it, max_it))
|
|
|
|
|
print("====================")
|
2011-12-17 03:18:40 +08:00
|
|
|
X = make_low_rank_matrix(
|
|
|
|
|
n_samples, n_features, effective_rank=rank, tail_strength=0.2
|
2010-12-06 03:21:08 +08:00
|
|
|
)
|
2010-11-28 23:27:47 +08:00
|
|
|
|
|
|
|
|
gc.collect()
|
2013-05-28 14:27:20 +08:00
|
|
|
print("benchmarking scipy svd: ")
|
2010-11-28 23:27:47 +08:00
|
|
|
tstart = time()
|
|
|
|
|
svd(X, full_matrices=False)
|
|
|
|
|
results["scipy svd"].append(time() - tstart)
|
|
|
|
|
|
|
|
|
|
gc.collect()
|
2013-05-28 14:27:20 +08:00
|
|
|
print("benchmarking scikit-learn randomized_svd: n_iter=0")
|
2010-11-28 23:27:47 +08:00
|
|
|
tstart = time()
|
2012-10-07 21:13:29 +08:00
|
|
|
randomized_svd(X, rank, n_iter=0)
|
|
|
|
|
results["scikit-learn randomized_svd (n_iter=0)"].append(time() - tstart)
|
2010-11-28 23:27:47 +08:00
|
|
|
|
|
|
|
|
gc.collect()
|
2013-05-28 14:27:20 +08:00
|
|
|
print("benchmarking scikit-learn randomized_svd: n_iter=%d " % n_iter)
|
2010-11-28 23:27:47 +08:00
|
|
|
tstart = time()
|
2012-10-07 21:13:29 +08:00
|
|
|
randomized_svd(X, rank, n_iter=n_iter)
|
|
|
|
|
results["scikit-learn randomized_svd (n_iter=%d)" % n_iter].append(
|
|
|
|
|
time() - tstart
|
|
|
|
|
)
|
2010-11-28 23:27:47 +08:00
|
|
|
|
|
|
|
|
return results
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-06-12 23:14:09 +08:00
|
|
|
from mpl_toolkits.mplot3d import axes3d # noqa register the 3d projection
|
2010-11-28 23:27:47 +08:00
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
|
|
2020-06-24 22:51:51 +08:00
|
|
|
samples_range = np.linspace(2, 1000, 4).astype(int)
|
|
|
|
|
features_range = np.linspace(2, 1000, 4).astype(int)
|
2010-12-06 03:21:08 +08:00
|
|
|
results = compute_bench(samples_range, features_range)
|
2010-11-28 23:27:47 +08:00
|
|
|
|
2013-05-28 14:27:20 +08:00
|
|
|
label = "scikit-learn singular value decomposition benchmark results"
|
|
|
|
|
fig = plt.figure(label)
|
2010-11-28 23:27:47 +08:00
|
|
|
ax = fig.gca(projection="3d")
|
2019-01-03 21:50:05 +08:00
|
|
|
for c, (label, timings) in zip("rbg", sorted(results.items())):
|
2010-11-28 23:27:47 +08:00
|
|
|
X, Y = np.meshgrid(samples_range, features_range)
|
|
|
|
|
Z = np.asarray(timings).reshape(samples_range.shape[0], features_range.shape[0])
|
|
|
|
|
# plot the actual surface
|
|
|
|
|
ax.plot_surface(X, Y, Z, rstride=8, cstride=8, alpha=0.3, color=c)
|
|
|
|
|
# dummy point plot to stick the legend to since surface plot do not
|
|
|
|
|
# support legends (yet?)
|
|
|
|
|
ax.plot([1], [1], [1], color=c, label=label)
|
|
|
|
|
|
|
|
|
|
ax.set_xlabel("n_samples")
|
|
|
|
|
ax.set_ylabel("n_features")
|
2013-05-28 14:27:20 +08:00
|
|
|
ax.set_zlabel("Time (s)")
|
2010-11-28 23:27:47 +08:00
|
|
|
ax.legend()
|
|
|
|
|
plt.show()
|