代码之家  ›  专栏  ›  技术社区  ›  Shamoon

我无法加载刚保存的numpy.npy文件

  •  0
  • Shamoon  · 技术社区  · 6 年前

    保存 numpy 阵列,我在做:

    save_tokens = 'myfile.npy'
    token_file = open(save_tokens, 'ab')
    
    tokens = np.array([], dtype='object')
    line_count = 0
    tokens_to_save = np.array([], dtype='object')
    with open(self.corpus_file) as infile:
        for line in infile:
            if line_count % 1000 == 0:
                print("Line Count: ", line_count, '')
                if save_tokens is not None:
                    np.save(token_file, tokens_to_save)
                    tokens_to_save = np.array([], dtype='object')
            line_count += 1
            line_tokens = pygments.lex(line + '\n', self.lexer)
            for line_token in line_tokens:
                tokens = np.append(tokens, line_token[1])
                tokens_to_save = np.append(tokens_to_save, line_token[1])
            if line_count % 10000 == 0:
                print("\tToken Count: ", len(tokens))
    
    np.save(token_file, tokens_to_save)
    

    我可以确认它保存了一个名为 myfile.npy 那是1.8MB。

    f = open('myfile.npy', 'rb')
    self.tokens = np.load(f, allow_pickle=True)
    [print(token) for token in self.tokens]
    print(self.tokens)
    f.close()
    return self.tokens
    

    我也试过:

    self.tokens = np.load('myfile.npy', allow_pickle=True)
    [print(token) for token in self.tokens]
    print(self.tokens)
    return self.tokens
    

    它打印一个空列表 [] . 怎么可能是空的?

    1 回复  |  直到 6 年前
        1
  •  1
  •   hpaulj    6 年前

    让我们对一个文件进行几次保存:

    In [92]: ofile = open('test.npy', 'ab')                                                                         
    In [93]: np.save(ofile, np.array([], object))                                                                   
    In [94]: arr = np.array([], object)                                                                             
    In [95]: arr = np.append(arr, np.array([1,2,3]))                                                                
    In [96]: arr                                                                                                    
    Out[96]: array([1, 2, 3], dtype=object)
    In [97]: arr = np.append(arr, np.array([1,2,3]))                                                                
    In [98]: arr                                                                                                    
    Out[98]: array([1, 2, 3, 1, 2, 3], dtype=object)
    In [99]: np.save(ofile, arr)                                                                                    
    In [100]: np.save(ofile, np.arange(12).reshape(3,4))                                                            
    In [101]: ofile.close() 
    

    做普通负载:

    In [103]: np.load('test.npy', allow_pickle=True)                                                                
    Out[103]: array(['✪'], dtype=object)
    

    看来我拿到原版了 []

    而是打开文件,并尝试重复加载:

    In [107]: f = open('test.npy', 'rb')                                                                            
    In [108]: np.load(f, allow_pickle=True)                                                                         
    Out[108]: array(['✪'], dtype=object)             # one 
    In [109]: np.load(f, allow_pickle=True)                                                                         
    Out[109]: array([], dtype=object)                # two
    In [110]: np.load(f, allow_pickle=True)                                                                         
    Out[110]: array([1, 2, 3, 1, 2, 3], dtype=object)    # three
    In [111]: np.load(f, allow_pickle=True)              # four                                                           
    Out[111]: 
    array([[ 0,  1,  2,  3],
           [ 4,  5,  6,  7],
           [ 8,  9, 10, 11]])
    In [112]: np.load(f, allow_pickle=True)                                                                         
    ---------------------------------------------------------------------------
    EOFError 
    

    所以,是的,可以将多个数组保存并加载到一个文件中,但这并不是它的初衷。 np.savez 用于保存多个文件。保存对象数据类型数组可能会有问题。它们的数据缓冲区有指向内存中其他位置的对象的指针。指针在保存/加载序列中无效。因此,它必须使用酸洗。

        2
  •  1
  •   BenT    6 年前

    您不需要打开文件来读取或输出 numpy 数组使用 np.load np.save

    应保存以下内容:

    np.save('myfile',tokens_to_save)
    

    self.tokens = np.load('myfile.npy', allow_pickle=True)
    

    编辑

    不能迭代地保存 努比 像这样排列。如果您想迭代地执行,请将其保存为文本文件。 np.保存 会覆盖你称之为超时的文件。考虑下面的例子,

    np.save('test',np.arange(0,100,10))
    
    np.save('test',np.arange(0,200,10))
    p = np.load('test.npy',)
    
    print p
    

    读入时的唯一输出是 [ 0 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190]