将NumPy数组矢量化重新标记为连续数字并取回

将NumPy数组矢量化重新标记为连续数字并取回,第1张

将NumPy数组矢量化重新标记为连续数字并取回

我们可以使用可选参数

return_inverse
with
np.unique
来获取那些唯一的顺序ID
/标记,如下所示-

unq_arr, unq_tags = np.unique(old_classes,return_inverse=1)

索引为

unq_arr
with
unq_tags
以取回-

old_classes_retrieved = unq_arr[unq_tags]

样品运行-

In [69]: old_classes = np.array([0,1,2,6,6,2,6,1,1,0])In [70]: unq_arr, unq_tags = np.unique(old_classes,return_inverse=1)In [71]: unq_arrOut[71]: array([0, 1, 2, 6])In [72]: unq_tagsOut[72]: array([0, 1, 2, 3, 3, 2, 3, 1, 1, 0])In [73]: old_classes_retrieved = unq_arr[unq_tags]In [74]: old_classes_retrievedOut[74]: array([0, 1, 2, 6, 6, 2, 6, 1, 1, 0])


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5639863.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存