473,585 Members | 2,501 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

numpy numbers converted wrong

in Gnuplot (Gnuplot.utils) the input array will be converted to a Numeric float array as shown below. When I insert a numpy array into Gnuplot like that below, numbers 7.44 are cast to 7.0
Why is this and what should I do ? Is this bug in numpy or in Numeric?
[Dbg]>>m #numpy array
array([[ 9.78109200e+08, 7.44000000e+00],
[ 9.78454800e+08, 7.44000000e+00],
[ 9.78541200e+08, 8.19000000e+00],
...,
[ 1.16162280e+09, 8.14600000e+01],
[ 1.16170920e+09, 8.10500000e+01],
[ 1.16179560e+09, 8.16800000e+01]])
[Dbg]>>Numeric.asarr ay(m, Numeric.Float32 )[:10]
array([[ 9.78109184e+008 , 7.00000000e+000],
[ 9.78454784e+008 , 7.00000000e+000],
[ 9.78541184e+008 , 8.00000000e+000],
[ 9.78627584e+008 , 8.00000000e+000],
[ 9.78713984e+008 , 8.00000000e+000],
[ 9.78973184e+008 , 8.00000000e+000],
[ 9.79059584e+008 , 8.00000000e+000],
[ 9.79145984e+008 , 8.00000000e+000],
[ 9.79232384e+008 , 9.00000000e+000],
[ 9.79318784e+008 , 8.00000000e+000]],'f')
[Dbg]>>Numeric.asarr ay(m, Numeric.Float)[:10]
array([[ 9.78109200e+008 , 7.00000000e+000],
[ 9.78454800e+008 , 7.00000000e+000],
[ 9.78541200e+008 , 8.00000000e+000],
[ 9.78627600e+008 , 8.00000000e+000],
[ 9.78714000e+008 , 8.00000000e+000],
[ 9.78973200e+008 , 8.00000000e+000],
[ 9.79059600e+008 , 8.00000000e+000],
[ 9.79146000e+008 , 8.00000000e+000],
[ 9.79232400e+008 , 9.00000000e+000],
[ 9.79318800e+008 , 8.00000000e+000]])
[Dbg]>>>
and why and what is:

[Dbg]>>m[0,1]
7.44
[Dbg]>>type(_)
<type 'numpy.float64' >
[Dbg]>>>
does this also slow down python math computations?
should one better stay away from numpy in current stage of numpy development?
I remember, with numarray there were no such problems.

-robert
PS: in Gnuplot.utils:

def float_array(m):
"""Return the argument as a Numeric array of type at least 'Float32'.

Leave 'Float64' unchanged, but upcast all other types to
'Float32'. Allow also for the possibility that the argument is a
python native type that can be converted to a Numeric array using
'Numeric.asarra y()', but in that case don't worry about
downcasting to single-precision float.

"""

