通常,我通过迭代一些数据来构建数组,例如:
my_array = []
for n in range(1000):
# do operation, get value
my_array.append(value)
# cast to array
my_array = array(my_array)
Run Code Online (Sandbox Code Playgroud)
我发现我必须首先构建一个列表,然后将它(使用"array")转换为数组.这有什么方法吗?所有这些转换调用使代码混乱......我怎样才能迭代地构建"my_array",从一开始它就是一个数组?
Ste*_*alt 32
NumPy提供了'fromiter'方法:
def myfunc(n):
for i in range(n):
yield i**2
np.fromiter(myfunc(5), dtype=int)
Run Code Online (Sandbox Code Playgroud)
产量
array([ 0, 1, 4, 9, 16])
Run Code Online (Sandbox Code Playgroud)
Chi*_*chi 15
建议的方法是在循环之前预分配并使用切片和索引进行插入
my_array = numpy.zeros(1,1000)
for i in xrange(1000):
#for 1D array
my_array[i] = functionToGetValue(i)
#OR to fill an entire row
my_array[i:] = functionToGetValue(i)
#or to fill an entire column
my_array[:,i] = functionToGetValue(i)
Run Code Online (Sandbox Code Playgroud)
numpy的确实提供了一个array.resize()方法,但是这将是慢得多由于在循环中重新分配存储器的成本.如果你必须有灵活性,那么恐怕唯一的办法就是创造一个array来自list.
编辑:如果你担心你为你的数据分配了太多的内存,我会使用上面的方法进行过度分配,然后在完成循环时,使用掉掉数组中未使用的位array.resize().这将是迄今为止,远快于不断重新分配循环内的数组.
编辑:响应@ user248237的评论,假设你知道数组的任何一个维度(为简单起见):
my_array = numpy.array(10000, SOMECONSTANT)
for i in xrange(someVariable):
if i >= my_array.shape[0]:
my_array.resize((my_array.shape[0]*2, SOMECONSTANT))
my_array[i:] = someFunction()
#lop off extra bits with resize() here
Run Code Online (Sandbox Code Playgroud)
一般原则是"分配比您认为需要的更多,如果事情发生变化,尽可能少地调整阵列大小".倍增尺寸可以被认为是过度的,但实际上这是用不同的语言几个标准库使用的几个数据结构的方法(java.util.Vector通过默认例如执行此操作.我想的几种实现std::vector在C++中做到这一点以及).
dou*_*oug -2
如果我正确理解你的问题,这应该做你想要的:
# the array passed into your function
ax = NP.random.randint(10, 99, 20).reshape(5, 4)
# just define a function to operate on some data
fnx = lambda x : NP.sum(x)**2
# apply the function directly to the numpy array
new_row = NP.apply_along_axis(func1d=fnx, axis=0, arr=ax)
# 'append' the new values to the original array
new_row = new_row.reshape(1,4)
ax = NP.vstack((ax, new_row))
Run Code Online (Sandbox Code Playgroud)