当我在python中使用两个1-d数组制作2 d网格时,我通常使用numpy.meshgrid,如下所示:
x = np.arange(10)
y = np.arange(9)
xy = np.meshgrid(x,y)
然而,我的问题是关于这个过程的逆转。我有3个2-d阵列。每个阵列表示纬度和经度以及相应的高度。有没有办法将这些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]
答案 0 :(得分:1)
我认为这可行。在结果数组中,每一行都是3个初始数组的xyz集:
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 ]])
答案 1 :(得分:1)
meshgrid
为每个输入生成一个2d数组
In [235]: xx,yy=np.meshgrid([1,2,3],[4,5,6])
一个具有相同的行
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个加入3d数组
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]])