2011-07-26 09:41:48 +08:00
|
|
|
"""Benchmarks of Lasso regularization path computation using Lars and CD
|
2010-12-12 10:42:48 +08:00
|
|
|
|
|
|
|
|
The input data is mostly low rank but is a fat infinite tail.
|
|
|
|
|
"""
|
2013-02-12 06:11:57 +08:00
|
|
|
from collections import defaultdict
|
2010-12-12 10:42:48 +08:00
|
|
|
import gc
|
|
|
|
|
import sys
|
2013-02-12 06:11:57 +08:00
|
|
|
from time import time
|
2010-12-12 10:42:48 +08:00
|
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
|
2019-03-06 15:06:47 +08:00
|
|
|
from sklearn.linear_model import lars_path, lars_path_gram
|
2011-09-03 18:57:53 +08:00
|
|
|
from sklearn.linear_model import lasso_path
|
2019-10-28 05:17:23 +08:00
|
|
|
from sklearn.datasets import make_regression
|
2010-12-12 10:42:48 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def compute_bench(samples_range, features_range):
|
|
|
|
|
|
|
|
|
|
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("====================")
|
2010-12-12 10:42:48 +08:00
|
|
|
dataset_kwargs = {
|
2011-08-04 21:09:32 +08:00
|
|
|
"n_samples": n_samples,
|
2010-12-12 10:42:48 +08:00
|
|
|
"n_features": n_features,
|
2018-04-26 00:37:56 +08:00
|
|
|
"n_informative": n_features // 10,
|
2010-12-12 10:42:48 +08:00
|
|
|
"effective_rank": min(n_samples, n_features) / 10,
|
2021-06-12 23:14:09 +08:00
|
|
|
# 'effective_rank': None,
|
2010-12-12 10:42:48 +08:00
|
|
|
"bias": 0.0,
|
|
|
|
|
}
|
2013-02-12 06:11:57 +08:00
|
|
|
print("n_samples: %d" % n_samples)
|
|
|
|
|
print("n_features: %d" % n_features)
|
2011-08-04 21:09:32 +08:00
|
|
|
X, y = make_regression(**dataset_kwargs)
|
2010-12-12 10:42:48 +08:00
|
|
|
|
|
|
|
|
gc.collect()
|
2013-05-28 14:27:20 +08:00
|
|
|
print("benchmarking lars_path (with Gram):", end="")
|
2010-12-12 10:42:48 +08:00
|
|
|
sys.stdout.flush()
|
|
|
|
|
tstart = time()
|
2011-12-17 03:18:40 +08:00
|
|
|
G = np.dot(X.T, X) # precomputed Gram matrix
|
2010-12-12 10:42:48 +08:00
|
|
|
Xy = np.dot(X.T, y)
|
2019-03-06 15:06:47 +08:00
|
|
|
lars_path_gram(Xy=Xy, Gram=G, n_samples=y.size, method="lasso")
|
2010-12-12 10:42:48 +08:00
|
|
|
delta = time() - tstart
|
2013-02-12 06:11:57 +08:00
|
|
|
print("%0.3fs" % delta)
|
2010-12-12 10:42:48 +08:00
|
|
|
results["lars_path (with Gram)"].append(delta)
|
|
|
|
|
|
|
|
|
|
gc.collect()
|
2013-05-28 14:27:20 +08:00
|
|
|
print("benchmarking lars_path (without Gram):", end="")
|
2010-12-12 10:42:48 +08:00
|
|
|
sys.stdout.flush()
|
|
|
|
|
tstart = time()
|
|
|
|
|
lars_path(X, y, method="lasso")
|
|
|
|
|
delta = time() - tstart
|
2013-02-12 06:11:57 +08:00
|
|
|
print("%0.3fs" % delta)
|
2010-12-12 10:42:48 +08:00
|
|
|
results["lars_path (without Gram)"].append(delta)
|
|
|
|
|
|
|
|
|
|
gc.collect()
|
2013-05-28 14:27:20 +08:00
|
|
|
print("benchmarking lasso_path (with Gram):", end="")
|
2010-12-12 10:42:48 +08:00
|
|
|
sys.stdout.flush()
|
|
|
|
|
tstart = time()
|
|
|
|
|
lasso_path(X, y, precompute=True)
|
|
|
|
|
delta = time() - tstart
|
2013-02-12 06:11:57 +08:00
|
|
|
print("%0.3fs" % delta)
|
2010-12-12 10:42:48 +08:00
|
|
|
results["lasso_path (with Gram)"].append(delta)
|
|
|
|
|
|
|
|
|
|
gc.collect()
|
2013-05-28 14:27:20 +08:00
|
|
|
print("benchmarking lasso_path (without Gram):", end="")
|
2010-12-12 10:42:48 +08:00
|
|
|
sys.stdout.flush()
|
|
|
|
|
tstart = time()
|
|
|
|
|
lasso_path(X, y, precompute=False)
|
|
|
|
|
delta = time() - tstart
|
2013-02-12 06:11:57 +08:00
|
|
|
print("%0.3fs" % delta)
|
2010-12-12 10:42:48 +08:00
|
|
|
results["lasso_path (without Gram)"].append(delta)
|
|
|
|
|
|
|
|
|
|
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-12-12 10:42:48 +08:00
|
|
|
import matplotlib.pyplot as plt
|
|
|
|
|
|
2020-06-24 22:51:51 +08:00
|
|
|
samples_range = np.linspace(10, 2000, 5).astype(int)
|
|
|
|
|
features_range = np.linspace(10, 2000, 5).astype(int)
|
2010-12-12 10:42:48 +08:00
|
|
|
results = compute_bench(samples_range, features_range)
|
|
|
|
|
|
2012-04-05 06:03:42 +08:00
|
|
|
max_time = max(max(t) for t in results.values())
|
2010-12-12 10:42:48 +08:00
|
|
|
|
2013-05-28 14:27:20 +08:00
|
|
|
fig = plt.figure("scikit-learn Lasso path benchmark results")
|
2010-12-12 10:42:48 +08:00
|
|
|
i = 1
|
2013-02-04 04:23:42 +08:00
|
|
|
for c, (label, timings) in zip("bcry", sorted(results.items())):
|
2010-12-12 10:42:48 +08:00
|
|
|
ax = fig.add_subplot(2, 2, i, projection="3d")
|
|
|
|
|
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.T, cstride=1, rstride=1, color=c, alpha=0.8)
|
|
|
|
|
|
|
|
|
|
# dummy point plot to stick the legend to since surface plot do not
|
|
|
|
|
# support legends (yet?)
|
2016-06-11 06:30:26 +08:00
|
|
|
# ax.plot([1], [1], [1], color=c, label=label)
|
2010-12-12 10:42:48 +08:00
|
|
|
|
|
|
|
|
ax.set_xlabel("n_samples")
|
|
|
|
|
ax.set_ylabel("n_features")
|
2013-05-28 14:27:20 +08:00
|
|
|
ax.set_zlabel("Time (s)")
|
2010-12-12 10:42:48 +08:00
|
|
|
ax.set_zlim3d(0.0, max_time * 1.1)
|
|
|
|
|
ax.set_title(label)
|
2016-06-11 06:30:26 +08:00
|
|
|
# ax.legend()
|
2010-12-12 10:42:48 +08:00
|
|
|
i += 1
|
|
|
|
|
plt.show()
|