4

I am completely puzzled by this.

From the following

import numpy as np

a = np.array([4, -9])
a[0] = 0.4
a

I expected output: array([ 0.4, -9]). But it gives me

array([ 0, -9]).

But when I changed the dtype to f

a = np.array([4, -9], 'f')
a[0] = 0.4
a

It gives me the expected out put of array([ 0.40000001, -9. ], dtype=float32)

The documentation for numpy.array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0) says:

dtype : data-type, optional The desired data-type for the array. If not given, then the type will be determined as the minimum type required to hold the objects in the sequence. This argument can only be used to ‘upcast’ the array. For downcasting, use the .astype(t) method.

When I initialized the array it initialized the values to integers and so when I indexed the array with a float it only recognized the integer part of 0.4 and hence gave me 0. This is how I understand it. Is this correct?. But I am still surprised by this behavior.

Question: What exactly is going on here?

1 Answer 1

3

The problem is that your array is of dtype=np.int64:

In [141]: a = np.array([4, -9])

In [142]: a.dtype
Out[142]: dtype('int64')

This means that you can only store integers, and any floats are truncated before assignment is done. If you want to store floats and ints together, you should specify dtype=object first:

In [143]: a = np.array([4, -9], dtype=object)

In [144]: a[0] = 0.4

In [145]: a
Out[145]: array([0.4, -9], dtype=object) 

As for the issue with array([ 0.40000001, -9. ], 0.4, as a floating point number does not have an exact representation in memory (only an approximate one), which accounts for the imprecision you see.

Sign up to request clarification or add additional context in comments.

8 Comments

Excellent. This solves the problem. Obviously, I have a lot to learn.
I don't like the idea of using dtype=object to save both floats and integers. It's fine for mixing strings and numbers if needed, or better for holding things like lists. But for purely numeric data, it's better to use a numeric dtype.
@hpaulj Accepted. With a non-numeric type you lose out on all the vectorisation benefits.
What would be a numeric dtype that will do the job?. float? d??
@JackDawkins I think the overarching problem is the fact that you want to store ints and floats together. If you want to do it, this is how. But the advice we give you is this: Don't do it.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.