简体   繁体   English

我尝试将浮点数从元组分配给 numpy 数组

[英]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]但我想要 [67.71, 43.49]

How to do this, Thanks!如何做到这一点,谢谢!

Looks like your numpy array is not of a float type.看起来您的 numpy 数组不是浮点类型。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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