Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 比较二进制数据的最快方法?_Python_Numpy_Binary - Fatal编程技术网

Python 比较二进制数据的最快方法?

Python 比较二进制数据的最快方法?,python,numpy,binary,Python,Numpy,Binary,我的数据格式为0010011或[假、假、真、假、假、真、真]。这种格式大约有1000万个示例,每个示例的值不是7个,而是1000个 在我的用例中,我得到了一个相同形式的新条目。然后,我想得到100个最相等项的索引。这里,most equal被定义为分别具有最相交的1s或Trues。例如,两个条目00011和00010有一个相交1 目前,我正在做这样的比较: similarties = [] for idx, binary_1 in enumerate(complete_list): si

我的数据格式为
0010011
[假、假、真、假、假、真、真]
。这种格式大约有1000万个示例,每个示例的值不是7个,而是1000个

在我的用例中,我得到了一个相同形式的新条目。然后,我想得到100个最相等项的索引。这里,most equal被定义为分别具有最相交的
1
s或
True
s。例如,两个条目
00011
00010
有一个相交
1

目前,我正在做这样的比较:

similarties = []
for idx, binary_1 in enumerate(complete_list):
    similarties += [(idx, np.binary_repr(binary_1 & binary_2).count('1'))]
similarties.sort(key=lambda t: t[1], reverse=True)

对于10000个随机测试条目,这需要0.2秒。有没有更快的方法可以做到这一点?

使用广播可能会有所帮助。比如说,

import numpy as np

complete_list = np.random.randint(0, 2, (10000, 10)).astype(bool)
binary_2 = np.random.randint(0, 2, 10).astype(bool)

similarities = np.sum(complete_list & binary_2, axis=1)
idx = np.argsort(similarities)

print("Seed", binary_2)
print("Result", complete_list[idx[-1]])
print("Similarity", similarities[idx[-1]])
我无法运行您的示例(可能是不同的python/库版本?),因此没有运行任何比较这两种方法的基准测试。当然,我们的机器会有所不同,但上面的机器在我的机器上大约需要半毫秒


请注意,根据您对预期逻辑的描述,我使用了
&
而不是
|

更新:发现了三倍的加速

下面是一种在压缩位上使用numpy来节省内存的方法。要在该格式和
uint8
numpy类型的单个
0
s和
1
s之间进行转换,numpy提供了
packbits
unpackbits
功能

下面的代码预先计算所有由16位块组成的
2^16
模式的总和

(旧版本在数据和模板中查找字节对)

我们使用视图转换来
uint64
64
位的块执行逐位交集,然后再转换回
uint16
进行查找

为了找到最近的
n
,我们使用
argpartition
(O(n))而不是
argsort
(O(n log n))


所以
111111
000100
更等于
001100
?是的,相交的
1
s是countsBtw。在你的代码片段中不应该是
(binary\u 1和binary\u 2)
?哦,是的,你是对的。你的条目是Python长整数吗?也许你可以通过使用一种更友好的格式来提高速度。令人印象深刻。您如何评价使用np.packbits?这将大大缩小矩阵的大小。我建议使用它。我发布的代码假定已应用了
packbits
。只需确保将每一行的零填充到64位的倍数即可。(
packbits
仅按8的倍数填充)不同的问题:当查看结果时,交点的值都略高于300,但为什么它们不按顺序排列呢?因为
argpartition
不排序,它只会分成两部分,这样第一部分中的任何项都不会比第二部分中的任何项大。即使您需要对结果进行排序,首先进行分区,然后只对最相似的
100
进行排序可能会更便宜
import numpy as np

n, m = 1_000_000, 1_000

data = np.random.randint(0, 256, (n, (m + 63) // 64 * 8), dtype=np.uint8)
test = np.random.randint(0, 256, ((m + 63) // 64 * 8,), dtype=np.uint8)

def create_lookup_1d():
    x, p = np.ogrid[:1<<16, :16]
    p = 1 << p
    return np.count_nonzero(x & p, axis=1)

lookup_1d = create_lookup_1d()

def find_closest(data, test, n):
    similarities = lookup_1d[(data.view(np.uint64) & test.view(np.uint64))
                             .view(np.uint16)].sum(axis=1)
    top_n = np.argpartition(similarities, len(data)-n)[-n:]
    return top_n, similarities[top_n]

# below is obsolete older version

def create_lookup_2d():
    x, y, p = np.ogrid[:256, :256, :8]
    p = 1 << p
    return np.count_nonzero(x & y & p, axis=2)

lookup_2d = create_lookup_2d()

def find_closest_old(data, test, n):
    similarities = lookup_2d[data, test].sum(axis=1)
    top_n = np.argpartition(similarities, len(data)-n)[-n:]
    return top_n, similarities[top_n]
>>> import time
>>> t = time.perf_counter(); find_closest(data, test, 100); t = time.perf_counter() - t
(array([913659, 727762, 819589, 810536, 671392, 573459, 197431, 642848,
         8792, 710169, 656667, 692412,  23355, 695527, 276548, 756096,
       286481, 931702, 301590, 309232, 223684, 838507, 888607, 205403,
       988198, 600239, 256834, 876452, 793813,  46501, 559521, 697295,
       948215, 247923, 503962, 808630, 515953,  22821, 614888, 487735,
       443673, 174083, 906902, 613131, 546603, 147657, 332898, 381553,
       808760, 383885, 107547,  85942,  20966, 880034, 522925,  18833,
       547674, 901503, 702596, 773050, 734658, 383581, 973043, 387713,
       645705,  27045, 230226,  77403, 906601, 507193, 828268, 175863,
       708155, 130634, 486701, 534719, 643487, 940071, 694781, 470385,
       954446, 134532, 748100, 110987, 417001, 871320, 993915, 489725,
         6509,  38345, 705618, 637435, 311252, 347282, 536091, 663643,
       830238, 376695, 896090, 823631]), array([305, 305, 305, 305, 305, 305, 305, 305, 305, 305, 305, 305, 305,
       305, 306, 305, 306, 306, 305, 305, 305, 305, 305, 306, 305, 306,
       305, 306, 314, 308, 307, 309, 306, 308, 307, 308, 307, 312, 308,
       306, 316, 306, 306, 307, 307, 308, 309, 308, 307, 309, 309, 311,
       309, 310, 310, 307, 307, 306, 307, 306, 307, 309, 308, 309, 308,
       306, 307, 309, 306, 306, 306, 311, 306, 308, 307, 306, 306, 307,
       308, 306, 307, 310, 307, 306, 307, 309, 306, 306, 310, 313, 306,
       306, 307, 310, 306, 307, 307, 309, 311, 307]))
>>> t
0.4612512579988106