[英]How to convert numpy int to float with separate numpy array?
I have a huge data of numpy memory error problem, I try to use slicing to handle it like following How to merge two large numpy arrays if slicing doesn't resolve memory error? 我有大量的numpy内存错误问题数据,我尝试使用切片来处理它,如下所示:如果切片无法解决内存错误,如何合并两个大型numpy数组?
Slicing is work for numpy.multiply, but it seems no way to convert numpy int to float with slicing. 切片适用于numpy.multiply,但似乎无法将numpy int转换为使用切片进行浮动。 Following is sample:
以下是示例:
images = numpy.array([1,2,3,4,5,6,7,8,9,10])
images[0:5] = images[0:5].astype(numpy.float32)
print type(images[0])
images = images.astype(numpy.float32)
print type(images[0])
<type 'numpy.int32'>
<type 'numpy.float32'>
Once I use images.astype(numpy.float32), I got memory error(dtype is same). 一旦我使用images.astype(numpy.float32),我得到了内存错误(dtype是相同的)。 Target memory is too small, and I may hard to use sparse matrix.
目标内存太小,可能很难使用稀疏矩阵。
Thanks for any suggestion...! 感谢您的任何建议...!
You can't modify the dtype
of a slice only. 您不能仅修改切片的
dtype
。 When you do 当你做
images[0:5] = images[0:5].astype(numpy.float32)
images[0:5].astype(numpy.float32)
creates a float
copy of your slice, but the result is converted back to int
when assigned back to the images
slice since images
is of dtype
int
. images[0:5].astype(numpy.float32)
创建了一个float
的切片的拷贝,但结果被转换回int
分配回时images
切片,因为images
是dtype
int
。
What you could do is create a temporary copy of your slice and convert it to float: 您可以做的是创建切片的临时副本并将其转换为float:
copied_slice = images[0:5].astype(numpy.float32)
do all the computation you need on this smaller part of your data, save whatever result you need, then move on to the next (copied and converted) slice. 对数据的这一较小部分进行所有所需的计算,保存所需的任何结果,然后继续进行下一个(复制和转换)切片。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.