我有一个形状为
针对每个沿着
这是一个哈希问题。
我使用字典创建了一个解决方案,但它会严重降低性能。对于每组值,我调用此函数:
m*n*k
的3D numpy数组arr
。针对每个沿着
m
轴的值集合(例如arr[:, 0, 0]
),我想生成一个单一的值来代表这个集合,以便最终得到一个n*k
的2D矩阵。如果沿着m
轴的值集合重复出现,则应该生成相同的值。这是一个哈希问题。
我使用字典创建了一个解决方案,但它会严重降低性能。对于每组值,我调用此函数:
def getCellId(self, valueSet):
# Turn the set of values (a numpy vector) to a tuple so it can be hashed
key = tuple(valueSet)
# Try and simply return an existing ID for this key
try:
return self.attributeDict[key]
except KeyError:
# If the key was new (and didnt exist), try and generate a new Id by adding one to the max of all current Id's. This will fail the very first time we do this (as there will be no Id's yet), so in that case, just assign the value '1' to the newId
try:
newId = max(self.attributeDict.values()) +1
except ValueError:
newId = 1
self.attributeDict[key] = newId
return newId
这个数组通常的大小是30*256*256,因此一组值将有30个。 我每次需要处理数百个这样的数组。 目前,对于100个数组的数据块,执行到计算哈希所需的所有处理时间为1.3秒。 包括哈希处理时间增加到75秒。
是否有更快的方法来生成单个代表值?