try:
# Try Float32 (this will refuse to downcast)
return Numeric.asarray (m, Numeric.Float32 )
except TypeError:
# That failure might have been because the input array was
# of a wider data type than Float32; try to convert to the
# largest floating-point type available:
return Numeric.asarray (m, Numeric.Float)
Oct 26 '06 #1
2 2785
robert wrote:
in Gnuplot (Gnuplot.utils) the input array will be converted to a Numeric float array as shown below. When I insert a numpy array into Gnuplot like that below, numbers 7.44 are cast to 7.0
Why is this and what should I do ? Is this bug in numpy or in Numeric?
[Dbg]>>m #numpy array
array([[ 9.78109200e+08, 7.44000000e+00],
[ 9.78454800e+08, 7.44000000e+00],
[ 9.78541200e+08, 8.19000000e+00],
...,
[ 1.16162280e+09, 8.14600000e+01],
[ 1.16170920e+09, 8.10500000e+01],
[ 1.16179560e+09, 8.16800000e+01]])
[Dbg]>>Numeric.asarr ay(m, Numeric.Float32 )[:10]
array([[ 9.78109184e+008 , 7.00000000e+000],
[ 9.78454784e+008 , 7.00000000e+000],
[ 9.78541184e+008 , 8.00000000e+000],
[ 9.78627584e+008 , 8.00000000e+000],
[ 9.78713984e+008 , 8.00000000e+000],
[ 9.78973184e+008 , 8.00000000e+000],
[ 9.79059584e+008 , 8.00000000e+000],
[ 9.79145984e+008 , 8.00000000e+000],
[ 9.79232384e+008 , 9.00000000e+000],
[ 9.79318784e+008 , 8.00000000e+000]],'f')
[Dbg]>>Numeric.asarr ay(m, Numeric.Float)[:10]
array([[ 9.78109200e+008 , 7.00000000e+000],
[ 9.78454800e+008 , 7.00000000e+000],
The problem is with the version of Numeric you are using. I can replicate this
problem with Numeric 24.0 but not with 24.2.
and why and what is:

[Dbg]>>m[0,1]
7.44
[Dbg]>>type(_)
<type 'numpy.float64' >
[Dbg]>>>
It is a scalar object. numpy supports more number types than Python does so the
scalar results of indexing operations need representations beyond the standard
int, float, complex types. These scalar objects also support the array
interface, so it's easier to write generic code that may operate on arrays or
scalars. Their existence also resolves the long-standing problem of maintaining
the precision of arrays even when performing operations with scalars. In
Numeric, adding the scalar 2.0 to a single precision array would return a
double-precision array. Worse, if a and b are single precision arrays, (a+b[0])
would give a double-precision result because b[0] would have to be represented
as a standard Python float.

The _Guide to NumPy_ has a discussion of these in Chapter 2, part of the sample
chapters:

http://numpy.scipy.org/numpybooksample.pdf
does this also slow down python math computations?
If you do a whole lot of computations with scalar values coming out of arrays,
then yes, somewhat. You can forestall that by casting to Python floats or ints
if that is causing problems for you.
should one better stay away from numpy in current stage of numpy development?
I remember, with numarray there were no such problems.
Not really, no.

--
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

Oct 26 '06 #2
robert wrote:
in Gnuplot (Gnuplot.utils) the input array will be converted to a Numeric float array as shown below. When I insert a numpy array into Gnuplot like that below, numbers 7.44 are cast to 7.0
Why is this and what should I do ? Is this bug in numpy or in Numeric?
[Dbg]>>m #numpy array
array([[ 9.78109200e+08, 7.44000000e+00],
[ 9.78454800e+08, 7.44000000e+00],
[ 9.78541200e+08, 8.19000000e+00],
...,
[ 1.16162280e+09, 8.14600000e+01],
[ 1.16170920e+09, 8.10500000e+01],
[ 1.16179560e+09, 8.16800000e+01]])
[Dbg]>>Numeric.asarr ay(m, Numeric.Float32 )[:10]
array([[ 9.78109184e+008 , 7.00000000e+000],
[ 9.78454784e+008 , 7.00000000e+000],
[ 9.78541184e+008 , 8.00000000e+000],
[ 9.78627584e+008 , 8.00000000e+000],
[ 9.78713984e+008 , 8.00000000e+000],
[ 9.78973184e+008 , 8.00000000e+000],
[ 9.79059584e+008 , 8.00000000e+000],
[ 9.79145984e+008 , 8.00000000e+000],
[ 9.79232384e+008 , 9.00000000e+000],
[ 9.79318784e+008 , 8.00000000e+000]],'f')
[Dbg]>>Numeric.asarr ay(m, Numeric.Float)[:10]
array([[ 9.78109200e+008 , 7.00000000e+000],
[ 9.78454800e+008 , 7.00000000e+000],
[ 9.78541200e+008 , 8.00000000e+000],
[ 9.78627600e+008 , 8.00000000e+000],
[ 9.78714000e+008 , 8.00000000e+000],
[ 9.78973200e+008 , 8.00000000e+000],
[ 9.79059600e+008 , 8.00000000e+000],
[ 9.79146000e+008 , 8.00000000e+000],
[ 9.79232400e+008 , 9.00000000e+000],
[ 9.79318800e+008 , 8.00000000e+000]])
This is odd but we need to know the version numbers of both packages to
help further. For one, I'm surprised that you can use Numeric.asarray
to force cast to Numeric.Float32 without raising an error.

Also, you can ask on nu************* *@lists.sourcef orge.net to reach an
audience more directly able to help.
[Dbg]>>>
and why and what is:

[Dbg]>>m[0,1]
7.44
[Dbg]>>type(_)
<type 'numpy.float64' >
[Dbg]>>>
does this also slow down python math computations?
No, not necessarily (depends on what you mean).

Python floats are still Python floats. NumPy provides, in addition, an
array scalar for every "kind" of data that a NumPy array can be composed
of. This avoids the problems with being unable to find an appropriate
Python scalar for a given data-type. Where possible, the NumPy scalar
inherits from the Python one.

By default, the NumPy scalars have their own math defined which uses the
error-mode setting capabilities of NumPy to handle errors. Right now,
these operations are a bit slower than Python's built-ins because of the
way that "mixed" calculations are handled.

For, the data-types that over-lap with Python scalars you can set things
up so that NumPy scalars use the Python math instead if you want. But,
again, NumPy does nothing to change the way that Python numbers are
calculated.

should one better stay away from numpy in current stage of numpy development?
No, definitely not. Don't stay away. NumPy 1.0 is out.

Oct 26 '06 #3

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

Similar topics

1
1516
by: drife | last post by:
Hello, I use the Python Numeric package extensively, and had been an avid user of the "old" scipy. In my view, both pieces of software are truly first rate, and have greatly improved my productivity in the area of scientific analysis. Thus, I was excited to make the transition to the new scipy core (numpy). Unfortunately, I am...
15
2509
by: greg.landrum | last post by:
After using numeric for almost ten years, I decided to attempt to switch a large codebase (python and C++) to using numpy. Here's are some comments about how that went. - The code to automatically switch python stuff over just kind of works. But it was a 90% solution, I could do the rest by hand. Of course, the problem is that then the code...
2
22358
by: mcdurr | last post by:
I recently installed Python 2.5 on Windows and also installed numpy 1.0. I'd like to compute an FFT on an array of numbers but I can't seem to access the FFT function. I'm fairly new to Python (obviously) and I can't seem to find documentation to match my distribution of numpy and I can't figure out how to access the FFT function. Python...
5
4120
by: auditory | last post by:
I am a newbie here I am trying to read "space separated floating point data" from file I read about csv module by searching this group, but I couldn't read space separated values with csv. (which may be matter of course..) I also read about numpy.fromfile(file, sep=' ') which i can use. but on my machine(ubuntu linux) numpy is unknown...
3
427
by: lancered | last post by:
Hi dear all, I am using Python2.4.2+NumPy1.0.1 to deal with a parameter estimation problem with the least square methods. During the calculations, I use NumPy package to deal with matrix operations, mostly matrix inversion and trasposition. The dimentions of the matrices used are about 29x19,19x19 and 29x29. During the calculation, I...
0
1714
by: Gary Herron | last post by:
Marlin Rowley wrote: Numpy can do this for you. First, do you really mean the array to contain lists of one string each? If so: kludge here array(, dtype='|S1') array(, ,
1
2188
by: martin.nordstrom87 | last post by:
Hi! I'm trying to wrap numpy with Cython and I've tried to use this guide to manage this: http://wiki.cython.org/WrappingNumpy However when I send an array to mysum() it gives me the right answer only when dtype of the array is float, otherwise it gives me random answers. The problem may be that the array is converted to a C double which is...
2
1690
by: Rick Giuly | last post by:
Hello All, Case 1 This generates an error, which makes sense because the argument should be a list of numbers: numpy.array(10,10) Case 2 This does not generate an error and the result is an array with a single element:
3
6827
by: Rüdiger Werner | last post by:
Hello! Out of curiosity and to learn a little bit about the numpy package i've tryed to implement a vectorised version of the 'Sieve of Zakiya'. While the code itself works fine it is astounding for me that the numpy Version is almost 7 times slower than the pure python version. I tryed to find out if i am doing something wrong but...
0
7908
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7836
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7950
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8212
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6606
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3835
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3863
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2343
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1175
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.