簡體   English   中英

從多個2D陣列創建3D陣列

[英]Create 3D array from multiple 2D arrays

我有兩個每月的網格數據集,以后想比較。 對於數據來說,輸入看起來都是這樣,這也是我想要輸出的方式。

In[4]: data1.shape
Out[4]: (444, 72, 144)

In[5]: gfz.shape
Out[5]: (155, 72, 144)

In[6]: data1
Out[6]: 
array([[[ 0.98412287,  0.96739882,  0.91172796, ...,  1.12651634,
          1.0682013 ,  1.07681048],
        [ 1.47803092,  1.44721365,  1.49585509, ...,  1.58934438,
          1.66956687,  1.57198083],
        [ 0.68730044,  0.76112831,  0.78218687, ...,  0.92582172,
          1.07873237,  0.87490368],
        ..., 
        [ 1.00752461,  1.00758123,  0.99440521, ...,  0.94128627,
          0.88981551,  0.93984401],
        [ 1.03467119,  1.02640462,  0.91580886, ...,  0.88302392,
          0.99204206,  0.96396238],
        [ 0.8280431 ,  0.82936555,  0.82637453, ...,  0.92009377,
          0.77890259,  0.81065702]],

       ..., 
       [[-0.12173297, -0.06624345, -0.02809682, ..., -0.04522502,
         -0.11502996, -0.22779272],
        [-0.61080372, -0.61958522, -0.52239478, ..., -0.6775983 ,
         -0.79460669, -0.70022893],
        [-0.12011283, -0.10849079,  0.096185  , ..., -0.45782232,
         -0.39763898, -0.31247514],
        ..., 
        [ 0.90601307,  0.88580155,  0.90268403, ...,  0.86414611,
          0.87041426,  0.86274058],
        [ 1.46445823,  1.31938004,  1.37585044, ...,  1.51378822,
          1.48515761,  1.49078977],
        [ 0.29749078,  0.22273554,  0.27161494, ...,  0.43205476,
          0.43777165,  0.36340511]],

       [[ 0.41008961,  0.44208974,  0.40928891, ...,  0.45899671,
          0.39472976,  0.36803097],
        [-0.13514084, -0.17332518, -0.11183424, ..., -0.22284794,
         -0.2532815 , -0.15402752],
        [ 0.28614867,  0.33750001,  0.48767376, ...,  0.01886483,
          0.07220326,  0.17406547],
        ..., 
        [ 1.0551219 ,  1.09540403,  1.19031584, ...,  1.09203815,
          1.07658005,  1.08363533],
        [ 1.54310501,  1.49531853,  1.56107259, ...,  1.57243073,
          1.5867976 ,  1.57728028],
        [ 1.1034857 ,  0.98658448,  1.14141166, ...,  0.97744882,
          1.13562942,  1.08589089]],

       [[ 1.02020931,  0.99780071,  0.87209344, ...,  1.11072564,
          1.01270151,  0.9222675 ],
        [ 0.93467152,  0.81068456,  0.68190312, ...,  0.95696563,
          0.84669352,  0.84596157],
        [ 0.97022212,  0.94228816,  0.97413743, ...,  1.06613588,
          1.08708596,  1.04224277],
        ..., 
        [ 1.21519053,  1.23492992,  1.2802881 , ...,  1.33915019,
          1.32537413,  1.27963519],
        [ 1.32051706,  1.28170252,  1.36266208, ...,  1.29100537,
          1.38395023,  1.34622073],
        [ 0.86108029,  0.86364979,  0.88489276, ...,  0.81707358,
          0.82471925,  0.83550251]]], dtype=float32)

因此,兩者具有相同的144x72空間分辨率,但時間長度不同。 由於其中之一缺少月份,因此我確保只有選中的月份都具有數據。 因此,我創建了一個二維數組,如果兩個數據集都包含本月,則根據數據的經度和緯度值存儲數據。 最后,我想為長度相同的data1和data2提供一個三維數組。

3Darray_data1 =[]
3Darray_data2=[]
xy_data1=[[0 for i in range(len(lons_data1))] for j in range(len(lats_data1))]
xy_data2=[[0 for i in range(len(lons_data2))] for j in range(len(lats_data2))] 

# comparing the time steps 
for i in range(len(time_data1)):
    for j in range(len(time_data2)):
        if time_data1.year[i] == time_data2[j].year and time_data1[i].month==time_data2[j].month:

            # loop for data1 which writes the data into a 2D array
            for x in range(len(lats_data1)):
                for y in range(len(lons_data1)):
                    xy_data1[x][y]=data1[j,0,x,y]

            # append to get an array of arrays                   
            xy_data1 = np.squeeze(np.asarray(xy_data1))
            3Darray_data1 = np.append(3Darray_data1,[xy_data1])

            # loop for data2 which writes the data into a 2D array
            for x in range(len(lats_data2)):
                for y in range(len(lons_data2)):
                    xy_data2[x][y]=data2[i,x,y]

            # append to get an array of arrays                    
            xy_data2 = np.squeeze(np.asarray(xy_data2))
            3Darray_data2 = np.append(3Darray_data2,[xy_data2])

該腳本運行沒有錯誤,但是,我只得到了一個很長的一維數組。

In[3]: 3Darray_data1
Out[3]: array([        nan,         nan,         nan, ...,  0.81707358,
        0.82471925,  0.83550251])

如何將其排列成三維陣列?

對我來說,我可以使用以下方法。 我定義了具有固定維度的經度和緯度以及未定義的時間軸長度的三維數組。

temp_data1 = np.zeros((0,len(lats_data1),len(lons_data1)))

然后,我沿着時間軸附加了二維輸出。

3Darray = np.append(3Darray,xy_data1[np.newaxis,:,:],axis=0)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM