在python(Scipy)中查找大型2D矩阵中的模式的有效方法



我正在使用scipystats.mode((来计算800*500 矩阵中的模式。执行时间如下:

`Time taken to execute 0.4359015187888584
Time taken to execute 0.42199154405135975
Time taken to execute 0.4250416821138656
Time taken to execute 0.4100701556064723
Time taken to execute 0.4371956395342953`

但我在以下情况下需要它:

Excution time 0.09193338154885265

有什么方法可以提高效率吗?

我不知道为什么scipy.stats.mode这么慢。无论如何,您可以使用np.bincount获得更快的结果:

# create random frame
>>> a = np.random.randint(0, 256, (800, 500)).astype(np.int8)
>>> 
# add row offsets to make bincount create separate bins for each row
>>> counts = np.bincount((a.view(np.uint8) + 256 * np.arange(800)[:, None]).ravel(), minlength=256*800).reshape(800, 256)
# find the mode
>>> values = np.argmax(counts, axis=1)
# discard the counts for all other values
>>> counts = counts[np.arange(800), values]
# convert modes back to original dtype
>>> values = values.astype(np.uint8).view(np.int8)

最新更新