Jai*_*ime 6 python memory arrays transpose numpy
我有一个很大的压缩文件(5000列×1M行),由0和1组成:
0 1 1 0 0 0 1 1 1....(×5000)
0 0 0 1 0 1 1 0 0
....(×1M)
Run Code Online (Sandbox Code Playgroud)
我想对它进行转置,但是使用numpy或其他方法只会将整个表加载到RAM中,而我只能使用6GB。
因此,我想使用一种方法将每条转置的行写入一个打开的文件,而不是将其存储在RAM中。我想出了以下代码:
import gzip
with open("output.txt", "w") as out:
with gzip.open("file.txt", "rt") as file:
number_of_columns = len(file.readline().split())
# iterate over number of columns (~5000)
for column in range(number_of_columns):
# in each iteration, go to the top line to start again
file.seek(0)
# initiate list storing the ith column's elements that will form the transposed column
transposed_column = []
# iterate over lines (~1M), storing the ith element in the list
for line in file:
transposed_column.append(line.split()[column])
# write the transposed column as a line to an existing file and back again
out.write(" ".join(transposed_column) + "\n")
Run Code Online (Sandbox Code Playgroud)
但是,这非常慢。有人可以建议我其他解决方案吗?有什么方法可以将列表作为列(而不是行)附加到现有的打开文件中?(伪代码):
with open("output.txt", w) as out:
with gzip.open("file.txt", rt) as file:
for line in file:
transposed_line = line.transpose()
out.write(transposed_line, as.column)
Run Code Online (Sandbox Code Playgroud)
更新
user7813790的答案将我引到以下代码:
import numpy as np
import random
# create example array and write to file
with open("array.txt", "w") as out:
num_columns = 8
num_lines = 24
for i in range(num_lines):
line = []
for column in range(num_columns):
line.append(str(random.choice([0,1])))
out.write(" ".join(line) + "\n")
# iterate over chunks of dimensions num_columns×num_columns, transpose them, and append to file
with open("array.txt", "r") as array:
with open("transposed_array.txt", "w") as out:
for chunk_start in range(0, num_lines, num_columns):
# get chunk and transpose
chunk = np.genfromtxt(array, max_rows=num_columns, dtype=int).T
# write out chunk
out.seek(chunk_start+num_columns, 0)
np.savetxt(out, chunk, fmt="%s", delimiter=' ', newline='\n')
Run Code Online (Sandbox Code Playgroud)
它采用如下矩阵:
0 0 0 1 1 0 0 0
0 1 1 0 1 1 0 1
0 1 1 0 1 1 0 0
1 0 0 0 0 1 0 1
1 1 0 0 0 1 0 1
0 0 1 1 0 0 1 0
0 0 1 1 1 1 1 0
1 1 1 1 1 0 1 1
0 1 1 0 1 1 1 0
1 1 0 1 1 0 0 0
1 1 0 1 1 0 1 1
1 0 0 1 1 0 1 0
0 1 0 1 0 1 0 0
0 0 1 0 0 1 0 0
1 1 1 0 0 1 1 1
1 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1
1 1 1 1 0 1 0 1
1 0 1 1 1 0 0 0
0 1 0 1 1 1 1 1
1 1 1 1 1 1 0 1
0 0 1 1 0 1 1 1
0 1 1 0 1 1 0 1
0 0 1 0 1 1 0 1
Run Code Online (Sandbox Code Playgroud)
并在二维尺寸等于列数(在这种情况下为8)上的2D块上进行迭代,将它们转置并将其附加到输出文件中。
第一块换位:
[[0 0 0 1 1 0 0 1]
[0 1 1 0 1 0 0 1]
[0 1 1 0 0 1 1 1]
[1 0 0 0 0 1 1 1]
[1 1 1 0 0 0 1 1]
[0 1 1 1 1 0 1 0]
[0 0 0 0 0 1 1 1]
[0 1 0 1 1 0 0 1]]
Run Code Online (Sandbox Code Playgroud)
第二块换位:
[[0 1 1 1 0 0 1 1]
[1 1 1 0 1 0 1 0]
[1 0 0 0 0 1 1 0]
[0 1 1 1 1 0 0 0]
[1 1 1 1 0 0 0 0]
[1 0 0 0 1 1 1 0]
[1 0 1 1 0 0 1 0]
[0 0 1 0 0 0 1 0]]
Run Code Online (Sandbox Code Playgroud)
等等
我正在尝试使用out.seek()将每个新块作为列追加到out文件中。据我所知,seek()将距文件开头(即列)的偏移量作为第一个参数,将0作为第二个参数表示从第一行再次开始。因此,我猜想下面的代码可以解决问题:
out.seek(chunk_start+num_columns, 0)
Run Code Online (Sandbox Code Playgroud)
但相反,它不会在接下来的行中以该偏移量继续。而且,它在第一行的开头添加了n = num_columns个空格。输出:
0 0 0 1 0 1 1 1 0 1 1 0 1 0 0 0
1 1 0 1 1 0 1 0
1 1 1 0 1 1 1 1
1 1 1 1 1 1 0 0
1 0 1 1 1 0 1 1
1 1 0 1 1 1 1 1
1 0 0 1 0 1 0 0
1 1 0 1 1 1 1 1
Run Code Online (Sandbox Code Playgroud)
关于如何正确使用seek()来完成此任务的任何见解?即生成此:
0 0 0 1 1 0 0 1 0 1 1 1 0 0 1 1 0 1 1 0 1 0 0 0
0 1 1 0 1 0 0 1 1 1 1 0 1 0 1 0 1 1 0 1 1 0 1 0
0 1 1 0 0 1 1 1 1 0 0 0 0 1 1 0 1 1 1 0 1 1 1 1
1 0 0 0 0 1 1 1 0 1 1 1 1 0 0 0 1 1 1 1 1 1 0 0
1 1 1 0 0 0 1 1 1 1 1 1 0 0 0 0 1 0 1 1 1 0 1 1
0 1 1 1 1 0 1 0 1 0 0 0 1 1 1 0 1 1 0 1 1 1 1 1
0 0 0 0 0 1 1 1 1 0 1 1 0 0 1 0 1 0 0 1 0 1 0 0
0 1 0 1 1 0 0 1 0 0 1 0 0 0 1 0 1 1 0 1 1 1 1 1
Run Code Online (Sandbox Code Playgroud)
请注意,这只是一个虚拟测试矩阵,实际矩阵为5008列×> 1M行。
更新2
我已经弄清楚了如何进行这项工作,它还可以利用任何尺寸的块。
import numpy as np
import random
# create example array and write to file
num_columns = 4
num_lines = 8
with open("array.txt", "w") as out:
for i in range(num_lines):
line = []
for column in range(num_columns):
line.append(str(random.choice([0,1])))
out.write(" ".join(line) + "\n")
# iterate over chunks of dimensions num_columns×chunk_length, transpose them, and append to file
chunk_length = 7
with open("array.txt", "r") as array:
with open("transposed_array.txt", "w") as out:
for chunk_start in range(0, num_lines, chunk_length):
# get chunk and transpose
chunk = np.genfromtxt(array, max_rows=chunk_length, dtype=str).T
# write out chunk
empty_line = 2 * (num_lines - (chunk_length + chunk_start))
for i, line in enumerate(chunk):
new_pos = 2 * num_lines * i + 2 * chunk_start
out.seek(new_pos)
out.write(f"{' '.join(line)}{' ' * (empty_line)}"'\n')
Run Code Online (Sandbox Code Playgroud)
在这种情况下,它需要一个这样的数组:
1 1 0 1
0 0 1 0
0 1 1 0
1 1 1 0
0 0 0 1
1 1 0 0
0 1 1 0
0 1 1 1
Run Code Online (Sandbox Code Playgroud)
并使用4列×7行的块将其转置,因此第一个块将是
1 0 0 1 0 1 0
1 0 1 1 0 1 1
0 1 1 1 0 0 1
1 0 0 0 1 0 0
Run Code Online (Sandbox Code Playgroud)
它被写入文件,从内存中删除,然后第二个块是
0
1
1
1
Run Code Online (Sandbox Code Playgroud)
并将其附加到文件中,因此最终结果是:
1 0 0 1 0 1 0 0
1 0 1 1 0 1 1 1
0 1 1 1 0 0 1 1
1 0 0 0 1 0 0 1
Run Code Online (Sandbox Code Playgroud)
在工作但缓慢的解决方案中,您正在读取输入文件5,000次-不会很快,但是最大程度减少读取次数的唯一简便方法就是在内存中全部读取。
您可以尝试一些折衷方案,例如一次读取50列到内存(〜50MB),然后将它们作为行写入文件。这样,您将“仅”读取文件100次。尝试几种不同的组合,以获得您满意的性能/内存折衷。
您可以在三个嵌套循环中执行此操作:
在最内层的循环中,将列值作为一行收集到二维数组中,中间的每个循环各占一行。在最外面的循环中,您在进入内部循环之前先清除数组,然后将其作为行打印到文件中。对于循环1的每次迭代,您将写出50行,每百万列。
如果不将整个目标文件加载到内存中,就不能真正插入普通文件的中间,您需要手动向前移动尾随字节。但是,由于您知道确切的文件大小,因此可以预先分配文件大小,并在写入每个字节时始终寻求位置。可能不是非常快地完成50亿次搜索,或者...如果您的1和0相当均匀地分布,则可以使用全零初始化文件,然后仅写入1(或相反)以将数字减半寻找。
编辑:添加了详细信息如何可以实现分块。