【问题标题】:How to make a np array of arrays如何制作数组的np数组
【发布时间】:2021-02-12 04:47:34
【问题描述】:

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

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)]

【问题讨论】:

  • 如果您需要迭代,请像使用negativeData 一样使用列表追加。 np.append 很难正确使用,而且速度较慢。如果您有一个列表列表(大小都相同),np.array(alist) 应该创建所需的二维数组。
  • 所以我做的一件事是将它全部附加为一个列表,然后在 data = [x, y, time, polar] positiveData.append(data) positiveData = np.array 之后将其设为 np.array (positiveData) 这会让我得到我想要的但是我觉得这可能会更快?因为它是 2GB 的数据并且非常缓慢地分块。单次运行 60-90 秒可能更多
  • 如果您必须遍历文件和“事件”,则任务的数组构建部分将是次要的。如果可以避免 python 级别的迭代,则有从 othdr 数组构建数组的快速方法。关于迭代地制作数组有很多SO问题
  • f['events'].numpy()type 是什么?是numpy 数组吗?

标签: arrays python-3.x numpy 3d


【解决方案1】:

尝试修改为

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

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

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

【讨论】:

    猜你喜欢
    • 2018-08-28
    • 1970-01-01
    • 2011-11-04
    • 2020-12-20
    • 2022-06-11
    • 1970-01-01
    • 1970-01-01
    • 2018-06-13
    • 1970-01-01
    相关资源
    最近更新 更多