Discussion:
[Numpy-discussion] numpy.random.permutation bug?
Emanuele Olivetti
2007-01-18 17:09:54 UTC
Permalink
Look at this:

----------bug.py-------
import numpy
a=numpy.array([1,2])
b=a.sum()
print type(b)
c=numpy.random.permutation(b)
-----------------------

If I run it (Python 2.5, numpy 1.0.1 on a Linux box) I get:
-----------
#> python /tmp/bug.py
<type 'numpy.int32'>
Traceback (most recent call last):
File "/tmp/bug.py", line 5, in <module>
c=numpy.random.permutation(b)
File "mtrand.pyx", line 1227, in mtrand.RandomState.permutation
File "mtrand.pyx", line 1211, in mtrand.RandomState.shuffle
TypeError: len() of unsized object
-----------

permutation() likes 'int' and dislikes 'numpy.int32' integers :(
Seems a bug.

HTH,

Emanuele
Keith Goodman
2007-01-18 17:20:00 UTC
Permalink
Post by Emanuele Olivetti
----------bug.py-------
import numpy
a=numpy.array([1,2])
b=a.sum()
print type(b)
c=numpy.random.permutation(b)
-----------------------
-----------
#> python /tmp/bug.py
<type 'numpy.int32'>
File "/tmp/bug.py", line 5, in <module>
c=numpy.random.permutation(b)
File "mtrand.pyx", line 1227, in mtrand.RandomState.permutation
File "mtrand.pyx", line 1211, in mtrand.RandomState.shuffle
TypeError: len() of unsized object
-----------
permutation() likes 'int' and dislikes 'numpy.int32' integers :(
Seems a bug.
numpy.random.permutation(1.2)
TypeError: len() of unsized object
Robert Kern
2007-01-18 17:31:39 UTC
Permalink
Post by Emanuele Olivetti
numpy.random.permutation(1.2)
TypeError: len() of unsized object
Well, that should raise an error, though maybe not that error.
--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
Keith Goodman
2007-01-18 17:37:42 UTC
Permalink
Post by Robert Kern
Post by Emanuele Olivetti
numpy.random.permutation(1.2)
TypeError: len() of unsized object
Well, that should raise an error, though maybe not that error.
Is my example any different than the one given by Emanuele? He passed
in a.sum().
Post by Robert Kern
Post by Emanuele Olivetti
numpy.random.permutation(1)
array([0])
Post by Robert Kern
Post by Emanuele Olivetti
numpy.random.permutation(1.2)
TypeError: len() of unsized object
Robert Kern
2007-01-18 17:51:27 UTC
Permalink
Post by Keith Goodman
Post by Robert Kern
Post by Emanuele Olivetti
numpy.random.permutation(1.2)
TypeError: len() of unsized object
Well, that should raise an error, though maybe not that error.
Is my example any different than the one given by Emanuele? He passed
in a.sum().
Post by Robert Kern
Post by Emanuele Olivetti
numpy.random.permutation(1)
array([0])
Post by Robert Kern
Post by Emanuele Olivetti
numpy.random.permutation(1.2)
TypeError: len() of unsized object
Yes. 1.2 is not an integer. In Emanuele's example, a.sum() was an integer,
though not an int.
--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
Keith Goodman
2007-01-18 17:57:49 UTC
Permalink
Post by Robert Kern
Yes. 1.2 is not an integer. In Emanuele's example, a.sum() was an integer,
though not an int.
Sorry, but I'm still confused. Does that mean that this is the
expected behaviour?
Post by Robert Kern
Post by Emanuele Olivetti
numpy.random.permutation(1)
array([0])

whereas
Post by Robert Kern
Post by Emanuele Olivetti
numpy.random.permutation(1.2)
should raise an error?
Keith Goodman
2007-01-18 17:59:18 UTC
Permalink
Post by Keith Goodman
Post by Robert Kern
Yes. 1.2 is not an integer. In Emanuele's example, a.sum() was an integer,
though not an int.
Sorry, but I'm still confused. Does that mean that this is the
expected behaviour?
Post by Robert Kern
Post by Emanuele Olivetti
numpy.random.permutation(1)
array([0])
whereas
Post by Robert Kern
Post by Emanuele Olivetti
numpy.random.permutation(1.2)
should raise an error?
Crap. I should have read the doc string:

Type: builtin_function_or_method
Base Class: <type 'builtin_function_or_method'>
String Form: <built-in method permutation of mtrand.RandomState
object at 0xb77abbb4>
Namespace: Interactive
Docstring:
Given an integer, return a shuffled sequence of integers >= 0 and
< x; given a sequence, return a shuffled array copy.

permutation(x)

Alan G Isaac
2007-01-18 17:23:39 UTC
Permalink
Confirmed for numpy 1.0.

Cheers,
Alan Isaac
Robert Kern
2007-01-18 17:31:50 UTC
Permalink
Post by Emanuele Olivetti
permutation() likes 'int' and dislikes 'numpy.int32' integers :(
Seems a bug.
Yup. I should get around to fixing it later tonight.
--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
Emanuele Olivetti
2007-01-18 17:34:04 UTC
Permalink
Post by Robert Kern
Post by Emanuele Olivetti
permutation() likes 'int' and dislikes 'numpy.int32' integers :(
Seems a bug.
Yup. I should get around to fixing it later tonight.
Wow. Superfast! :)

Emanuele
Loading...