【问题标题】:How to write the ndarray to text file in python?如何将ndarray写入python中的文本文件?
【发布时间】:2015-11-08 11:29:37
【问题描述】:

我正在尝试将 MNIST 数据用于我的研究工作。现在数据集描述为:

training_data 以包含两个条目的元组形式返回。 第一个条目包含实际的训练图像。这是一个 具有 50,000 个条目的 numpy ndarray。每个条目依次是一个 numpy ndarray 有 784 个值,代表 28 * 28 = 784 单个 MNIST 图像中的像素。

The second entry in the ``training_data`` tuple is a numpy ndarray
containing 50,000 entries.  Those entries are just the digit
values (0...9) for the corresponding images contained in the first
entry of the tuple.

现在我正在像这样转换训练数据:

特别是,training_data 是一个包含 50,000 个 2 元组(x, y)x 是一个 784 维的 numpy.ndarray 包含输入图像。 y 是一个 10 维 numpy.ndarray 表示对应的单位向量 x 的正确数字。 代码是:

def load_data_nn():
    training_data, validation_data, test_data = load_data()
    #print training_data[0][1]
    #inputs = [np.reshape(x, (784, 1)) for x in training_data[0]]
    inputs = [np.reshape(x, (784,1)) for x in training_data[0]]
    print inputs[0]
    results = [vectorized_result(y) for y in training_data[1]]
    training_data = zip(inputs, results)
    test_inputs = [np.reshape(x, (784, 1)) for x in test_data[0]]
    return (training_data, test_inputs, test_data[1])

现在我想将输入写入一个文本文件,这意味着一行将是输入 [0],另一行将是输入 [1],输入 [0] 内的数据将用空格分隔,并且没有 ndarray 括号存在。例如:

 0 0.45 0.47 0,76

 0.78 0.34 0.35 0.56

文本文件中的这一行是inputs[0]。如何将ndarray转换为文本文件中的上述内容??

【问题讨论】:

    标签: python numpy


    【解决方案1】:

    由于您的问题的答案似乎很简单,我猜您的问题是速度。幸运的是,我们可以在这里使用多处理。 试试这个:

    from multiprocessing import Pool
    
    def joinRow(row):
        return ' '.join(str(cell) for cell in row)
    
    def inputsToFile(inputs, filepath):
        # in python3 you can do:
        # with Pool() as p:
        #     lines = p.map(joinRow, inputs, chunksize=1000)
        # instead of code from here...
        p = Pool()
        try:
            lines = p.map(joinRow, inputs, chunksize=1000)
        finally:
            p.close()
        # ...to here. But this works for both.
    
        with open(filepath,'w') as f:
            f.write('\n'.join(lines)) # joining already created strings goes fast
    

    在我这破笔记本电脑上仍然需要一段时间,但比 '\n'.join(' '.join(str(cell) for cell in row) for row in inputs) 快很多

    顺便说一句,您也可以加快其余代码的速度:

    def load_data_nn():
        training_data, validation_data, test_data = load_data()
        # suppose training_data[0].shape == (50000,28,28), otherwise leave it as is
        inputs = training_data[0].reshape((50000,784,1))
        print inputs[0]
        # create identity matrix and use entries of training_data[1] to
        # index corresponding unit vectors
        results = np.eye(10)[training_data[1]]
        training_data = zip(inputs, results)
        # suppose test_data[0].shape == (50000,28,28), otherwise leave it as is
        test_inputs = test_data[0].reshape((50000,784,1))
        return (training_data, test_inputs, test_data[1])
    

    【讨论】:

      猜你喜欢
      • 2018-10-28
      • 2017-05-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-31
      • 1970-01-01
      • 2023-02-08
      相关资源
      最近更新 更多