使用np.eye生成M*N矩阵,后面跟的数组说明1偏移的位置
import numpy as np
values = np.array([1, 0, 3])
n_values = np.max(values) + 1
print(np.eye(n_values)[values])
# 输出结果
array([[ 0., 1., 0., 0.],
[ 1., 0., 0., 0.],
[ 0., 0., 0., 1.]])
参考
https://blog.csdn.net/zhongranxu/article/details/79332154