我正在尝试将温度数据的4D矩阵重塑一年(形状:[月,日,纬度,经度])为3D矩阵(形状:[日,纬度,经度])。
我以为我已经找到了一种方法(参见下面的代码),但是当我检查两个矩阵中每个数据值的出现次数是否相同时,它们不是。显然我的方法失败了。
是否有人知道其他一些有效的方法(可能使用python函数)将多个维度的矩阵重塑为一个较少的矩阵?
P.S。我可以忍受" -9999.0价值观"在几天内在3D矩阵中徘徊数天< 31,因为我总能在以后掩饰它们。我的问题是,显然我无法将4D矩阵重塑为3D。 D.S.
import numpy as np
# Create some fake data
TEMP = np.random.randint(10.0, size=(12,31,40,60))
TEMP_ReSh = np.full(([372,40,60]), -9999.0)
M31 = [1, 3, 5, 7, 8, 10, 12]
M30 = [4, 6, 9, 11]
M28 = [2]
count = 0
for m in M31:
print('Reshaping months with 31 days')
for d in np.arange(31):
TEMP_ReSh[count, :, :] = TEMP[m-1,d,:,:]
count = count + 1
print('Done with months of 31 days')
for m in M30:
print('Reshaping months with 30 days')
for d in np.arange(30):
TEMP_ReSh[count, :, :] = TEMP[m-1,d,:,:]
count = count + 1
print('Done with months of 30 days')
for m in M28:
print('Reshaping February')
for d in np.arange(28):
TEMP_ReSh[count, :, :] = TEMP[m-1,d,:,:]
count = count + 1
print('Done with February')
# Check if the number of occurrences of each integer value are the same for the 4D and 3D matrices:
TEMP_check = np.zeros([10,1])
TEMP_ReSh_check = np.zeros([10,1])
for n in np.arange(10):
TEMP_check[n] = np.count_nonzero(TEMP == n)
TEMP_ReSh_check[n] = np.count_nonzero(TEMP_ReSh == n)
# Print the number of occurrences:
print(TEMP_check)
print(TEMP_ReSh_check)