Convert array of indices to one-hot encoded array in NumPy
Your array a defines the columns of the nonzero elements in the output array. You need to also define the rows and then use fancy indexing: >>> a = np.array([1, 0, 3]) >>> b = np.zeros((a.size, a.max()+1)) >>> b[np.arange(a.size),a] = 1 >>> b array([[ 0., 1., 0., 0.], [ 1., 0., 0., 0.], [ 0., … Read more