将两个列表列表合并到一个列表列表中

时间:2020-01-28 00:25:40

标签: python

我需要从两个多元正态分布(每个10个)中抽取20个值。

现在我有两个列表 dat1,dat2 ,看起来像

# means
m1, m2 = [2, 2], [1, -1]
# covariance
cv1, cv2 = [[0.8, 0.4], [0.4, 0.8]], [[1.3, -0.7], [-0.7, 1.3]]
dat1 = np.random.multivariate_normal(m1, cv1, 10)
dat2 = np.random.multivariate_normal(m2, cv2, 10)

>>>dat1
array([[2.41897704, 1.01060023],
       [2.59949247, 1.93349966],
       [2.65142916, 2.58018331],
       [2.17005156, 3.17732589],
       [3.02638605, 2.8026237 ],
       [2.98480009, 3.27263144],
       [2.43494798, 1.88818356],
       [3.3352379 , 2.22139406],
       [1.47718545, 1.77338061],
       [0.21706158, 2.74841337]])

如果我尝试

TRAINING_DATA = [c+d for c, d in zip(dat1, dat2)]

我明白了

>>>TRAINING_DATA
[array([ 3.57377544, -0.89898446]), array([4.11181782, 0.23353741]), ...]

如何将dat2附加到dat1上,使其看起来像

>>>TRAINING_DATA
array([[3.57377544, -0.89898446],
       [4.11181782, 0.23353741],
       ...
       ])

1 个答案:

答案 0 :(得分:1)

尝试将两个数组串联在一起,这会将dat2附加到dat1上。

TRAINING_DATA = np.concatenate((dat1, dat2), axis=0)

[[ 1.50548361  1.48826683]
 [ 2.29344     3.53410247]
 [ 3.19513868  0.94069523]
 [ 1.81553839  0.61394121]
 [ 1.84940489  2.51819747]
 [ 1.9253743   2.01885069]
 [ 2.19767682  1.81290866]
 [ 2.6041682   2.04656255]
 [ 1.14421244  1.43839062]
 [ 2.12712095  1.50493227]
 [ 0.0252174  -1.54414803]
 [ 1.94548543 -1.71105078]
 [ 0.42478946 -0.65312376]
 [ 0.18242194 -1.08179918]
 [-0.33842747 -1.39057522]
 [-0.19844272  0.28873601]
 [-0.03024569 -1.20532982]
 [ 2.91372093 -1.90649743]
 [ 0.64701937 -1.90869586]
 [ 0.99986509 -0.92723385]]

阅读文档:numpy.concatenate