在python中使用大型2D矩阵查找模式的有效方法

时间:2018-06-29 06:30:41

标签: python scipy

我正在使用 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

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

1 个答案:

答案 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)
相关问题