a numarray question

A

avharut

hello everyone

would anyone please tell me what is the best (fastest) way of replacing
values in numarray arrays?

lets say i have an array that may contain 0s, and i just want to get
rid of those 0s by replacing them with another number. what would be
the most efficient way to do that?

many thanks in advance!
 
J

Juho Schultz

hello everyone

would anyone please tell me what is the best (fastest) way of replacing
values in numarray arrays?

lets say i have an array that may contain 0s, and i just want to get
rid of those 0s by replacing them with another number. what would be
the most efficient way to do that?

many thanks in advance!
One way to do that is

data2 = numarray.choose(numarray.equal(data,0), (data, another_number))

I am not sure this is the most efficient way. If you get other
suggestions, you can compare them with the timeit module.

http://www.python.org/doc/2.4.2/lib/module-timeit.html

But if you plan to do calculations with the data, I think replacing
zeros is not critical for performance.
 
B

Bas

I believe it is something like

a[a==0] = 5

Note that numarray will eventually be replaced by Scipy/Numpy at some
time, but this wouldn't change the basic stuff.

Cheers,
Bas
 
A

avharut

thanks guys!

------------
import timeit
first_way = "the_array[the_array == 0] = 5.0"
second_way = "another_array = numarray.choose(the_array == 0,
(the_array, 5.0))"
some_third_way = """\
indx = numarray.where(the_array == 0)[0]
another_array = numarray.put(the_array, indx, len(indx) * [5.0,])
"""
initial_statements = 'import numarray; the_array =
numarray.array(int(1.0e6) * [1.0, 0.0])'
t1 = timeit.Timer(first_way, initial_statements)
t2 = timeit.Timer(second_way, initial_statements)
t3 = timeit.Timer(some_third_way, initial_statements)
print '1st way takes', round(t1.timeit(number=100) / 100, 4),
'seconds\n',\
'2nd way takes', round(t2.timeit(number=100) / 100, 4),
'seconds\n',\
'3rd way takes', round(t3.timeit(number=100) / 100, 4),
'seconds'
------------

the above written stuff printed like:

1st way takes 0.0624 seconds
2nd way takes 0.0948 seconds
3rd way takes 0.0641 seconds

no any other idea on how else one might do the replacement and the
difference among those three seems to be not very big, well, i expected
something bigger.

p.s. gonna use the first way =)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top