IPython:如何自动加载npz文件并为变量赋值? [英] IPython: how to automagically load npz file and assign values to variables?

查看:642
本文介绍了IPython:如何自动加载npz文件并为变量赋值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python新手,我急切地从MATLAB迁移到IPython,作为我在实验室进行数据分析的首选语言。

I'm new to Python and I'm eagerly migrating from MATLAB to IPython as my preferred language for data analysis at the lab.

在MATLAB中,经过一段数据处理后,我会做

In MATLAB, after a session of data crunching, I would do

>>> save('myresults.mat','x','y','z');

并将变量x,y和z的结果保存在名为'myresults.mat'的文件中。稍后,我可以简单地说:

and save the results of the variables x, y and z in a file called 'myresults.mat'. Later on, I could simply say:

>>> load('myresults');

并且MATLAB将加载.mat文件并将存储的变量值分配给当前工作空间。

and MATLAB would load the .mat file AND assign the stored values of the variables to the current workspace.

我最近了解到我可以使用numpy为IPython做类似的事情,即:

I've recently learned that I can do similarly for IPython using numpy, namely:

import numpy as np
a = 2
b = np.sqrt(2)
np.savez('myresultsinpython',a,b)

稍后会做类似

npzfile = np.load('myresultsinpython')

然而,我得到的是一个对象,我可以访问我的变量:

However, what I get is an object from which I can access my variables by:

npzfile['arr_1']

等等,但我丢失了变量原始名称的所有信息。我知道我可以通过这样做来保存文件

etc., but I loose all info on the original names of the variables. I know I could save the file by doing

np.savez('myresultsinpython',a=a,b=b)

但这不是很有用,因为我仍然必须这样做:

but this is not that useful, as I still have to do something like:

npzfile['a']

访问变量的值。

如何加载文件并在我的工作区中创建变量,并根据文件中存储的值分配它们的值?

How do I both load the file and have the variables created in my workspace and their values assigned according to the values stored in the file?

np.load('myresults')之类的东西,能够做到 a + b 并获得 3.4142135623730949(= 2 + sqrt(2))作为回报。

Something like np.load('myresults') and be able to do a+b and get 3.4142135623730949 (=2+sqrt(2)) in return.

推荐答案

locals().update(npzfile)
a   # and/or b

在Ipython会话中, locals()是一个包含您已定义的变量,输入历史记录行和各种变量的大字典输出。 update npzfile 的字典值添加到较大的字典值。

In the Ipython session, locals() is a large dictionary with variables the you've defined, the input history lines, and various outputs. update adds the dictionary values of npzfile to that larger one.

顺便说一句,您还可以加载和保存MATLAB .mat文件。使用 scipy.io.loadmat savemat 。它处理 v4(级别1.0),v6和v7到7.2 文件。但是你有同样的问题 - 结果是字典。

By the way, you can also load and save MATLAB .mat files. Use scipy.io.loadmat and savemat. It handles v4 (Level 1.0), v6 and v7 to 7.2 files. But you have same issue - the result is a dictionary.

Octave有一个表达形式的load命令,它将数据加载到一个结构中

Octave has an expression form of the load command, that loads the data into a structure

S = load(file,options,v1,v2,...)

S = load ("file", "options", "v1", "v2", ...)

一般情况 numpy ipython 没有与MATLAB相同的保存工作空间快照的想法。我主要编写脚本来生成(并加载)必要的数据,并将它们作为独立的python会话运行,或者在ipython中运行它们。 ipython确实有很好的历史功能。您可能还会发现ipython笔记本工具很有用。我没有用过它。

In general numpy and ipython does not have the same sort of 'save a snapshot of the workspace' idea that MATLAB does. I mostly write scripts to generate (and load) the necessary data, and run them either as stand alone python sessions or run them from with in ipython. ipython does have a good history feature. You might also find the ipython notebook tool useful. I haven't used that.

在一个函数中, locals()与外界不同。它是每个功能的本地。有一个 globals()字典可能允许在函数外部设置变量。但是不鼓励这种编程风格。

Within a function, locals() is different from outside it. It is 'local' to each function. There is a globals() dictionary that might allow setting variables outside the function. But this style of programming is not encouraged.

Anurag 引用的SO问题指向 save_ipython_variables 包。 https://pypi.python.org/pypi/save_ipython_variables/0.0.3 它使用 pickle 保存变量,并通过执行类似

The SO question that Anurag cited points to a save_ipython_variables package. https://pypi.python.org/pypi/save_ipython_variables/0.0.3 It saves variables with pickle, and loads them by doing something like

exec '__builtins__[name] = pickle.load(...)

使用 exec __ builtins __ 它正在推动一些安全的编程边界。所以要谨慎使用。但是一个小测试在我的环境中起作用。相同的技术可能适用于 npz 文件。

With use of exec and __builtins__ it is pushing some safe programming boundaries. So use with caution. But a small test does work in my environment. The same technique might work with npz files.

这篇关于IPython:如何自动加载npz文件并为变量赋值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