Sum decimal numbers in numpy array

Question:

I need to sum values from different arrays, but the problem is that an array that contains the number 0.5 is recognized just as 0. . I need to sum the decimal part of the number (.5) because is to calculate an index.

This is my code

red_band = dataset.read(4)
nir_band = dataset.read(5)
L = np.full_like(red_band, 0.5)


savi = ((nir_band.astype(float) - red_band.astype(float)) / (red_band.astype(float) + nir_band.astype(float) + L))*(np.full_like(red_band, 1) + L)

And this is an example of my ouput for variable L

array([[0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       ...,
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0],
       [0, 0, 0, ..., 0, 0, 0]], dtype=uint16)
Asked By: Franco Barrionuevo

||

Answers:

np.full_like matches the size and type of the given array. In this case, red_band is a integer array (specifically uint16). Attempting to populate an integer array with a floating point number like 0.5 results in truncation, hence why L contains all 0s.

To create a float array with the same size as red_band, you can use np.full instead:

L = np.full(red_band.size, 0.5)
Answered By: Brian