简体   繁体   English

如何保存numpy数组,以便以后可以适当地加载它们?

[英]How do I save numpy arrays such that they can be loaded later appropriately?

I have a code which outputs an N-length Numpy array at every iteration. 我有一个代码,每次迭代输出一个N长度的Numpy数组。

Eg. 例如。 -- theta = [ 0, 1, 2, 3, 4 ] -theta = [0,1,2,3,4]

I want to be able to save the arrays to a text file or .csv file dynamically such that I can load the data file later and extract appropriately which array corresponds to which iteration. 我希望能够将数组动态保存到文本文件或.csv文件,以便以后可以加载数据文件并适当地提取哪个数组对应于哪个迭代。 Basically, it should be saved in an ordered fashion. 基本上,应按有序方式保存它。

I am assuming the data file would look something like this:- 我假设数据文件看起来像这样:

0 1 2 3 4 0 1 2 3 4

1 2 3 4 5 1 2 3 4 5

2 3 4 5 6 ... (Random output) 2 3 4 5 6 ...(随机输出)

I thought of using np.c_ but I don't want to overwrite the file at every iteration and if I simply save the terminal output as > output.txt, it saves as arrays including the brackets. 我考虑过使用np.c_,但我不想在每次迭代时都覆盖文件,如果我只是将终端输出另存为> output.txt,它将另存为包括括号在内的数组。 I don't know how to read such a text file. 我不知道如何读取这样的文本文件。

Is there a proper method to do this, ie write and read the data? 是否有适当的方法来执行此操作,即写入和读取数据?

I use pandas and openpyxl in order to store data I calculated with Python. 我使用pandas和openpyxl来存储用Python计算的数据。

Here an example in which I generate a numpy array, I convert it to a pandas dataframe and then I save it into an excel file located in "path" (remember that if you are working on Windows you need "r" at the beginning of the file path in order to convert the symbol "\\" to the symbol "\\\\") 在此示例中,我生成一个numpy数组,将其转换为pandas数据框,然后将其保存到位于“ path”中的excel文件中(请记住,如果您在Windows上工作,则在开始时需要“ r”为了将符号“ \\”转换为符号“ \\\\”的文件路径)

import numpy as np
import pandas as pd
from openpyxl import load_workbook

path = r"C:\Users\fedel\Desktop\excelData\fileName.xlsx"

data = np.random.randn(100)
data = pd.DataFrame(data)
n = 0
data.to_excel(path, sheet_name = 'sheet number ' + str(n)) # save data in an excel worksheet

Now you can open the "fileName.xlsx" file and see that you stored data on a work sheet named "sheet number 0". 现在,您可以打开“ fileName.xlsx”文件,并看到将数据存储在名为“工作表编号0”的工作表上。

Next step is to generate other data and save them in other worksheets, without deleting the first one 下一步是生成其他数据并将其保存在其他工作表中,而不删除第一个工作表

book = load_workbook(path) #load excel file 
writer = pd.ExcelWriter(path, engine = 'openpyxl') # use pandas to write in the some excel file
writer.book = book # write what you saved before in order to avoid "overwriting"

for n in range(1, 10):
    data = np.random.randn(100)
    data = pd.DataFrame(data)
    data.to_excel(writer, sheet_name = 'sheet number ' + str(n) ) # iteratively save data on different excel worksheets

writer.save()
writer.close()

When you want to open and analyze the data you stored then I suggest you to type 当您要打开并分析您存储的数据时,建议您键入

xls = pd.ExcelFile(path)
df1 = xls.parse(0)

if you want to open data from the first worksheet or 如果要从第一个工作表中打开数据,或者

xls = pd.ExcelFile(path)
df1 = xls.parse(1)

if you want to open data from the second one, you may even write 如果您想从第二个打开数据,您甚至可以编写

xls = pd.ExcelFile(path)
dataNames = xls.sheet_names
df1 = xls.parse(dataNames[0])

Are you looking for something like np.savetxt ? 您是否正在寻找类似np.savetxt东西?

If you want to append data to an existing file, you can open the file with append mode. 如果要将数据追加到现有文件,则可以使用追加模式打开文件。

with open('data.txt', 'a') as f:
    np.savetxt(f, newdata)

Check out this post Appending a matrix to an existing file using numpy 签出此帖子使用numpy将矩阵附加到现有文件

You can read the text file using np.loadtxt 您可以使用np.loadtxt读取文本文件

How about ndarray 's .tofile() method? ndarray.tofile()方法怎么样? To read use numpy.fromfile() . 要读取,请使用numpy.fromfile()

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

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