简体   繁体   English

如何将“n”个数组保存到“n”个npy文件?

[英]How to save 'n' arrays to 'n' npy files?

I have strings like these ones in a file:我在文件中有这样的字符串:

tactccctccatcccataatataagacatggtcaaacttggcacggtcttcaaaactaatctttaacttttaatttctca
gttggtatcagaggagtggtcctcggaggtttccgcgacggcgccgtccaccaaaccttccgtgcagacgcgtctgctgc
tgttatggtgggtaaaacatatgaaaggcggcccaagcagcccataggccaggccacgccatgagatcaagattaaaggg
tgttcaggccagcaatggacagtttccgggcactgtgccggctccgcgcgcctcggtcccggctcgcgctcgcgcgctcg
cgttatcagcacgagctgcctaagctaatagatactaaaaaaaaatttctgataaaaaaccatatttattatcgactcat
aaagtggtatcagagcttgaagatcctaaagatggcgagtaacaatgttcccttccaagtcccggtgctcacaaagagca
tggtatcagacagccgatcacaagcttccgctccctaacccatcgccgacgacctcaccggccaccacatcctcttctcc
gctggaatagctcagttggttagagcgtgtggctgttaaccacaaggtcggaggttcaagccctccttctagcgcttttt
gagcactgctcccgtccagcaaacggtaccccaggtaccggtaccccggtacgaaacttaatctgaccattgaattagag
gtgttaacccagatcaaatgcctcgttccctgggccgcctcattgtgaggggaagtattgcgacaagtcactaactcttc
cttgagcagagagttgctcattggaatactacaatattcatatattttactggagcttctaaacggattccttcaccatg
aagtgtgatgattggaggtgtgcgggttcgagcgctgtacaactacaccggagaggagcctgatgagctctccttcaaat
gtccactgacctgtaatagagaactggattgctcatgagaccccgatcgttcacttgtaatacattgatagttttcttta
atcaacgggagatgtccaagaccgaataactaagcgaattagtttggacttatcagcgaaatttcagtgatgctgaaagc
aaaatcagtttatcaacgggagatgtccaagaccgaataactaagcgaattagtttggacttatcagcgaaatttcagtg
ctactatgtttaaaaatatgttgaacaaaaaattctctctgttaaaaaaaacagttgggaaatatttttcgaacaattaa
gtaatagccagtcgcacctctactgtactcgaaatacatgtcagtgagattaacattcattattctgaccagacggctta
ttggccatcaaatgagggaaacaggccagctcctcgacaaaaaaagtggcatgagatttatcaaaggattgagaaatgcc

I need to one-hot encode each line and save to a numpy binary file.我需要每一行进行一次热编码并保存到一个 numpy 二进制文件。 I wrote this function for the task.我为任务编写了这个函数。

def ohe(padded_file):
    integer_encoder = LabelEncoder()  
    one_hot_encoder = OneHotEncoder(categories='auto')   
    input_features = []
    with open(padded_file, 'r') as padded:
        padded = padded.readlines()
        arr_nums = [i for i in range(len(padded))]
        for linha in padded:
            linha = linha.strip()
            integer_encoded = integer_encoder.fit_transform(list(linha))
            integer_encoded = np.array(integer_encoded).reshape(-1, 1)
            one_hot_encoded = one_hot_encoder.fit_transform(integer_encoded)
            stacked = np.stack(one_hot_encoded.toarray())
            input_features.append(stacked)
        np.savez_compressed("seq_arrays.npz", *input_features)

It works, but if I need to handle a bigger file containing many thousands of lines and longer strings, depending on the RAM capacity of the computer it can't do the job.它可以工作,但是如果我需要处理包含数千行和更长字符串的更大文件,则取决于计算机的 RAM 容量,它无法完成这项工作。 So, for the latter case, I thougth I could write a numpy binary file for each line and then join in a same compressed file or even let them in a directory to be used when necessary.因此,对于后一种情况,我认为我可以为每一行编写一个 numpy 二进制文件,然后加入同一个压缩文件,甚至将它们放在一个目录中以在必要时使用。 WRT that I did the following: WRT我做了以下事情:

def ohe(padded_file):
    integer_encoder = LabelEncoder()  
    one_hot_encoder = OneHotEncoder(categories='auto')   
    with open(padded_file, 'r') as padded:
        padded = padded.readlines()
        arr_nums = [i for i in range(len(padded))]
        for linha in padded:
            linha = linha.strip()
            integer_encoded = integer_encoder.fit_transform(list(linha))
            integer_encoded = np.array(integer_encoded).reshape(-1, 1)
            one_hot_encoded = one_hot_encoder.fit_transform(integer_encoded)
            stacked = np.stack(one_hot_encoded.toarray())
            for num in arr_nums:
                save(f'arr_{num}.npy',stacked)

It creates a number of files based on the number of lines, but all of them store the same data, which is the last string one-hot encoded.它根据行数创建多个文件,但所有文件都存储相同的数据,即最后一个字符串单热编码。 I need to have the first file with the first line one-hot encoded, the second file with the second line one-hot encoded and so on.我需要将第一个文件的第一行单热编码,第二个文件的第二行单热编码,依此类推。 What am I doing wrong?我究竟做错了什么?

Use a construct that does not read the whole file but line by line使用不读取整个文件而是逐行读取的构造

    with open(padded_file, 'r') as padded:
        for i,linha in enumerated(padded):
            linha = linha.strip()
            # ....other stuff
            save(f'arr_{i}.npy',stacked)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM