I have an numpy array with shape (1, m)
and each entry (n
) is an integer ranging 0-9.
I want to create a new matrix that has shape (m, 10)
where all the entries are 0, except it is 1 for the nth column.
For example:
[2, 3, 1] -> [[0, 0, 1, 0, ...], [0, 0, 0, 1, ...], [0, 1, 0, 0, ...]]
The code I wrote for it that works is:
y_values = np.array([[2, 3, 6, 4, 7]])
y = np.zeros((10, y_values.shape[1]))
for i in range(y_values.shape[1]):
y[y_values[0][i]][i] = 1
Is there a way I can get rid of the for
loop, and make this more efficient?