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.
asked May 9, 2016 at 23:25
David R
1,0441 gold badge12 silver badges28 bronze badges
1 Answer 1
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)
answered May 9, 2016 at 23:39
MaxU - stand with Ukraine
212k37 gold badges402 silver badges437 bronze badges
Sign up to request clarification or add additional context in comments.
1 Comment
David R
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?
lang-py