473,324 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

a numarray question

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!

Feb 15 '06 #1
3 977
av*****@gmail.com wrote:
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.
Feb 15 '06 #2
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

Feb 15 '06 #3
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 =)

Feb 15 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: RJS | last post by:
Hi all, I can't get a py2exe compiled app to run with numarray (numarray-0.5.win32- py2.2). Also wxPythonWIN32-2.3.3.1-Py22 and ActivePython-2.2.1-222. In the sample below, commenting out...
2
by: Tim Rowe | last post by:
Does numarray-0.5.win32-py2.2.exe work with Python 2.3? If not, is there a version that will? Tia, Tim
0
by: Colin J. Williams | last post by:
numarray is a package which is under development and intended to replace Numeric, an efficient and operational package. One of the classes in numarray is NumArray. As currently implemented,...
3
by: Alexander Schwaigkofler | last post by:
Hi! I have the following problem with numarray. I read the install.txt manual, but it doesn't already work. OS: Microsoft Windows 2000 python: Python 2.2.3 (#42, May 30 2003, 18:12:08) on...
8
by: Russell E. Owen | last post by:
I'm writing a C extension for numarray and am puzzled about the idiom for error handling. The documentation seems to say one should always decref an array after calling NA_InputArray, etc., to...
3
by: Alex Hunsley | last post by:
I'm looking for a way to iterate over all the items in a numarray. Writing a few nested loops isn't going to cut it, because the numarray in question could be of any dimension... I am aware of the...
3
by: SunX | last post by:
I tried to initialize a float point array by: import numarray xur = numarray.fromfunction(lambda x,y,z:x*y*z, (2, 2, 2)) but I ended up with an integer array even though x, y, and z are all...
11
by: grv | last post by:
So it is supposed to be very fast to have an array of say 5 million integers stored in a binary file and do a = numarray.fromfile('filename', (2, 2, 2)) numarray.add(a, 9, a) but how is that...
1
by: Chris P. | last post by:
Hi. I have a very simple task to perform and I'm having a hard time doing it. Given an array called 'x' (created using the numarray library), is there a single command that rounds each of its...
10
by: Bryan | last post by:
hi, what is the difference among numeric, numpy and numarray? i'm going to start using matplotlib soon and i'm not sure which one i should use. this page says, "Numarray is a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.