【发布时间】:2019-12-15 05:43:28
【问题描述】:
我有一个 numpy 数组,由大约 1200 个数组组成,每个数组包含 10 个值。 np.shape = 1200, 10。每个元素的值介于 0 到 570 万之间。
接下来我有一个包含 3800 行的 .csv 文件。每行包含 2 个值。第一个值指示范围,第二个值是标识符。 .csv 文件的前 5 行和后 5 行:
509,47222
1425,47220
2404,47219
4033,47218
6897,47202
...,...
...,...
...,...
5793850,211
5794901,186
5795820,181
5796176,43
5796467,33
第一列会上升,直到达到 570 万。对于 numpy 数组中的每个值,我想检查 .csv 文件的第一列。例如,我有值3333,这意味着属于3333 的标识符是47218。每行表示从前一行的第一列到该行的第一列,例如:2404 - 4033,标识符为47218。
现在我想获取 numpy 数组中每个值的标识符,然后我想保护标识符以及在 numpy 数组中找到该标识符的频率。这意味着我需要在 12000 行的 csv 文件上循环 3800 次,然后 ++ 一个整数。这个过程大约需要 30 秒,这太长了。
这是我目前使用的代码:
numpy_file = np.fromfile(filename, dtype=np.int32)
#some code to format numpy_file correctly
with open('/identifer_file.csv') as read_file:
csv_reader = csv.reader(read_file, delimiter=',')
csv_reader = list(csv_reader)
identifier_dict = {}
for numpy_array in numpy_file:
for numpy_value in numpy_array:
#there are 12000 numpy_value in numpy_file
for row in csv_reader:
last_identifier = 0
if numpy_value <= int(row[0]):
last_identifier = int(row[1])
#adding the frequency of the identifier in numpy_file to a dict
if last_identifier in identifier_dict:
identifier_dict[last_identifier] += 1
else:
identifier_dict[last_identifier] = 1
else:
continue
break
for x, y in identifier_dict.items():
if(y > 40):
print("identifier: {} amount of times found: {}".format(x, y))
我应该实施什么算法来加快这个过程?
编辑 我尝试将 numpy 数组折叠为一维数组,因此它有 12000 个值。这对速度没有实际影响。最新测试是 33 秒
【问题讨论】:
-
所以 ndarray 就像:
a = np.random.randint(0, 5700000, (1200,10))? -
...
NameError: name 'vector_pos' is not defined:请提供minimal reproducible example。 -
@wwii 确实,将 vector_pos 固定为 numpy_value
-
内部 for 循环中的
break语句导致循环在第一次迭代后停止。这是故意的吗? -
当 numpy
标签: python algorithm performance csv numpy