通过Python中的迭代在numpy/scipy中构建数组?
通常,我通过迭代一些数据来构建数组,例如:
my_array = []
for n in range(1000):
# do operation, get value
my_array.append(value)
# cast to array
my_array = array(my_array)
我发现我必须首先构建一个列表,然后将其(使用“数组”)转换为数组。有办法解决这些问题吗?所有这些转换调用都使代码变得混乱......我如何迭代地构建“my_array”,让它从一开始就是一个数组?
Often, I am building an array by iterating through some data, e.g.:
my_array = []
for n in range(1000):
# do operation, get value
my_array.append(value)
# cast to array
my_array = array(my_array)
I find that I have to first build a list and then cast it (using "array") to an array. Is there a way around these? All these casting calls clutter the code... how can I iteratively build up "my_array", with it being an array from the start?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
NumPy 提供了一个“fromiter”方法:
它产生
NumPy provides a 'fromiter' method:
which yields
推荐的方法是在循环之前进行预分配,并使用切片和索引来插入
numpy 确实提供了
array.resize()
方法,但这会慢得多由于在循环内重新分配内存的成本。如果您必须具有灵活性,那么恐怕唯一的方法就是从列表
创建数组
。编辑:如果您担心为数据分配太多内存,我会使用上面的方法来过度分配,然后当循环完成时,使用 array 删除数组中未使用的位.resize()。这将比在循环内不断重新分配数组快远、远。
编辑:响应@user248237的评论,假设您知道数组的任何一维(为了简单起见):
一般原则是“分配比您认为需要的更多的空间,如果情况发生变化,请调整数组大小几次尽可能”。将大小加倍可能会被认为是过度的,但实际上这是其他语言的几个标准库中的几个数据结构所使用的方法(例如,
java.util.Vector
默认情况下这样做。我认为 C++ 中的std::vector
的几个实现也可以做到这一点)。The recommended way to do this is to preallocate before the loop and use slicing and indexing to insert
numpy does provide an
array.resize()
method, but this will be far slower due to the cost of reallocating memory inside a loop. If you must have flexibility, then I'm afraid the only way is to create anarray
from alist
.EDIT: If you are worried that you're allocating too much memory for your data, I'd use the method above to over-allocate and then when the loop is done, lop off the unused bits of the array using
array.resize()
. This will be far, far faster than constantly reallocating the array inside the loop.EDIT: In response to @user248237's comment, assuming you know any one dimension of the array (for simplicity's sake):
The general principle is "allocate more than you think you'll need, and if things change, resize the array as few times as possible". Doubling the size could be thought of as excessive, but in fact this is the method used by several data structures in several standard libraries in other languages (
java.util.Vector
does this by default for example. I think several implementations ofstd::vector
in C++ do this as well).使用 list.append() 构建数组似乎比 Numpy 数组的任何类型的动态调整大小要快得多:
Building up the array using
list.append()
seems to be much faster than any kind of dynamic resizing of a Numpy array:如果我正确理解你的问题,这应该做你想要的:
If i understand your question correctly, this should do what you want: