初始化多个Numpy数组(多个分配)-类似于MATLAB Deal() [英] Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()

查看:103
本文介绍了初始化多个Numpy数组(多个分配)-类似于MATLAB Deal()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找不到任何描述如何执行此操作的信息,这导致人们相信我没有以适当的惯用Python方式执行此操作.有关实现此操作的适当" Python方法的建议也将不胜感激.

I was unable to find anything describing how to do this, which leads to be believe I'm not doing this in the proper idiomatic Python way. Advice on the 'proper' Python way to do this would also be appreciated.

我正在编写的数据记录器有很多变量(任意记录长度,已知最大长度).在MATLAB中,我会将它们全部初始化为长度为n的零的一维数组,n比我见过的条目数大,在日志记录循环中分配每个单独的元素variable(measurement_no) = data_point,并在出现以下情况时修剪多余的零测量结束.初始化看起来像这样:

I have a bunch of variables for a datalogger I'm writing (arbitrary logging length, with a known maximum length). In MATLAB, I would initialize them all as 1-D arrays of zeros of length n, n bigger than the number of entries I would ever see, assign each individual element variable(measurement_no) = data_point in the logging loop, and trim off the extraneous zeros when the measurement was over. The initialization would look like this:

[dData gData cTotalEnergy cResFinal etc] = deal(zeros(n,1));

是否可以在Python/NumPy中做到这一点,所以我不必将每个变量放在自己的行上:

Is there a way to do this in Python/NumPy so I don't either have to put each variable on its own line:

dData = np.zeros(n)
gData = np.zeros(n)
etc.

我还不希望仅制作一个大矩阵,因为跟踪哪个列是哪个变量是令人不愉快的.也许解决方案是制作(length x numvars)矩阵,并将列切片分配给各个变量?

I would also prefer not just make one big matrix, because keeping track of which column is which variable is unpleasant. Perhaps the solution is to make the (length x numvars) matrix, and assign the column slices out to individual variables?

假设到结束时,我将拥有许多长度相同的向量;例如,我的后期处理将获取每个日志文件,计算出一堆单独的指标(> 50),将其存储,然后重复进行直到所有日志都被处理为止.然后我生成直方图,均值/最大值/西格玛斯/等.对于我计算出的所有各种指标.由于在Python中初始化50多个向量显然不容易,因此最佳的方式(最简洁的代码和良好的性能)是什么?

Assume I'm going to have a lot of vectors of the same length by the time this is over; e.g., my post-processing takes each log file, calculates a bunch of separate metrics (>50), stores them, and repeats until the logs are all processed. Then I generate histograms, means/maxes/sigmas/etc. for all the various metrics I computed. Since initializing 50+ vectors is clearly not easy in Python, what's the best (cleanest code and decent performance) way of doing this?

推荐答案

如果您真的很愿意以单线方式执行此操作,则可以创建一个由零组成的(n_vars, ...)数组,然后将其沿第一个维度解压缩:

If you're really motivated to do this in a one-liner you could create an (n_vars, ...) array of zeros, then unpack it along the first dimension:

a, b, c = np.zeros((3, 5))
print(a is b)
# False

另一种选择是使用列表推导或生成器表达式:

Another option is to use a list comprehension or a generator expression:

a, b, c = [np.zeros(5) for _ in range(3)]   # list comprehension
d, e, f = (np.zeros(5) for _ in range(3))   # generator expression
print(a is b, d is e)
# False False

但是要小心!您可能会认为,在包含对np.zeros()的调用的列表或元组上使用*运算符将实现相同的结果,但是不会:

Be careful, though! You might think that using the * operator on a list or tuple containing your call to np.zeros() would achieve the same thing, but it doesn't:

h, i, j = (np.zeros(5),) * 3
print(h is i)
# True

这是因为元组内部的表达式首先被求值.因此,np.zeros(5)仅被调用一次,并且重复的元组中的每个元素最终都是对同一数组的引用.这就是为什么不能只使用a = b = c = np.zeros(5)的原因.

This is because the expression inside the tuple gets evaluated first. np.zeros(5) therefore only gets called once, and each element in the repeated tuple ends up being a reference to the same array. This is the same reason why you can't just use a = b = c = np.zeros(5).

除非您确实需要分配大量的空数组变量,并且您真的非常在意使代码紧凑(!),否则我建议在单独的行上对其进行初始化以提高可读性.

Unless you really need to assign a large number of empty array variables and you really care deeply about making your code compact (!), I would recommend initialising them on separate lines for readability.

这篇关于初始化多个Numpy数组(多个分配)-类似于MATLAB Deal()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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