Let's say i have
Y = np.array([2, 0, 1, 1])
From here I want to get a matrix X with the form (len(Y), 3) . In this particular case, the first row of X should have one at the second index and zero at the other. The second line of X must have one index 0 and zero otherwise. To be explicit:
X = np.array([[0, 0, 1], [1, 0, 0], [0, 1, 0], [0, 1, 0]])
How can I create this matrix? I started with
X = np.zeros((Y.shape[0], 3))
but then couldn't figure out how to populate / populate those from the list of indices
As always, thanks for your time!
python numpy
cd98
source share