[Numpy-discussion] ndarray.count() ?

Robert Kern robert.kern at gmail.com
Thu Sep 7 23:35:06 EDT 2006


rex wrote:
> Robert Kern <robert.kern at gmail.com> [2006-09-07 16:35]:
>> rex wrote:
>>> Charles R Harris <charlesr.harris at gmail.com> [2006-09-07 15:04]:
>>>> I don't know about count, but you can gin up something like this
>>>>
>>>> In [78]: a = ran.randint(0,2, size=(10,))
>>>>
>>>> In [79]: a
>>>> Out[79]: array([0, 1, 0, 1, 1, 0, 0, 1, 1, 1])
>>> This exposed inconsistent randint() behavior between SciPy and the Python
>>> random module. The Python randint includes the upper endpoint. The SciPy
>>> version excludes it.
>> numpy.random.random_integers() includes the upper bound, if you like. 
>> numpy.random does not try to emulate the standard library's random module.
> 
> I'm not in a position to argue the merits, but IMHO, when code that
> previously worked silently starts returning subtly bad results after
> importing numpy, there is a problem. What possible upside is there in
> having randint() behave one way in the random module and silently behave
> differently in numpy? 

I don't understand you. Importing numpy does not change the standard library's 
random module in any way. There is no silent difference in behavior. If you use 
numpy.random you get one set of behavior. If you use random, you get another. 
Pick the one you want. They're not interchangeable, and nothing suggests that 
they ought to be.

> More generally, since numpy.random does not try to emulate the random
> module, how does one convert from code that uses the random module to
> numpy? Is randint() the only silent problem, or are there others? If so,
> how does one discover them? Are they documented anywhere?

The docstrings in that module are complete.

> I deeply appreciate the countless hours the core developers have
> contributed to numpy/scipy, but sometimes I think you are too close to
> the problems to fully appreciate the barriers to widespread adoption such
> silent "gotchas" present. If the code breaks, fine, you know there's a
> problem. When it runs, but returns wrong -- but not obviously wrong --
> results, there's a serious problem that will deter a significant number
> of people from ever trying the product again.
> 
> Again, what is the upside of changing the behavior of the standard
> library's randint() without also changing the name?

Again, numpy.random has nothing to do with the standard library module random. 
The names of the functions match those in the PRNG facilities that used to be in 
Numeric and scipy which numpy.random is replacing. Specifically, 
numpy.random.randint() derives its behavior from Numeric's RandomArray.randint().

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





More information about the NumPy-Discussion mailing list