[英]Populating a 2D array in python
我需要填充一个形状为 3xN 的二维数组,其中 N 最初是未知的。 代码如下所示:
import numpy as np
import random
nruns = 5
all_data = [[]]
for run in range(nruns):
n = random.randint(1,10)
d1 = random.sample(range(0, 30), n)
d2 = random.sample(range(0, 30), n)
d3 = random.sample(range(0, 30), n)
data_tmp = [d1, d2, d3]
all_data = np.concatenate((all_data,data_tmp),axis=0)
这给出了以下错误:
ValueError Traceback (most recent call last)
<ipython-input-103-22af8f04e7c0> in <module>
10 d3 = random.sample(range(0, 30), n)
11 data_tmp = [d1, d2, d3]
---> 12 all_data = np.concatenate((all_data,data_tmp),axis=0)
13 print(np.shape(data_tmp))
<__array_function__ internals> in concatenate(*args, **kwargs)
ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 0 and the array at index 1 has size 4
有没有办法在不预先分配all_data
的情况下做到这一点? 请注意,在我的应用程序中,数据不是随机的,而是在循环内生成的。
非常感谢!
您可以将 for 循环的每个步骤中生成的数据存储到列表中,并在完成后创建数组。
In [298]: import numpy as np
...: import random
In [299]: nruns = 5
...: all_data = []
In [300]: for run in range(nruns):
...: n = random.randint(1,10)
...: d1 = random.sample(range(0, 30), n)
...: d2 = random.sample(range(0, 30), n)
...: d3 = random.sample(range(0, 30), n)
...: all_data.append([d1, d2, d3])
In [301]: all_data = np.hstack(all_data)
In [302]: all_data
Out[302]:
array([[13, 28, 14, 15, 11, 0, 0, 19, 6, 28, 14, 18, 1, 15, 4, 20,
9, 14, 15, 13, 27, 28, 25, 5, 7, 4, 10, 22, 12, 6, 23, 15,
0, 20, 14, 5, 13],
[10, 9, 23, 4, 25, 28, 17, 14, 3, 4, 5, 9, 7, 18, 23, 9,
14, 15, 25, 26, 29, 12, 21, 0, 5, 6, 11, 27, 13, 26, 22, 14,
6, 5, 7, 23, 0],
[13, 0, 7, 14, 29, 26, 12, 16, 13, 3, 9, 6, 11, 2, 19, 17,
28, 14, 25, 24, 3, 12, 22, 7, 23, 18, 5, 14, 0, 14, 15, 8,
3, 2, 26, 21, 16]])
看看这是否是您需要的,即沿轴1
而不是0
填充。
import numpy as np
import random
nruns = 5
all_data = [[], [], []]
for run in range(nruns):
n = random.randint(1,10)
d1 = random.sample(range(0, 30), n)
d2 = random.sample(range(0, 30), n)
d3 = random.sample(range(0, 30), n)
data_tmp = [d1, d2, d3]
all_data = np.concatenate((all_data, data_tmp), axis=1)
仅使用np.random
怎么样:
nruns = 5
# set seed for repeatability, remove for randomness
np.random.seed(42)
# randomize the lengths for the runs
num_samples = np.random.randint(1,10, nruns)
# sampling with the total length
all_data = np.random.randint(0,30, (3, num_samples.sum()))
# or, if `range(0,30)` represents some population
# all_data = np.random.choice(range(0,30), (3,num_samples.sum()) )
print(all_data)
Output:
[[25 18 22 10 10 23 20 3 7 23 2 21 20 1 23 11 29 5 1 27 20 0 11 25
21 28 11 24 16 26 26]
[ 9 27 27 15 14 29 29 14 29 18 11 22 19 24 2 4 18 6 20 8 6 17 3 24
27 13 17 25 8 25 20]
[ 1 19 27 14 27 6 11 28 7 14 2 13 16 3 17 7 3 1 29 5 21 9 3 21
28 17 25 11 1 9 29]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.