[Numpy-discussion] Setting small numbers to zero.
Robert Kern
robert.kern at gmail.com
Wed Mar 17 15:28:04 EDT 2010
On Wed, Mar 17, 2010 at 14:18, Keith Goodman <kwgoodman at gmail.com> wrote:
> On Wed, Mar 17, 2010 at 12:08 PM, Robert Kern <robert.kern at gmail.com> wrote:
>> On Wed, Mar 17, 2010 at 14:03, Keith Goodman <kwgoodman at gmail.com> wrote:
>>> On Wed, Mar 17, 2010 at 12:04 PM, Christopher Barker
>>> <Chris.Barker at noaa.gov> wrote:
>>>> Friedrich Romstedt wrote:
>>>>> Code:
>>>>>
>>>>> import numpy
>>>>> import time
>>>>>
>>>>> a = numpy.random.random((2000, 2000))
>>>>>
>>>>> start = time.time()
>>>>> a[abs(a) < 10] = 0
>>>>> stop = time.time()
>>>>
>>>> I highly recommend ipython and its "timeit" function --much better for this.
>>>
>>> One of the methods, the fast one, uses an in-place operation, so
>>> timeit won't work. But for cases where timeit does work, yes, it is
>>> great.
>>
>> You need to provide correct setup code.
>>
>> from timeit import Timer
>>
>> t = Timer("a[abs(a) < 0.1] = 0", "import
>> numpy;a=numpy.random.random((2000, 2000))")
>> t.repeat()
>
> BTW, is there some way to get the above to give the same output (and
> same number of runs etc) as
>
> timeit a[abs(a) < 0.1] = 0
Much of that is implemented in the implementation of %timeit:
http://bazaar.launchpad.net/~ipython-dev/ipython/trunk/annotate/head:/IPython/core/magic.py#L1755
--
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