在Python/Numpy中使用不同尺寸的阵列的最佳实践是什么?

发布于 2025-02-11 05:29:27 字数 380 浏览 1 评论 0原文

通常,您将如何使用numpy创建一个空数组并添加值。我正在撰写一个脚本,在该脚本中我迭代了一些数据,但是我不知道我需要添加的每个元素将要有多大,因此我无法预先分配内存。处理这种情况的最佳实践是什么?我可以将数据附加到对象类型数组中,但这很慢。有更好的方法来处理这一点吗?

示例:

# data is shape (100,256,256)
results_hist = np.zeros((0,0), dtype=object)
for i in data:
    results = doSomething(i) # varying size
    results_hist = np.append(results_hist,results)

这是最好的做法吗?

Normally how you would use numpy is to create an empty array and add values in as they come. I am working on a script where I iterate over some data, but I don't know how large each element I need to add is going to be, so I'm not able to preallocate memory. What is the best practice for dealing with such a situation? I can append the data to a object type array, but that is slow. Is there a better way to handle this?

Example:

# data is shape (100,256,256)
results_hist = np.zeros((0,0), dtype=object)
for i in data:
    results = doSomething(i) # varying size
    results_hist = np.append(results_hist,results)

Is this the best practice?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文