2017-05-23 1 views
0

두 개의 월별 데이터 세트가 있습니다. 나중에 비교하고 싶습니다. 입력은 두 데이터 모두에 대해 이렇게 보입니다. 그 결과는 출력을 원하는 방식입니다.여러 2D 배열에서 3D 배열 만들기

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의 동일한 공간 해상도를 갖지만 시간 길이는 다릅니다. 그 중 한 달에 누락 된 달이 몇 달 있기 때문에 둘만의 데이터가있는 개월 만 선택했는지 확인했습니다. 그래서 두 데이터 세트가 이번 달을 포함하면 위도와 경도 값에 따라 데이터가 저장되는 2 차원 배열을 만들었습니다. 결국 나는 같은 길이의 data1과 data2에 대한 3 차원 배열을 원합니다.

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]) 

스크립트는 오류없이 실행되지만, 실제로는 매우 긴 1D 배열 만 가져옵니다.

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

어떻게 3 차원 배열로 배열 할 수 있습니까?

+0

이 제품을 찾고 계십니까? https://stackoverflow.com/questions/18595488/combining-2d-arrays-to-3d-arrays – kazemakase

+0

입력 및 원하는 출력의 예를 제공 할 수 있습니까? – Allen

+0

예, 이것과 비슷하지만 세 번째 축을 따라 이들을 함께 쌓기를 원했습니다. 시간과 행과 열이 섞이지 않았습니다. @kazemakase –

답변

0

나를 위해 다음과 같이 작업했습니다. 나는 경도와 위도의 고정 된 치수와 시간 축의 정의되지 않은 길이로 3 차원 배열을 정의했습니다.

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

그런 다음 시간 축을 따라 2 차원 출력을 추가했습니다.

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