Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

Slicing is work for numpy.multiply, but it seems no way to convert numpy int to float with slicing. 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). Target memory is too small, and I may hard to use sparse matrix.

Thanks for any suggestion...!

like image 373
Arlen Avatar asked Sep 03 '26 07:09

Arlen


1 Answers

You can't modify the dtype of a slice only. 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.

What you could do is create a temporary copy of your slice and convert it to 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.

like image 137
Julien Avatar answered Sep 04 '26 20:09

Julien



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!