以下R索引平均代码的等效Python

以下R索引平均代码的等效Python,python,r,Python,R,下面的命令用于查找数组中特定x、y、z索引的平均值。在我试图解决的更大的问题中,2Gb文件被读入4D数组,其中前3个维度与空间x、y、z相关,第4个维度是时间。自从编写了这个R脚本之后,我就开始使用python来读取包含数据的2Gb文件,并希望将下面的R脚本行转换为python,这样我就可以用一种语言完成这一切。有人知道这方面的等效Python吗 # create a small example dataset for testing out script test_dat <- arra

下面的命令用于查找数组中特定x、y、z索引的平均值。在我试图解决的更大的问题中,2Gb文件被读入4D数组,其中前3个维度与空间x、y、z相关,第4个维度是时间。自从编写了这个R脚本之后,我就开始使用python来读取包含数据的2Gb文件,并希望将下面的R脚本行转换为python,这样我就可以用一种语言完成这一切。有人知道这方面的等效Python吗

# create a small example dataset for testing out script
test_dat <- array(rnorm(10*10*4*50), dim=c(10,10,4,50))  

# create a list of specific indices I want the average of (arbitrary
# in this case, but not in the larger problem at hand)
xyz_index <- list(c(2,10,1), c(4,5,1), c(6,7,1), c(9,3,1)) 

# bind the index data into a matrix for the next step
m <- do.call(rbind,xyz_index) ## 4x3 matrix 

# will return the average of the values in test_dat that are 
# in the positions listed in xyz_index for each time index 
# (50 values in this small problem)
sapply(seq(dim(test_dat)[4]), function(i) mean(test_dat[cbind(m,i)])) 

我想这就是你想要的,请确认。 事实证明,在numpy中连接多个切片是非常痛苦的,显然在其他情况下也是如此。你真的不想写这样晦涩难懂的代码


顺便说一句,我还研究了

如果从R迁移的唯一原因是内存问题,那么就解决它们,您可以迭代地这样做,而不需要将整个图像保留在内存中。或者,如果您想在Python中执行此操作,请使用NumPy数组和/或PIL。您正在尝试解决一个非问题。smci,我没有将整个图像保留在内存中,这就是使用python脚本处理2Gb 4d数组信息的美妙之处。因此,我可以留在一个环境中,我仍然想知道对于以下R位,等效的python是什么?:“sapplyseqdimtest_dat[4],function I meanset_dat[cbindm,I]”您也不需要将其保存在R中的内存中。无论您是在R中还是在python numpy中执行此操作,一种通用的更高效的内存方法是屏蔽选定的x,y,3D阵列的z元素,然后应用平均滤波器,并将其扫过t轴t=1..50
import numpy as np

test_dat = np.random.randn(10,10,4,50)

#xyz_index <- list(c(2,10,1), c(4,5,1), c(6,7,1), c(9,3,1))     
#m <- do.call(rbind,xyz_index) ## 4x3 matrix 
# I'm not sure about this, but it seems to get the 4x50 submatrix of concatenated slices
# see https://stackoverflow.com/questions/21349133/numpy-array-integer-indexing-in-more-than-one-dimension
m = np.r_[ '0,2', test_dat[2,9,1,:], test_dat[4,5,1,:], test_dat[6,7,1,:], test_dat[9,3,1,:] ]

# Compute .mean() of values, sweep over t-axis
#sapply(seq(dim(test_dat)[4]), function(i) mean(test_dat[cbind(m,i)]))
m.mean(axis=1)