How can i modified all the values of a numpy array whose value is smaller than a given epsilon to zero? Example epsilon=0.01 a = [[0.003,2][23,0.0001]] output: [[0,2][23,0]]  View this message in context: http://old.nabble.com/Settingsmallnumberstozero.tp27933569p27933569.htm... Sent from the Numpydiscussion mailing list archive at Nabble.com.
On Wed, Mar 17, 2010 at 8:51 AM, gerardob <gberbeglia@gmail.com> wrote:
How can i modified all the values of a numpy array whose value is smaller than a given epsilon to zero?
Example epsilon=0.01 a = [[0.003,2][23,0.0001]]
output: [[0,2][23,0]]
Give this a try:
import numpy as np epsilon=0.01 a=np.array([[0.003,2],[23,0.0001]]) a array([[ 3.00000000e03, 2.00000000e+00], [ 2.30000000e+01, 1.00000000e04]]) a[np.abs(a) < epsilon] = 0 a array([[ 0., 2.], [ 23., 0.]])
On Wed, Mar 17, 2010 at 8:51 AM, gerardob <gberbeglia@gmail.com> wrote:
How can i modified all the values of a numpy array whose value is smaller than a given epsilon to zero?
Example epsilon=0.01 a = [[0.003,2][23,0.0001]]
output: [[0,2][23,0]]
Here's one way:
a = np.array([[0.003,2],[23,0.0001]]) a[np.abs(a) < 0.01] = 0 a
array([[ 0., 2.], [ 23., 0.]])
On Wed, Mar 17, 2010 at 11:56 AM, Keith Goodman <kwgoodman@gmail.com> wrote:
On Wed, Mar 17, 2010 at 8:51 AM, gerardob <gberbeglia@gmail.com> wrote:
How can i modified all the values of a numpy array whose value is smaller than a given epsilon to zero?
Example epsilon=0.01 a = [[0.003,2][23,0.0001]]
output: [[0,2][23,0]]
Here's one way:
a = np.array([[0.003,2],[23,0.0001]]) a[np.abs(a) < 0.01] = 0 a
array([[ 0., 2.], [ 23., 0.]]) _______________________________________________ NumPyDiscussion mailing list NumPyDiscussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpydiscussion
But as Skipper said before You might find these helpful. http://www.scipy.org/Tentative_NumPy_Tutorial http://www.scipy.org/Numpy_Example_List Josef
Code: import numpy import time a = numpy.random.random((2000, 2000)) start = time.time() a[abs(a) < 10] = 0 stop = time.time() print stop  start a = numpy.random.random((2000, 2000)) start = time.time() a = a * (abs(a) >= 10) stop = time.time() print stop  start a = numpy.random.random((2000, 2000)) start = time.time() a *= (abs(a) >= 10) stop = time.time() print stop  start Output (reproducible): 0.680999994278 0.220999956131 0.19000005722 Friedrich
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, though last I checked, it's written in Python, and thus not all that fast.  Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 5266959 voice 7600 Sand Point Way NE (206) 5266329 fax Seattle, WA 98115 (206) 5266317 main reception Chris.Barker@noaa.gov
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.
though last I checked, it's written in Python,
No, it isn't.
and thus not all that fast.
No, it's reasonably performant.  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
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]) Josef
though last I checked, it's written in Python,
No, it isn't.
and thus not all that fast.
No, it's reasonably performant.
 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 _______________________________________________ NumPyDiscussion mailing list NumPyDiscussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpydiscussion
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.  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
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,1e4,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 _______________________________________________ NumPyDiscussion mailing list NumPyDiscussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpydiscussion
Robert Kern wrote:
On Wed, Mar 17, 2010 at 14:04, Christopher Barker <Chris.Barker@noaa.gov> wrote:
though last I checked, it's written in Python,
No, it isn't.
and thus not all that fast.
No, it's reasonably performant.
nice to know  a good while back, I wrote a small collection of addons to Numeric, in painfully handwritten C, including a "fast_clip". I think every one of them has been rendered obsolete in recent versions of numpy. Chris  Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 5266959 voice 7600 Sand Point Way NE (206) 5266329 fax Seattle, WA 98115 (206) 5266317 main reception Chris.Barker@noaa.gov
On Wed, Mar 17, 2010 at 12:04 PM, 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.
One of the methods, the fast one, uses an inplace operation, so timeit won't work. But for cases where timeit does work, yes, it is great.
On Wed, Mar 17, 2010 at 14:03, Keith Goodman <kwgoodman@gmail.com> wrote:
On Wed, Mar 17, 2010 at 12:04 PM, 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.
One of the methods, the fast one, uses an inplace 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()  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
On Wed, Mar 17, 2010 at 12:08 PM, Robert Kern <robert.kern@gmail.com> wrote:
On Wed, Mar 17, 2010 at 14:03, Keith Goodman <kwgoodman@gmail.com> wrote:
On Wed, Mar 17, 2010 at 12:04 PM, 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.
One of the methods, the fast one, uses an inplace 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
On Wed, Mar 17, 2010 at 14:18, Keith Goodman <kwgoodman@gmail.com> wrote:
On Wed, Mar 17, 2010 at 12:08 PM, Robert Kern <robert.kern@gmail.com> wrote:
On Wed, Mar 17, 2010 at 14:03, Keith Goodman <kwgoodman@gmail.com> wrote:
On Wed, Mar 17, 2010 at 12:04 PM, 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.
One of the methods, the fast one, uses an inplace 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/~ipythondev/ipython/trunk/annotate/head:/IPytho...  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
participants (7)

Alan McIntyre

Christopher Barker

Friedrich Romstedt

gerardob

josef.pktd＠gmail.com

Keith Goodman

Robert Kern