Memory error for array conversion

If we convert a large array containing 0both 1as booleanand to another array containing 0and 1how float, the size of the array will be almost 10 times larger. What is the best way (if any) to solve this problem in python (Numpy) if we need this conversion?

+4
source share
1 answer

You probably don't need to do the conversion. If you perform some calculations using your bool array and another floating point array, the conversion will be performed during the operation:

import numpy as np
y = np.array([False, True, True, False], dtype=bool)
x = np.array([2.5, 3.14, 2.7, 8.9], dtype=float)
z = x*y
print z
[ 0. 3.14 2.7 0. ]
+6
source

All Articles