循环中的 3 个维度反转 np.reshape
Reverse np.reshape for 3 dimensions in loop
我正在尝试为网格化数据集(月数:192(等于 16 年),纬度数:36,数经度:72)。为了从每个月中减去平均值,我将 TheData 重塑为 (192/12, 12),每 12 个月 16 年。之后我需要将数据恢复到原来的形状 (192, 36, 72)。
我在 3 个暗淡的重塑中挣扎。我猜第一个或最后一个有问题 np.reshape,但不确定。
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
我期待额外的 "fifth (192, 36, 72)"。
如果你能帮忙就太好了!
在您提供更多信息后重写我的答案:
你有:
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))
我不能确定对数据所做的就是你想要的,但是使用随机数据我能够得到这个 运行 并且看起来它会在至少能帮助您走上正轨,去做您想做的事。
我正在尝试为网格化数据集(月数:192(等于 16 年),纬度数:36,数经度:72)。为了从每个月中减去平均值,我将 TheData 重塑为 (192/12, 12),每 12 个月 16 年。之后我需要将数据恢复到原来的形状 (192, 36, 72)。
我在 3 个暗淡的重塑中挣扎。我猜第一个或最后一个有问题 np.reshape,但不确定。
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
我期待额外的 "fifth (192, 36, 72)"。
如果你能帮忙就太好了!
在您提供更多信息后重写我的答案:
你有:
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))
我不能确定对数据所做的就是你想要的,但是使用随机数据我能够得到这个 运行 并且看起来它会在至少能帮助您走上正轨,去做您想做的事。