Python 2.7 python3d数组。计算R的平方

Python 2.7 python3d数组。计算R的平方,python-2.7,numpy,Python 2.7,Numpy,我有2个3维的数组。我需要计算这些数据的平方。澄清 Array1.shape = Array2.shape = (100, 100, 10) 所以 如果传递了两个数组,stats.linregresse希望这两个数组是一维的 Array1[i:i+1,j:j+1,:]具有形状(1,1,10),因此它是三维的。因此,改用Array1[i,j,:]: import numpy as np import scipy.stats as stats Array1 = np.random.random

我有2个3维的数组。我需要计算这些数据的平方。澄清

Array1.shape = Array2.shape = (100, 100, 10) 
所以


如果传递了两个数组,
stats.linregresse
希望这两个数组是一维的

Array1[i:i+1,j:j+1,:]
具有形状
(1,1,10)
,因此它是三维的。因此,改用
Array1[i,j,:]

import numpy as np
import scipy.stats as stats

Array1 = np.random.random((100, 100, 10))
Array2 = np.random.random((100, 100, 10))
resultArray = np.ones(100*100).reshape(100,100)
for i in range(Array1.shape[0]):
        for j in range(Array1.shape[1]):
            slope, intercept, r_value, p_value, std_err = stats.linregress(
                Array1[i, j, :],
                Array1[i, j, :])
            R2 = r_value**2
            resultArray[ i , j ] = R2

print(resultArray)

谢谢你。我写的方式很有效,我的输入数据是错误的,但我会保留你的建议,因为你的代码比我的干净。@wfoschiera不要忘记,如果你愿意,你可以点击左边的箭头接受答案……谢谢你Saullo。所以,我在处理所有数据的时间上仍然存在问题。我说的是6000多个阵列的形状(800870,10)。任何想法都将受到欢迎。@wfoschiera:NumPy在一次NumPy函数调用中卸载大量计算时,具有速度优势。在这里,您正在对长度为10几十万次或数百万次的小数组调用
linregresse
。这是缓慢的秘诀。我看不到使用NumPy获得速度优势的明显方法,因为我看不到将多个调用所完成的工作打包为一个调用的方法。但是,如果您发布另一个显示代码的问题(包含所有6000个数组对),可能有人会给您一个建议。@unutbu:再次感谢您,但比这更复杂。现在,我正试图编写一个函数来计算R平方,我相信它比实际代码更聪明。我正在使用Modis数据(Terra和Aqua平台上的传感器),从10年的时间里提取一些统计数据。我会尝试以不同的方式思考,如果运行速度比现在快,我会在这里发布。
import numpy as np
import scipy.stats as stats

Array1 = np.random.random((100, 100, 10))
Array2 = np.random.random((100, 100, 10))
resultArray = np.ones(100*100).reshape(100,100)
for i in range(Array1.shape[0]):
        for j in range(Array1.shape[1]):
            slope, intercept, r_value, p_value, std_err = stats.linregress(
                Array1[i, j, :],
                Array1[i, j, :])
            R2 = r_value**2
            resultArray[ i , j ] = R2

print(resultArray)