Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

efficiently compute ordering permutations in numpy array

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...

like image 844
JoeDanger Avatar asked Dec 06 '14 18:12

JoeDanger


People also ask

How do you use a permutation in an array?

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.

What are the parameters of NumPy random permutation?

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.

How many permutations are there in Python?

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.

How do you randomly permute an array in Python?

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) .


1 Answers

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
like image 127
Ashwini Chaudhary Avatar answered Nov 14 '22 21:11

Ashwini Chaudhary