forked from scikit-learn/scikit-learn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench_pca_solvers.py
165 lines (151 loc) · 5.68 KB
/
bench_pca_solvers.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# %%
#
# This benchmark compares the speed of PCA solvers on datasets of different
# sizes in order to determine the best solver to select by default via the
# "auto" heuristic.
#
# Note: we do not control for the accuracy of the solvers: we assume that all
# solvers yield transformed data with similar explained variance. This
# assumption is generally true, except for the randomized solver that might
# require more power iterations.
#
# We generate synthetic data with dimensions that are useful to plot:
# - time vs n_samples for a fixed n_features and,
# - time vs n_features for a fixed n_samples for a fixed n_features.
import itertools
from math import log10
from time import perf_counter
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn import config_context
from sklearn.decomposition import PCA
REF_DIMS = [100, 1000, 10_000]
data_shapes = []
for ref_dim in REF_DIMS:
data_shapes.extend([(ref_dim, 10**i) for i in range(1, 8 - int(log10(ref_dim)))])
data_shapes.extend(
[(ref_dim, 3 * 10**i) for i in range(1, 8 - int(log10(ref_dim)))]
)
data_shapes.extend([(10**i, ref_dim) for i in range(1, 8 - int(log10(ref_dim)))])
data_shapes.extend(
[(3 * 10**i, ref_dim) for i in range(1, 8 - int(log10(ref_dim)))]
)
# Remove duplicates:
data_shapes = sorted(set(data_shapes))
print("Generating test datasets...")
rng = np.random.default_rng(0)
datasets = [rng.normal(size=shape) for shape in data_shapes]
# %%
def measure_one(data, n_components, solver, method_name="fit"):
print(
f"Benchmarking {solver=!r}, {n_components=}, {method_name=!r} on data with"
f" shape {data.shape}"
)
pca = PCA(n_components=n_components, svd_solver=solver, random_state=0)
timings = []
elapsed = 0
method = getattr(pca, method_name)
with config_context(assume_finite=True):
while elapsed < 0.5:
tic = perf_counter()
method(data)
duration = perf_counter() - tic
timings.append(duration)
elapsed += duration
return np.median(timings)
SOLVERS = ["full", "covariance_eigh", "arpack", "randomized", "auto"]
measurements = []
for data, n_components, method_name in itertools.product(
datasets, [2, 50], ["fit", "fit_transform"]
):
if n_components >= min(data.shape):
continue
for solver in SOLVERS:
if solver == "covariance_eigh" and data.shape[1] > 5000:
# Too much memory and too slow.
continue
if solver in ["arpack", "full"] and log10(data.size) > 7:
# Too slow, in particular for the full solver.
continue
time = measure_one(data, n_components, solver, method_name=method_name)
measurements.append(
{
"n_components": n_components,
"n_samples": data.shape[0],
"n_features": data.shape[1],
"time": time,
"solver": solver,
"method_name": method_name,
}
)
measurements = pd.DataFrame(measurements)
measurements.to_csv("bench_pca_solvers.csv", index=False)
# %%
all_method_names = measurements["method_name"].unique()
all_n_components = measurements["n_components"].unique()
for method_name in all_method_names:
fig, axes = plt.subplots(
figsize=(16, 16),
nrows=len(REF_DIMS),
ncols=len(all_n_components),
sharey=True,
constrained_layout=True,
)
fig.suptitle(f"Benchmarks for PCA.{method_name}, varying n_samples", fontsize=16)
for row_idx, ref_dim in enumerate(REF_DIMS):
for n_components, ax in zip(all_n_components, axes[row_idx]):
for solver in SOLVERS:
if solver == "auto":
style_kwargs = dict(linewidth=2, color="black", style="--")
else:
style_kwargs = dict(style="o-")
ax.set(
title=f"n_components={n_components}, n_features={ref_dim}",
ylabel="time (s)",
)
measurements.query(
"n_components == @n_components and n_features == @ref_dim"
" and solver == @solver and method_name == @method_name"
).plot.line(
x="n_samples",
y="time",
label=solver,
logx=True,
logy=True,
ax=ax,
**style_kwargs,
)
# %%
for method_name in all_method_names:
fig, axes = plt.subplots(
figsize=(16, 16),
nrows=len(REF_DIMS),
ncols=len(all_n_components),
sharey=True,
)
fig.suptitle(f"Benchmarks for PCA.{method_name}, varying n_features", fontsize=16)
for row_idx, ref_dim in enumerate(REF_DIMS):
for n_components, ax in zip(all_n_components, axes[row_idx]):
for solver in SOLVERS:
if solver == "auto":
style_kwargs = dict(linewidth=2, color="black", style="--")
else:
style_kwargs = dict(style="o-")
ax.set(
title=f"n_components={n_components}, n_samples={ref_dim}",
ylabel="time (s)",
)
measurements.query(
"n_components == @n_components and n_samples == @ref_dim "
" and solver == @solver and method_name == @method_name"
).plot.line(
x="n_features",
y="time",
label=solver,
logx=True,
logy=True,
ax=ax,
**style_kwargs,
)
# %%