我正在尝试执行以下操作: 我有一个清单:
classes = [0,1,2,3,4,5,6,7,8,9]
我还有另一个列表:
labels = [1,1,1,5,3]
我想创建一个列表,其中包含10个列表,以便列表编号i包含指示符标签列表中的元素是否等于类i的指示器。 对于此示例:
[[0,0,0,0,0],
[1,1,1,0,0],
[0,0,0,0,0],
[0,0,0,0,1],
[0,0,0,0,0],
[0,0,0,1,0]
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0]]
我尝试了以下代码:
mappers = [np.vectorize(lambda x : 1 if x==c else 0) for c in classes]
indicators = [mapper(labels) for mapper in mappers]
但是,我没有得到想要的东西,我希望您对此有所帮助。我得到:
[[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0],
[0,0,0,0,0]]