用于追加和堆叠的列表与NumPy数组

fv2wmkja  于 2023-06-23  发布在  其他
关注(0)|答案(1)|浏览(130)

我很想知道是否有一种方法可以将NumPy数组从0个元素堆叠到MxN个元素,这样在循环的每次迭代中,我们都会向堆栈中添加一行大小为N的元素。
下面,我有一个程序,我所描述的,它确实工作。

import numpy as np

initial_lst = np.linspace(1, 100, 100)
print(initial_lst)

output = np.array([])
i = 0
for arr_part in initial_lst.reshape(10, 10):
    # Insert random intermittent process here
    new_stack = arr_part * 2
    # Stack the array 
    if i==0:
        output = np.append(output, new_stack) # output.shape is (0,) and new_stack.shape is (10,)
        i += 1
    elif i==1:
        output = np.stack((output, new_stack)) # both arrays have shape of (10,)
        i += 1
    else:
        output = np.append(output, [new_stack], axis=0) # output has an ever increasing shape of (10, n)

print(output)

这个程序从1 - 100创建一个np.linspace,将其整形,将每一行10个数字乘以2,并将其堆叠到一个输出数组中。这是我得到的输出,这是正确的。

[[  2.   4.   6.   8.  10.  12.  14.  16.  18.  20.]
 [ 22.  24.  26.  28.  30.  32.  34.  36.  38.  40.]
 [ 42.  44.  46.  48.  50.  52.  54.  56.  58.  60.]
 [ 62.  64.  66.  68.  70.  72.  74.  76.  78.  80.]
 [ 82.  84.  86.  88.  90.  92.  94.  96.  98. 100.]
 [102. 104. 106. 108. 110. 112. 114. 116. 118. 120.]
 [122. 124. 126. 128. 130. 132. 134. 136. 138. 140.]
 [142. 144. 146. 148. 150. 152. 154. 156. 158. 160.]
 [162. 164. 166. 168. 170. 172. 174. 176. 178. 180.]
 [182. 184. 186. 188. 190. 192. 194. 196. 198. 200.]]

虽然它确实可以工作,但我很好奇是否可以更简洁地完成这一切,类似于下面的程序,它做了完全相同的事情,但使用的是普通的Python列表。

import numpy as np

initial_lst = np.linspace(1, 100, 100)
print(initial_lst)

output = []
i = 0
for arr_part in initial_lst.reshape(10, 10):
    # Insert random intermittent process here
    new_stack = arr_part * 2
    # Stack the array by converting numpy.array to a Python list
    output.append(new_stack.tolist())

print(np.array(output))

np.append线复制到其他步骤中会导致尺寸错误。例如,如果我用下面的代码替换np.stack行,因为两个数组的大小相同:

elif i==1:
    output = np.append(output, [new_stack]) # both arrays have shape of (10,)
    i += 1

我得到以下错误。

ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 1 dimension(s) and the array at index 1 has 2 dimension(s)

我也尝试用下面的方法使用np.concatenate

output = np.concatenate([output, new_stack])

但是,它只返回一个1x100数组而不是10x10,并且指定axis=1会给我带来维度错误。
如果需要的话,我可以提供更多的错误,但这就是一般的想法。我的问题如下:
1.有没有一种方法可以在尽可能少的行中得到我想要的输出,而不需要转换成Python列表?如果是,它们的表现如何?
1.列表方法与NumPy操作相比一般追加和堆叠操作如何?

dddzy1tm

dddzy1tm1#

np.concatenate将以这种方式工作:

import numpy as np

initial_lst = np.linspace(1, 100, 100)
print(initial_lst)

output = np.zeros((1,10))

for arr_part in initial_lst.reshape(10, 10):
    # Insert random intermittent process here
    #print(arr_part)
    new_stack = arr_part * 2
    # Stack the array by converting numpy.array to a Python list
    if not output.any():
       output = new_stack[None,:]
       continue
    output = np.concatenate((output,new_stack[None,:]),axis=0)

print(output)

但这太慢了。请注意,您正在处理每个迭代中重新整形initial_lst的一维阵列切片。因此,高效的方法可以使用np.apply_along_axis如下:

output= np.apply_along_axis(lambda x:x*2, 1,initial_lst.reshape(10,10))
print(output)

相关问题