Skip to content
Advertisement

python transform 1d array of probabilities to 2d array

I have an array of probabilities:

l = [0.9, 0.2, 0.4]

and I want to make it 2d array:

l = [0.9 0.1
     0.2 0.8
     0.4 0.6]

What is the best way to do so?

Advertisement

Answer

One idea is use numpy.hstack:

l = [0.9, 0.2, 0.4]

a = np.array(l)[:, None]

arr = np.hstack((a, 1 - a))
print (arr)
[[0.9 0.1]
 [0.2 0.8]
 [0.4 0.6]]

Or use numpy.c_:

a = np.array(l)
arr = np.c_[a, 1 - a]
print (arr)
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement