我正在使用 scipy 中的 stats.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
有什么方法可以提高效率?
答案 0 :(得分:1)
我不知道为什么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)