问题
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?
回答1:
As you would expect, there is a way, using fancy indexing. You need to supply two arrays, giving the corresponding coordinates in each direction. The column index you already have. The row index, corresponding to each column, is just np.arange(m)
:
result = np.zeros((m, 10), dtype=np.bool)
result[np.arange(m), y_values[0]] = True
回答2:
Another solution would be (if you are certain that all 0-9 classes are going to be there),
df = pd.get_dummies([2, 3, 1, 4]).T
来源:https://stackoverflow.com/questions/59474970/expanding-numpy-array-while-updating-the-values