2015-09-07 98 views
0

當使一個2 d網格在python兩個1 d陣列,我通常使用numpy.meshgrid如下面:轉換2- d陣列1-d對應的陣列在Python

x = np.arange(10) 
y = np.arange(9) 
xy = np.meshgrid(x,y) 

然而,我的問題是關於這個過程的逆向。我有3個二維數組。每個陣列都表示經度和相應的高度。沒有任何方法可以將這些2-d網格轉換爲python中相互對應的1-d數組? 樣品陣列顯示如下圖所示:

x= 
[[-104.09417725 -104.08866882 -104.0831604 ..., -103.8795166 -103.87399292 
-103.86849976] 
[-104.09458923 -104.08908081 -104.08358765 ..., -103.87991333 
-103.87438965 -103.86889648] 
[-104.09500122 -104.08950806 -104.08401489 ..., -103.88031006 
-103.87481689 -103.86932373] 
..., 
[-104.11058044 -104.10507202 -104.09954834 ..., -103.89535522 
-103.88983154 -103.88430786] 
[-104.11100769 -104.10548401 -104.09997559 ..., -103.89575195 
-103.89022827 -103.88470459] 
[-104.11141968 -104.10591125 -104.10038757 ..., -103.89614868 -103.890625 
-103.88513184]] 

y= 
[[ 40.81712341 40.81744385 40.81776428 ..., 40.82929611 40.82960129 
40.82990646] 
[ 40.82128525 40.8216095 40.82191849 ..., 40.83345795 40.83376694 
40.83407593] 
[ 40.8254509 40.82577515 40.82608795 ..., 40.83763123 40.83792877 
40.83824539] 
..., 
[ 40.97956848 40.9798851 40.98020172 ..., 40.99177551 40.99207687 
40.99238968] 
[ 40.98373413 40.98405457 40.98437119 ..., 40.99593735 40.99624252 
40.99655533] 
[ 40.98789597 40.9882164 40.98854065 ..., 41.00011063 41.00041199 
41.00072479]] 

z= 
[[ 1605.58544922 1615.62341309 1624.33911133 ..., 1479.11254883 
1478.328125 1476.13378906] 
[ 1618.58520508 1632.38305664 1645.36132812 ..., 1485.84899902 
1483.43847656 1481.36865234] 
[ 1640.63037109 1656.0925293 1667.14697266 ..., 1492.79797363 
1488.65686035 1487.40478516] 
..., 
[ 1599.78015137 1602.82250977 1610.40197754 ..., 1595.12097168 
1594.40551758 1597.87902832] 
[ 1597.80883789 1601.17883301 1607.41320801 ..., 1595.7421875 
1594.26452637 1597.90893555] 
[ 1596.03857422 1600.5690918 1606.30712891 ..., 1598.56982422 
1594.90454102 1594.07763672]] 

任何幫助或想法將非常感激。

預期陣列是: 如

x = 
[-104.09417725 -104.08866882 -104.0831604 ..., -103.8795166 -103.87399292 
-103.86849976] 
y = 
[ 40.82128525 40.8216095 40.82191849 ..., 40.83345795 40.83376694 
40.83407593] 
z = 
[ 1618.58520508 1632.38305664 1645.36132812 ..., 1485.84899902 
1483.43847656 1481.36865234] 
+1

您需要向我們展示瞭如何排列的樣子。 –

+1

列出樣本輸入數據的預期輸出? – Divakar

+0

@AnandSKumar我添加了示例數組,謝謝 – Isaac

回答

1

我想這可能工作。在所得到的陣列,每一行是一個XYZ組3個初始陣列:

In [105]: 
arr1 = np.random.random((2,3)) 
arr2 = np.random.random((2,3)) 
arr3 = np.random.random((2,3)) 

In [106]: 
arr1 
Out[106]: 
array([[ 0.95919623, 0.76646714, 0.07782125], 
     [ 0.82285529, 0.80274853, 0.28257592]]) 

In [107]: 
arr2 
Out[107]: 
array([[ 0.0575891 , 0.13063203, 0.11439967], 
     [ 0.83353859, 0.72917084, 0.14294741]]) 

In [108]: 
arr3 
Out[108]: 
array([[ 0.75823658, 0.09216087, 0.80364941], 
     [ 0.50705487, 0.24498723, 0.3719806 ]]) 

In [109]: 
np.dstack((arr1, arr2, arr3)).reshape((-1,3)) 

Out[109]: 
array([[ 0.95919623, 0.0575891 , 0.75823658], 
     [ 0.76646714, 0.13063203, 0.09216087], 
     [ 0.07782125, 0.11439967, 0.80364941], 
     [ 0.82285529, 0.83353859, 0.50705487], 
     [ 0.80274853, 0.72917084, 0.24498723], 
     [ 0.28257592, 0.14294741, 0.3719806 ]]) 
+0

謝謝您的建議CT朱。您似乎將(2,3)的三維二維數組堆棧到(6,3)的一維二維數組。其實這不是我想要的。我認爲我的問題本身不是很準確。我會嘗試在單獨的帖子中更準確地提出問題。感謝您的時間。 – Isaac

+0

看起來你只需要'x.flatten()'現在.... –

0

meshgrid產生用於每個輸入

In [235]: xx,yy=np.meshgrid([1,2,3],[4,5,6]) 

之一具有2D陣列相同的行

In [236]: xx 
Out[236]: 
array([[1, 2, 3], 
     [1, 2, 3], 
     [1, 2, 3]]) 

另一列有相同的列

In [237]: yy 
Out[237]: 
array([[4, 4, 4], 
     [5, 5, 5], 
     [6, 6, 6]]) 

恢復原件只是一個選擇行或列

In [238]: xx[0,:] 
Out[238]: array([1, 2, 3]) 

In [239]: yy[:,0] 
Out[239]: array([4, 5, 6]) 

x也有類似的問題,但不完全相同的行。所以你可以選擇一個而忽略其他的。或者你可以取它們的平均值

In [240]: xx.mean(axis=0) 
Out[240]: array([ 1., 2., 3.]) 

或者你可以扁平化陣列,保持所有值

In [241]: xx.flatten() 
Out[241]: array([1, 2, 3, 1, 2, 3, 1, 2, 3]) 

In [242]: xx.T.flatten() 
Out[242]: array([1, 1, 1, 2, 2, 2, 3, 3, 3]) 

y相似彭定康是不太明顯。和z?具有3個輸入的meshgrid將產生3個3d陣列。

或者你可以參加所有3成三維陣列

In [252]: np.dstack([xx,yy,xx+10]) 
Out[252]: 
array([[[ 1, 4, 11], 
     [ 2, 4, 12], 
     [ 3, 4, 13]], 

     [[ 1, 5, 11], 
     [ 2, 5, 12], 
     [ 3, 5, 13]], 

     [[ 1, 6, 11], 
     [ 2, 6, 12], 
     [ 3, 6, 13]]]) 

,並把這一回一個3列陣列

In [253]: np.dstack([xx,yy,xx+10]).reshape(-1,3) 
Out[253]: 
array([[ 1, 4, 11], 
     [ 2, 4, 12], 
     [ 3, 4, 13], 
     [ 1, 5, 11], 
     [ 2, 5, 12], 
     [ 3, 5, 13], 
     [ 1, 6, 11], 
     [ 2, 6, 12], 
     [ 3, 6, 13]])