def tensor_threshold(tensor, n):
thresholds = [sorted(arr)[-n] for arr in tensor]
return [[0 if x < th else 1 for x in arr]
for arr, th in zip(tensor, thresholds)]
T = [[0.8, 0.1, 0.9, 0.2], [0.7, 0.1, 0.4, 0.6]]
tensor_threshold(T, n=2)
>>> [[1, 0, 1, 0], [1, 0, 0, 1]]
为了提高性能效率,我们可以使用^{} -
或者我们可以在
argpartition
步骤中使用np.argpartition(-a,n,axis=1)[:,:n]
样本运行-
您可以使用^{} 获取索引,然后将其设置为
1
以获得新的张量或
n=3
你可以使用列表理解。 简单快速
首先选择每个数组的阈值(基于
n
)相关问题 更多 >
编程相关推荐