反转np.重塑对于回路中的三维

2024-04-19 23:43:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图为网格数据集(月数:192(等于16年),纬度数:36,经度数:72)创建2004-2009年期间的异常(ClimSt ClimEd)。为了从每个月中减去平均数,相对而言,我把数据改成(192/12,12),每12个月16年。之后我需要把数据恢复到原来的形状(192,36,72)。你知道吗

我挣扎与3维重塑。我猜不是第一个就是最后一个np.重塑,但不确定。你知道吗

import matplotlib.pyplot as plt
import numpy as np

TheData = np.random.randint(0, 100, size=(192, 36, 72))
TheMDI = -1e30
NLats = 36
NLons = 72
ClimSt = 2004
ClimEd = 2009
NewSt = 2002

print('first', TheData.shape) #prints: first (192, 36, 72)
for lt in range(NLats): # range nlats goes through 0...36
    for ln in range(NLons): # range nlons goes through 0...72
        print('second', TheData.shape) #prints: second (192, 36, 72)
        TheData = np.reshape(TheData[:,lt,ln],(len(TheData)//12,12))
        for mm in range(12):
            print('third', TheData.shape) #prints 12x: third (16, 12)
            subarr=TheData[:,mm]
            subclim=subarr[ClimSt-NewSt:(ClimEd+1)-NewSt] # creates data from 2004-2009
            climgots=np.where(subclim > TheMDI)[0] # we want to add only existing data to 2004-2009
            gots=np.where(subarr > TheMDI)[0] # we want to add only existing data to 2002-2017
            if (len(climgots) > 15):
                subarr[gots]=subarr[gots]-np.mean(subclim[climgots])
            TheData[:,mm]=subarr
        print('fourth', TheData.shape) #prints: fourth (16, 12)
        TheData[:,lt,ln] = np.reshape(TheData,np.size(TheData)) #gives: IndexError: too many indices for array
        print('fifth', TheData.shape) #should print: fifth (192, 36, 72)

打印输出如下:

first (192, 36, 72)
second (192, 36, 72)
third (16, 12)
third (16, 12)
third (16, 12)
third (16, 12)
third (16, 12)
third (16, 12)
third (16, 12)
third (16, 12)
third (16, 12)
third (16, 12)
third (16, 12)
third (16, 12)
fourth (16, 12)
Traceback (most recent call last):
  File "testooo.py", line 27, in <module>
    TheData[:,lt,ln] = np.reshape(TheData,np.size(TheData))
IndexError: too many indices for array

我期待一个额外的“第五(192,36,72)”。你知道吗

如果你能帮上忙那就太好了!你知道吗


Tags: toinltfornprangeprintsprint
2条回答

在您提供更多信息后,请重新撰写我的回答:

您有:

    print('fourth', TheData.shape) #prints: fourth (16, 12)
    TheData[:,lt,ln] = np.reshape(TheData,np.size(TheData)) #gives: IndexError: too many indices for array
    print('fifth', TheData.shape) #should print: fifth (192, 36, 72)

这里的第二行,即TheData[:,lt,ln]给出了错误,因为TheData是一个二维数组,但您尝试使用TheData[:,lt,ln]

有一个问题不仅仅是你在那里的错误,我想我找到了一个方法来解决这两个问题,虽然我不是100%清楚你是怎么处理这些数据的。你知道吗

您有TheData作为您正在处理的整个数据集。但是,当在循环中执行TheData = np.reshape(TheData[:,lt,ln],(len(TheData)//12,12)操作时,会丢失索引lt,ln之外的所有数据。您需要保留数据并使用其他容器来完成这一中间工作。请参阅下面的代码,其中我为新数据创建了一个新容器,并为工作使用了一个中间数据变量:

TheData = np.random.randint(0, 100, size=(192, 36, 72))        

newData = np.empty((192, NLats, NLons))

for lt in range(NLats): # range nlats goes through 0...36
    for ln in range(NLons): # range nlons goes through 0...72
        intermediateData = np.reshape(TheData[:,lt,ln],(len(TheData)//12,12))
        for mm in range(12):
            subarr=intermediateData[:,mm]
            subclim=subarr[ClimSt-NewSt:(ClimEd+1)-NewSt] # creates data from 2004-2009
            climgots=np.where(subclim > TheMDI)[0] # we want to add only existing data to 2004-2009
            gots=np.where(subarr > TheMDI)[0] # we want to add only existing data to 2002-2017
            if (len(climgots) > 15):
                subarr[gots]=subarr[gots]-np.mean(subclim[climgots])
            intermediateData[:,mm]=subarr
        newData[:,lt,ln] = np.reshape(intermediateData,np.size(intermediateData))

我不能确定正在用数据做的事情是否就是你要做的,但是用随机数据我能够运行这个,看起来它至少会帮助你走上正轨,去做你想做的事情。你知道吗

相关问题 更多 >