Skip to content Skip to sidebar Skip to footer

How To Convert A Binary Matrix To A Bipolar One In Python

There is a function in Keras to generate a binary matrix for an array of labels: # Consider an array of 5 labels out of a set of 3 classes {0, 1, 2}: > labels array([0, 2, 1, 2,

Solution 1:

You could do the following:

import numpy as np

arr = np.array([[1., 0., 0.],
                [0., 0., 1.],
                [0., 1., 0.],
                [0., 0., 1.],
                [1., 0., 0.]])

arr[np.isclose(arr, 0)] = -1print(arr)

Output

[[ 1. -1. -1.][-1. -1.  1.][-1.  1. -1.][-1. -1.  1.][ 1. -1. -1.]]

Solution 2:

Post a Comment for "How To Convert A Binary Matrix To A Bipolar One In Python"