[Numpy-discussion] Setting small numbers to zero.
josef.pktd@gmai...
josef.pktd@gmai...
Wed Mar 17 14:26:41 CDT 2010
On Wed, Mar 17, 2010 at 3:11 PM, Robert Kern <robert.kern@gmail.com> wrote:
> On Wed, Mar 17, 2010 at 14:07, <josef.pktd@gmail.com> wrote:
>> On Wed, Mar 17, 2010 at 3:01 PM, Robert Kern <robert.kern@gmail.com> wrote:
>>> On Wed, Mar 17, 2010 at 14:04, Christopher Barker <Chris.Barker@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.
>>>>
>>>> And numpy.clip() may be helpful here,
>>>
>>> No, it's not.
>>
>> why not?
>>
>> np.clip([-1,-5,1,1e90,np.inf, np.nan], 0, np.inf)
>> array([ 0.00000000e+00, 0.00000000e+00, 1.00000000e+00,
>> 1.00000000e+90, Inf, NaN])
>
> Why do you think that's equivalent? That just clips negative numbers
> to 0. The question is how to set numbers of small absolute magnitude
> to 0.
my mistake, that's the reason scipy.stats.threshold still exists
>>> stats.threshold([-1,0.05,1e-4,1, np.inf, np.nan],0.1,np.inf)
array([ 0., 0., 0., 1., Inf, NaN])
Josef
>
> --
> 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
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion@scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
More information about the NumPy-Discussion
mailing list