Home > Mobile >  Assign integer value of choice to True / False values in a boolean other than 0 and 1
Assign integer value of choice to True / False values in a boolean other than 0 and 1

Time:05-25

Is it possible to assign integer/ float values other than default 1 and 0 to the boolean True and False in array?

By default True converts to 1 and False to 0 from what I know. However when I try to assign '5.0' to True from a 2d array dtype=bool, the output still gives True and not the assigned integer value

#main_arr is a 3d array
main_arr = self.predictor(image)["instances"].pred_masks
#Taking the slice from 3d array based on 1st dimension
arr = np.asarray(main_arr[0,:,:])
#finding the non zero elements
row, col = np.nonzero(arr)
#assigning values to non zero elements
arr[row, col] = 5.0
print("The Non Zero value of element is :", arr[row[0]][col[0]])

The output is still True

The Non Zero value of element is : True

Whereas I would like to see something like

The Non Zero value of element is : 5.0

CodePudding user response:

Since the dtype of arr is boolean, when you make assignments within that array, it is being cast to match the dtype. You need an array with the appropriate dtype to do this:

arr = arr.astype(np.float64)
arr[row, col] = 5.0
  • Related