\$\begingroup\$
\$\endgroup\$
1
I want to extract the combinations of numpy arrays in this way:
from itertools import combinations
import numpy as np
X = np.array(np.random.randn(4, 6))
combination = np.array([X[:, [i, j]] for i, j in combinations(range(X.shape[1]), 2)])
Is there a way to speed this up? My array has a shape of 200x50000.
Caridorc
28k7 gold badges54 silver badges137 bronze badges
asked Mar 5, 2021 at 13:01
1 Answer 1
\$\begingroup\$
\$\endgroup\$
2
I decided to use an example of shape 20x5000. For which I could reduce the time from ~58 sec to ~3 sec i.e. almost a factor of 20.
def combinations(arr):
n = arr.shape[0]
a = np.broadcast_to(arr, (n, n))
b = np.broadcast_to(arr.reshape(-1,1), (n, n))
upper = np.tri(n, n, -1, dtype='bool').T
return b[upper].reshape(-1), a[upper].reshape(-1)
X = np.array(np.random.randn(20,5000))
%timeit X[:, [*combinations(np.arange(X.shape[1]))]]
%timeit np.array([X[:, [i, j]] for i, j in itertools.combinations(range(X.shape[1]), 2)])
is giving me
3.2 s ± 29.5 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
57.8 s ± 2.35 s per loop (mean ± std. dev. of 7 runs, 1 loop each)
And
combination = np.array([X[:, [i, j]] for i, j in itertools.combinations(range(X.shape[1]), 2)])
np.allclose(combination, np.moveaxis(X[:, [*combinations(np.arange(X.shape[1]))]], -1, 0))
confirms I am calculating the right thing. Just the axes are ordered differently.
-
\$\begingroup\$ Can you explain why your code is better than the original code? \$\endgroup\$2021年07月21日 22:26:00 +00:00Commented Jul 21, 2021 at 22:26
-
2\$\begingroup\$ @pacmaninbw Sure. It's the standard
numpy
approach. You work with whole arrays at once rather than looping over individual entries. So rather than having a slow python interpreter doing the work you can hand it all to highly optimized/pre-compiledc
orfortran
code written by thenumpy
community. \$\endgroup\$Lukas S– Lukas S2021年07月21日 22:45:48 +00:00Commented Jul 21, 2021 at 22:45
Explore related questions
See similar questions with these tags.
lang-py
combinations
. \$\endgroup\$