473,396 Members | 2,038 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,396 software developers and data experts.

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.asarray(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.asarray(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.asarray()', 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 2771
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.asarray(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.asarray(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.asarray(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.asarray(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.sourceforge.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
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...
15
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...
2
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...
5
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....
3
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...
0
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
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...
2
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...
3
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.