输入:
- 数组长度(整数)
- 索引(集合或列表)
输出:
一个布尔型的numpy数组,对于给定的索引位置为1,其他位置为0。
示例:
输入:array_length=10, indexes={2,5,6}
输出:
[0,0,1,0,0,1,1,0,0,0]
这是我简单的实现方式:
def indexes2booleanvec(size, indexes):
v = numpy.zeros(size)
for index in indexes:
v[index] = 1.0
return v
有更加优雅的方法来实现这个吗?