numpy 数组维度的正确应用

Correct application of numpy array dimensions

我正在收集一个人或多人 body 随着时间推移的多个关键点的 3D 坐标。这些数据需要收集在一个数据集中。 这基本上导致每个帧的输入由一个或多个 19x3 矩阵组成。数据集结构应该如下所示:

我真的在绞尽脑汁思考如何循环一个数据集中的所有数据。由于数据是在运行时产生的,我无法预先set-up外层的大小。 到目前为止,我创建 3D/4D 阵列的所有尝试都导致尺寸未对齐。

如何解决这样的问题?

如果您知道每帧中 19x3 矩阵的最大数量,那么您可以设置外部尺寸并在没有数据的地方放置空矩阵(或 0 矩阵)。我认为您可能想使用 pandas 数据框并利用索引使以后更容易访问您的数据。

你有 N 个帧,N 个不确定,每个帧包含一对 (19, 3) 矩阵。您可以预先分配您当前正在处理的框架:

frame = np.full((2, 19, 3), np.nan)

用 NaN 填充可以方便以后查找未使用的矩阵。收集一帧后,将其添加到列表中:

frames = []
...
frames.append(frame)  # Probably in a loop somewhere

完成后,连接成一个形状为 (N, 2, 19, 3):

的数组
frames = np.stack(frames, axis=0)

结果可以索引为 (frame, matrix, row, col),其中有时 matrix == 1 未填充。

基于疯狂物理学家的回答我的最终解决方案:

global dataFrame
dataFrame = []
#dataframe containing the entire coordinates table 
#The dataFrame can ce accessed as following: 
#dataFrame[#ofFrame][IdInFrame][limbNumber][Coordinate(x,y,z)]

def takeCoordinates():
    global dataFrame
    frame = np.full((2, 19, 3), np.nan)
    xRow, xCol = x.shape
    for n in range(xRow):
        coordArray = np.empty((0, 3), int)
        for m in range(xCol):
            coordArray = np.append(coordArray, np.array([[x[n, m], y[n, m], z[n, m]]]), axis=0)
        frame = np.insert(frame,n,coordArray,axis=0)
    dataFrame.append(frame)