np.repeat()用於將numpy數組重復。
numpy.repeat(a, repeats, axis=None);
參數:
axis=0,沿着y軸復制,實際上增加了行數
axis=1,沿着x軸復制,實際上增加了列數
1. 一維數組重復3次
# 隨機生成[0, 5)之間的數,形狀1行4列,將此數組按y軸重復3次 import numpy as np pop = np.random.randint(0, 5, size=(1, 4)).repeat(3, axis=0) print(pop)
[[1 4 0 4] [1 4 0 4] [1 4 0 4]]
2. 二維數組在第一維和第二維分別重復3次
# 二維數組在第一維和第二維分別重復3次 pop_reshape = pop.reshape(2, 6) pop_reshape_repeataxis0 = pop_reshape.repeat(3, axis=0) pop_reshape_repeataxis1 = pop_reshape.repeat(3, axis=1) print(pop_reshape) print(pop_reshape_repeataxis0) print(pop_reshape_repeataxis1 )
[[1 4 0 4 1 4] [0 4 1 4 0 4]] [[1 4 0 4 1 4] [1 4 0 4 1 4] [1 4 0 4 1 4] [0 4 1 4 0 4] [0 4 1 4 0 4] [0 4 1 4 0 4]] [[1 1 1 4 4 4 0 0 0 4 4 4 1 1 1 4 4 4] [0 0 0 4 4 4 1 1 1 4 4 4 0 0 0 4 4 4]]
來自:https://blog.csdn.net/u013555719/article/details/83855965