I've got a numpy array. What is the fastest way to compute all the permutations of orderings.
What I mean is, given the first element in my array, I want a list of all the elements that sequentially follow it. Then given the second element, a list of all the elements that follow it.
So given my list: b, c, & d follow a. c & d follow b, and d follows c.
x = np.array(["a", "b", "c", "d"])
So a potential output looks like:
[
["a","b"],
["a","c"],
["a","d"],
["b","c"],
["b","d"],
["c","d"],
]
I will need to do this several million times so I am looking for an efficient solution.
I tried something like:
im = np.vstack([x]*len(x))
a = np.vstack(([im], [im.T])).T
results = a[np.triu_indices(len(x),1)]
but its actually slower than looping...
A permutation can be specified by an array P [] where P [i] represents the location of the element at index i in the permutation. For example, the array [3, 2, 1, 0] represents the permutation that maps the element at index 0 to index 3, the element at index 1 to index 2, the element at index 2 to index 1 and the element at index 3 to index 0.
Parameters of np.random.permutation x: It is an array. If the input in x is an integer value, then it randomly permutes np. arange (x). If the input in x is an array, then it makes a copy and shuffles the elements randomly.
A permutation is a rearrangement of members of a sequence into a new sequence. For example, there are 24 permutations of [a, b, c, d]. Some of them are [b, a, d, c], [d, a, b, c] and [a, d, b, c] . A permutation can be specified by an array P [] where P [i] represents the location of the element at index i in the permutation.
Randomly permute a sequence, or return a permuted range. If x is a multi-dimensional array, it is only shuffled along its first index. New code should use the permutation method of a default_rng () instance instead; please see the Quick Start. If x is an integer, randomly permute np.arange (x) .
You can use itertools
's functions like chain.from_iterable
and combinations
with np.fromiter
for this. This involves no loop in Python, but still not a pure NumPy solution:
>>> from itertools import combinations, chain
>>> arr = np.fromiter(chain.from_iterable(combinations(x, 2)), dtype=x.dtype)
>>> arr.reshape(arr.size/2, 2)
array([['a', 'b'],
['a', 'c'],
['a', 'd'],
...,
['b', 'c'],
['b', 'd'],
['c', 'd']],
dtype='|S1')
Timing comparisons:
>>> x = np.array(["a", "b", "c", "d"]*100)
>>> %%timeit
im = np.vstack([x]*len(x))
a = np.vstack(([im], [im.T])).T
results = a[np.triu_indices(len(x),1)]
...
10 loops, best of 3: 29.2 ms per loop
>>> %%timeit
arr = np.fromiter(chain.from_iterable(combinations(x, 2)), dtype=x.dtype)
arr.reshape(arr.size/2, 2)
...
100 loops, best of 3: 6.63 ms per loop
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With