在Python/Numpy中使用不同尺寸的阵列的最佳实践是什么?
通常,您将如何使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论