algorithm - 比较海量数据的最佳算法

标签 algorithm performance python-3.x string-comparison bigdata

我有一个很大的 csv 数据集 (334MB),如下所示。

month, output
1,"['23482394','4358309','098903284'....(total 2.5 million entries)]"
2,"['92438545','23482394',323103404'....(total 2.2 million entries)]"
3,"[...continue

现在,我需要比较一个月的输出与上个月重叠的百分比。

例如,当我比较第 1 个月和第 2 个月时,我希望得到类似“第 2 个月的输出与第 1 个月有 90% 重叠”的结果,然后是“第 3 个月与第 2 个月有 88% 的重叠”

用 Python3 解决这个问题的最佳方法是什么?

最佳答案

您可以使用集合交集方法来提取两个数组或列表的公共(public)元素 b/w。集合交集的复杂度为O(min(len(a), len(b))。

# generate random numpy array with unique elements
import numpy as np

month1 = np.random.choice(range(10**5, 10**7), size=25*10**5, replace=False)
month2 = np.random.choice(range(10**5, 10**7), size=22*10**5, replace=False)
month3 = np.random.choice(range(10**5, 10**7), size=21*10**5, replace=False)

print('Month 1, 2, and 3 contains {}, {}, and {} elements respectively'.format(len(month1), len(month2), len(month3)))

Month 1, 2, and 3 contains 2500000, 2200000, and 2100000 elements respectively

# Compare month arrays for overlap

import time

startTime = time.time()
union_m1m2 = set(month1).intersection(month2) 
union_m2m3 = set(month2).intersection(month3)

print('Percent of elements in both month 1 & 2: {}%'.format(round(100*len(union_m1m2)/len(month2),2)))
print('Percent of elements in both month 2 & 3: {}%'.format(round(100*len(union_m2m3)/len(month3),2)))

print('Process time:{:.2f}s'.format(time.time()-startTime))

Percent of elements in both month 1 & 2: 25.3%
Percent of elements in both month 2 & 3: 22.24%
Process time:2.46s

您可能会更成功地处理月份条目与实际数据之间的重叠。

关于algorithm - 比较海量数据的最佳算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45561176/

相关文章:

java - 如何在图中进行向后节点连接?

algorithm - 将高斯曲线拟合到c++中的某个直方图峰值

R/RStudio 慢得令人痛苦

java - 有没有一种有效的方法可以用 Java 将多个 HTML 字符串写入 PDF 文档?

python-3.x - Python : Unable to install hdbcli library on Python 3. 8 安装

regex - 使用正则表达式从右到左将数字分成三组

algorithm - 多点触摸屏的分类算法

C "Battleship"程序在 10k+ 次迭代后的非统计输出

html - 从 HTML/CSS 触发 Chrome 中的 GPU 光栅化以实现背景图像动画

python 计算列表项的出现次数