我通过data_np
将numpy数组data_np.dtype = np.float32
的类型从int32更改为float32
但是它正在改变
,并在两者之间添加一个附加的0
列。
有关如何正确执行此操作的任何建议。
答案 0 :(得分:1)
ndarray.dtype
并不意味着修改dtype。我会使用astype
:
data_np = data_np.astype(np.float32)
示例:
data_np = np.random.randint(0,10,(3,3),dtype=np.int32)
>>> data_np
array([[7, 8, 4],
[7, 6, 8],
[4, 5, 9]], dtype=int32)
data_np = data_np.astype(np.float32)
>>> data_np
array([[7., 8., 4.],
[7., 6., 8.],
[4., 5., 9.]], dtype=float32)