2

I'm confused by some output I'm seeing.

If p is a list of floats, what is the difference between:

input = np.array([p]).astype('f')

and

input = np.array([p],float)

If I use the second option and then do a print(input), I always get something like:

[array([-0.662, 0.246, 1.029])]

But if I use the first option, sometimes I get simply: [[ 0.61900002 1.71300006 2.16899991]]

but other times I get the [array([])] form.

1 Answer 1

4

here is the explanation:

In [217]: np.array([1.1,1.2,1.3]).astype('f')
Out[217]: array([ 1.10000002,  1.20000005,  1.29999995], dtype=float32)

In [218]: np.array([1.1,1.2,1.3]).astype('float')
Out[218]: array([ 1.1,  1.2,  1.3])

In [219]: np.array([1.1,1.2,1.3]).astype(float)
Out[219]: array([ 1.1,  1.2,  1.3])

Types:

In [220]: np.array([1.1,1.2,1.3]).astype(float).dtype
Out[220]: dtype('float64')

In [221]: np.array([1.1,1.2,1.3]).astype('f').dtype
Out[221]: dtype('float32')

so you will have the same result using np.array([p], 'f'):

In [224]: np.array([1.1,1.2,1.3],'f')
Out[224]: array([ 1.10000002,  1.20000005,  1.29999995], dtype=float32)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, MaxU, I see that part of the difference is linked to float32 versus float64, but can you explain why sometimes I just get: [[ 0.61900002 1.71300006 2.16899991]], no "array" showing up when I do a print() command?

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.