python 多维数组同时计算_PythonNumPy中多维数组的条件化与运算

我有一个三维数组(如下,z),例如,在时间上代表一系列2D数组(如下,a1和a2)。我想为所有这些2D数组沿它们的轴选择一些值(下面两个参考轴(x和y)的条件),然后对产生的“较小”2D数组执行一些操作(例如,平均值、求和…)。在

下面的代码提出了几种方法。我发现solution1非常不雅观,但它似乎比solution2快。为什么会这样呢?有没有更好的方法(更简洁、更高效(速度和内存))?在

关于步骤2,哪一个是最好的选择,还有其他更有效的方法吗?为什么计算C2不起作用?谢谢!

[灵感来源:Get mean of 2D slice of a 3D array in numpy]import numpy

import time

# Control parameters (to be modified to make different tests)

xx=1000

yy=6000

# Some 2D arrays, z is a 3D array containing a succesion of such arrays (2 here)

a1=numpy.arange(xx*yy).reshape((yy, xx))

a2=numpy.linspace(0,100, num=xx*yy).reshape((yy, xx))

z=numpy.array((a1, a2))

# Axes x and y along which conditioning for the 2D arrays is made

x=numpy.arange(xx)

y=numpy.arange(yy)

# Condition is on x and y, to be applied on a1 and a2 simultaneously

xmin, xmax = xx*0.4, xx*0.8

ymin, ymax = yy*0.2, yy*0.5

xcond = numpy.logical_and(x>=xmin, x<=xmax)

ycond = numpy.logical_and(y>=ymin, y<=ymax)

def solution1():

xcond2D = numpy.tile(xcond, (yy, 1))

ycond2D = numpy.tile(ycond[numpy.newaxis].transpose(), (1, xx))

xymask = numpy.logical_not(numpy.logical_and(xcond2D, ycond2D))

xymaskzdim = numpy.tile(xymask, (z.shape[0], 1, 1))

return numpy.ma.MaskedArray(z, xymaskzdim)

def solution2():

return z[:,:,xcond][:,ycond, :]

start=time.clock()

z1=solution1()

end=time.clock()

print "Solution1: %s sec" % (end-start)

start=time.clock()

z2=solution2()

end=time.clock()

print "Solution2: %s sec" % (end-start)

# Step 2

# Now compute some calculation on the resulting z1 or z2

print "A1: ", z2.reshape(z2.shape[0], z2.shape[1]*z2.shape[2]).mean(axis=1)

print "A2: ", z1.reshape(z1.shape[0], z1.shape[1]*z1.shape[2]).mean(axis=1)

print "B1: ", z2.mean(axis=2).mean(axis=1)

print "B2: ", z1.mean(axis=2).mean(axis=1)

print "Numpy version: ", numpy.version.version

print "C1: ", z2.mean(axis=(1, 2))

print "C2: ", z1.mean(axis=(1, 2))

输出:

^{pr2}$

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值