首页 > 解决方案 > 如何制作数组的np数组

问题描述

嘿,我想这可能已经得到了回答,但是我找不到我正在寻找的东西。下面是代码:

positiveData = np.array([])
negativeData = []

with AedatFile('someFile') as f:
    # loop through the "frames" stream
    for e in f['events'].numpy():
        for event in e:
            time, x, y, polarity, _, _ = event
            if polarity == 1:
                data = np.array([time, x, y, polarity])
                print(data)
                positiveData = np.append(positiveData,data)
                print(positiveData)
            else:
                data = [time, x, y, polarity]
                negativeData.append(data) 

我希望代码看起来像这样:

[[1,2,3,4],
[1,2,3,4],
....]

我打算用它来制作一个 3d 图,所以我想要一个数组,这样我就可以轻松地 plot3d(array[0][:],array[1][:],array[2][:])

欢呼所有人。

这是下面要求的一组样本数据。我不能粘贴更多,因为它说我的评论主要是代码,不允许我在不添加更多文本的情况下发布更多内容,这非常愚蠢。

[(1612584805989190, 254, 304, 1, 0, 0)
 (1612584805989190, 254, 283, 1, 0, 0)
 (1612584805989190, 254, 286, 1, 0, 0) ...
 (1612584805999148, 596,  20, 1, 0, 0)
 (1612584805999162, 549,  60, 1, 0, 0)
 (1612584805999189, 461, 225, 0, 0, 0)]
[(1612584806009235, 512,  31, 1, 0, 0)
 (1612584806009263, 419, 274, 1, 0, 0)
 (1612584806009287, 338, 188, 0, 0, 0) ...
 (1612584806019188, 214, 241, 0, 0, 0)
 (1612584806019188, 214, 237, 0, 0, 0)
 (1612584806019189, 211, 234, 0, 0, 0)]

标签: arrayspython-3.xnumpy3d

解决方案


尝试修改为

positiveData = np.append( positiveData, [data] )

或者

data = np.array( [[ time, x, y, polarity ]] )

append 函数将在数组目标 https://numpy.org/doc/stable/reference/generated/numpy.append.html中附加(嵌入)给定的数组


推荐阅读