简体   繁体   中英

I am try to assigne float numbers from tuple to numpy array

I try to assign float numbers stored in tuple to the array but in result I got only integers.

print(type(coordinates))
<class 'tuple'>
        
print(coordinates)
(67.70841587330506, 43.49477297494752)
       
print(type(a_punkty))
<class 'numpy.ndarray'>

print(a_punkty.shape)
(10, 2)
a_punkty[0] = coordinate
print(a_punkty[0])
[67 43]

but I would like to have [67.71, 43.49]

How to do this, Thanks!

Looks like your numpy array is not of a float type. This means that your array can store only integers in this example.

import numpy as np

# because of argument dtype, everything this array stores is casted to uint
a = np.array([(1, 2), (1.1, 2.2), (-1, -5.1)], dtype=np.uint8)
print(a)
# array([[1, 2], [1, 2], [255, 251]], dtype=uint8)

To solve this issue, change the datatype of array to float (beware that every stored value is also casted to float).

b = a.astype(np.float32)
b[0] = (9.99, -5.2)
print(b)
# array([[9.99, -5.2], [1.0, 2.0], [255.0, 251.0]], dtype=float32)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM