【问题标题】:Best algorithm to compare massive data比较海量数据的最佳算法
【发布时间】:2018-01-15 14:26:45
【问题描述】:

我有一个大数据集作为 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 解决这个问题的最佳方法是什么?

【问题讨论】:

  • 每个特定月份的值是否唯一且始终为整数?
  • 334 MB 将适合普通计算机的 RAM,因此请确保不要过度设计这台计算机。请定义这种重叠:这些总是整数吗? “0”前缀重要吗?它们是独一无二的吗?订单是否相关?请添加一些代码,以展示如何在 python 中比较两个简短的示例字符串。这会让事情变得更容易。
  • @IvanSivak 每个月的值都是唯一的,它们总是整数。
  • 如果值是唯一的 - “重叠”是什么意思?
  • @MBo 我的意思是一个月的列表中没有重叠。因此,从这个意义上说,价值是独一无二的。例如,当您比较 month1 和 month2 的值时,会有重叠。

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


【解决方案1】:

您可以使用集合交集方法从两个数组或列表中提取公共元素。集合交集的复杂度是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

您可能会在月份条目与实际数据之间的重叠方面取得更好的成功。

【讨论】:

  • 嗨,有什么理由使用 np.random.choice 而不是 random.sample?谢谢。
猜你喜欢
  • 1970-01-01
  • 2018-11-16
  • 2016-09-12
  • 1970-01-01
  • 2018-04-11
  • 1970-01-01
  • 2011-02-16
  • 2012-02-24
  • 2012-01-26
相关资源
最近更新 更多