<br><br><div class="gmail_quote">On Fri, Dec 3, 2010 at 6:31 AM, Mario Moura <span dir="ltr"><<a href="mailto:moura.mario@gmail.com">moura.mario@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">

Hi Folks<br>
<br>
I have this situation<br>
<br>
>>> from timeit import Timer<br>
>>> reps = 5<br>
>>><br>
>>> t = Timer('itertools.combinations(range(1,10),3)', 'import itertools')<br>
>>> print sum(t.repeat(repeat=reps, number=1)) / reps<br>
1.59740447998e-05<br>
>>> t = Timer('itertools.combinations(range(1,100),3)', 'import itertools')<br>
>>> print sum(t.repeat(repeat=reps, number=1)) / reps<br>
1.74999237061e-05<br>
>>><br>
>>> t = Timer('list(itertools.combinations(range(1,10),3))', 'import itertools')<br>
>>> print sum(t.repeat(repeat=reps, number=1)) / reps<br>
5.31673431396e-05<br>
>>> t = Timer('list(itertools.combinations(range(1,100),3))', 'import itertools')<br>
>>> print sum(t.repeat(repeat=reps, number=1)) / reps<br>
0.0556231498718<br>
>>><br>
<br>
You can see list(itertools.combinations(range(1,100),3)) is terrible!!<br>
<br>
If you change to range(1,100000) your computer will lock.<br>
<br>
So I would like to know a good way to convert <itertools.combinations<br>
object> to ndarray? fast! without use list<br>
Is it possible?<br>
<br>
>>> x = itertools.combinations(range(1,10),3)<br>
>>> x<br>
<itertools.combinations object at 0x25f1520><br>
>>><br>
<br>
I tried this from<br>
<a href="http://docs.python.org/library/itertools.html?highlight=itertools#itertools.combinations" target="_blank">http://docs.python.org/library/itertools.html?highlight=itertools#itertools.combinations</a><br>
<br>
>>> numpy.fromiter(itertools.combinations(range(1,10),3), int, count=-1)<br>
Traceback (most recent call last):<br>
  File "<stdin>", line 1, in <module><br>
ValueError: setting an array element with a sequence.<br>
>>><br>
<br>
and this from<br>
<a href="http://docs.python.org/library/itertools.html?highlight=itertools#itertools.combinations" target="_blank">http://docs.python.org/library/itertools.html?highlight=itertools#itertools.combinations</a><br>
<br>
import numpy<br>
from itertools import *<br>
from numpy import *<br>
<br>
def combinations(iterable, r):<br>
    pool = tuple(iterable)<br>
    n = len(pool)<br>
    for indices in permutations(range(n), r):<br>
        if sorted(indices) == list(indices):<br>
            yield tuple(pool[i] for i in indices)<br>
<br>
<br>
numpy.fromiter(combinations(range(1,10),3), int, count=-1)<br>
<br>
>>> numpy.fromiter(combinations(range(1,10),3), int, count=-1)<br>
Traceback (most recent call last):<br>
  File "<stdin>", line 1, in <module><br>
ValueError: setting an array element with a sequence.<br>
>>><br>
<br>
<br>
I like itertools.combinations performance but I need convert it to numpy.<br>
<br></blockquote><div><br><br>The docstring for numpy.fromiter() says it creates a 1D array.  You can use it with itertools.combinations if you specify a dtype for a 1D  structured array.  Here's an example (I'm using ipython with the -pylab option, so the numpy functions have all been imported):<br>

<br><br>In [1]: from itertools import combinations<br><br>In [2]: dt = dtype('i,i,i')<br><br>In [3]: a = fromiter(combinations(range(100),3), dtype=dt, count=-1)<br><br>In [4]: b = array(list(combinations(range(100),3)))<br>

<br>In [5]: all(a.view(int).reshape(-1,3) == b)<br>Out[5]: True<br><br>In [6]: timeit a = fromiter(combinations(range(100),3), dtype=dt, count=-1)<br>10 loops, best of 3: 92.7 ms per loop<br><br>In [7]: timeit b = array(list(combinations(range(100),3)))<br>

1 loops, best of 3: 627 ms per loop<br><br>In [8]: a[:3]<br>Out[8]: <br>array([(0, 1, 2), (0, 1, 3), (0, 1, 4)], <br>      dtype=[('f0', '<i4'), ('f1', '<i4'), ('f2', '<i4')])<br>

<br>In [9]: b[:3]<br>Out[9]: <br>array([[0, 1, 2],<br>       [0, 1, 3],<br>       [0, 1, 4]])<br><br><br>In the above example, 'a' is a 1D structured array; each element of 'a' holds one of the combinations.  If you need it, you can create a 2D view with a.view(int).reshape(-1,3).<br>

<br>Warren<br><br></div></div>