[英]Joining two 2D numpy arrays into a single 2D array of 2-tuples
我有兩個像這樣的2D numpy數組,代表三點之間的x / y距離。 我需要將x / y距離作為單個數組中的元組。
所以來自:
x_dists = array([[ 0, -1, -2],
[ 1, 0, -1],
[ 2, 1, 0]])
y_dists = array([[ 0, -1, -2],
[ 1, 0, -1],
[ 2, 1, 0]])
我需要:
dists = array([[[ 0, 0], [-1, -1], [-2, -2]],
[[ 1, 1], [ 0, 0], [-1, -1]],
[[ 2, 2], [ 1, 1], [ 0, 0]]])
我已經嘗試過使用dstack / hstack / vstack / concatenate的各種排列,但它們似乎都沒有做我想要的。 代碼中的實際數組可能是巨大的,因此迭代python中的元素並“手動”進行重新排列不是速度方面的選擇。
編輯:這是我最終提出的: https : //gist.github.com/807656
import numpy as np
dists = np.vstack(([x_dists.T], [y_dists.T])).T
像你想要的那樣返回dists
。 之后它不是“單個2元組的二維陣列”,而是一個普通的3D陣列,其中第三個軸是兩個原始陣列的串聯。
你看:
dists.shape # (3, 3, 2)
numpy.rec.fromarrays([x_dists, y_dists], names='x,y')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.