[SOLVED] Python, Numpy: all UNIQUE combinations of a numpy.array() vector

Issue

I want to get all unique combinations of a numpy.array vector (or a pandas.Series). I used itertools.combinations but it’s very slow. For an array of size (1000,) it takes many hours. Here is my code using itertools (actually I use combination differences):

def a(array):
    temp = pd.Series([])
    for i in itertools.combinations(array, 2):
        temp = temp.append(pd.Series(np.abs(i[0]-i[1])))
    temp.index=range(len(temp))
    return temp

As you see there is no repetition!!
The sklearn.utils.extmath.cartesian is really fast and good but it provides repetitions which I do not want! I need help rewriting above function without using itertools and much more speed for large vectors.

Solution

For a random array of ints:

import numpy as np
import pandas as pd
import itertools as it

b = np.random.randint(0, 8, ((6,)))
# array([7, 0, 6, 7, 1, 5])
pd.Series(list(it.combinations(np.unique(b), 2)))

it returns:

0    (0, 1)
1    (0, 5)
2    (0, 6)
3    (0, 7)
4    (1, 5)
5    (1, 6)
6    (1, 7)
7    (5, 6)
8    (5, 7)
9    (6, 7)
dtype: object

Answered By – atomh33ls

Answer Checked By – Terry (BugsFixing Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *