如何将矩阵的每一行复制N次

ggg*_*ggg 1 python numpy row matrix duplicates

我有一个具有这些尺寸(150,2)的矩阵,我想每行重复N次。我举一个例子来说明我的意思。

输入:

a = [[2, 3], [5, 6], [7, 9]]
Run Code Online (Sandbox Code Playgroud)

假设N = 3,我需要此输出:

[[2 3]
 [2 3]
 [2 3]
 [5 6]
 [5 6]
 [5 6]
 [7 9]
 [7 9]
 [7 9]]
Run Code Online (Sandbox Code Playgroud)

谢谢。

San*_*apa 5

np.repeat与参数配合使用axis=0

a = np.array([[2, 3],[5, 6],[7, 9]])

print(a)
[[2 3]
 [5 6]
 [7 9]]

r_a = np.repeat(a, repeats=3, axis=0)

print(r_a)
[[2 3]
 [2 3]
 [2 3]
 [5 6]
 [5 6]
 [5 6]
 [7 9]
 [7 9]
 [7 9]]
Run Code Online (Sandbox Code Playgroud)