簡體   English   中英

在 python 中填充二維數組

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM