Skip to content
Advertisement

Apply threshold for numpy.ndarray

I have a model predictions type of numpy.ndarray The predictions looks like

y_pred = array([[0.25602802, 0.74397198],
       [0.33962464, 0.66037536],
       [0.95954497, 0.04045503],
       [0.11751671, 0.88248329],
       [0.6809288 , 0.3190712 ],
       [0.6648042 , 0.3351958 ],
       [0.21672122, 0.78327878],
       [0.52893726, 0.47106274],
       [0.45118992, 0.54881008],
       [0.36868405, 0.63131595]])

where the first value of inner array corresponds to 0 class and the second value corresponds to 1 class. For this y_pred i need to apply FNR threshold 0.21552509277542697, which i also calculated. That is the efficient numpy way to do it ?

The result should be in predict interface. For example for pair [0.6824846 , 0.3175154 ] result should 1 because 0.3175154 > threshold 0.21552509277542697. For pair [0.95894656, 0.04105344] the result should be 0 because 0.04045503 < threshold 0.21552509277542697

I expect the result looks like

array([1, 1, 0, 1, 0, 0, 1, 0, 1, 1])

Advertisement

Answer

A mask based on both columns looks like this:

(y_pred > threshold).all(1)

The result is an array of booleans. You can sum booleans to get an integer, or use them as an index. If you absolutely insist on getting zeros and ones, you can convert to integer, e.g.:

(y_pred > threshold).all(1).astype(int)

Without copying any data, you can do:

(y_pred > threshold).all(1).view(np.uint8)
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement