np.repeat函數


np.repeat用法

覺得有用的話,歡迎一起討論相互學習~

我的微博我的github我的B站

np.repeat用於將numpy數組重復

一維數組重復三次

import numpy as np
# 隨機生成[0,5)之間的數,形狀為(1,4),將此數組重復3次
pop = np.random.randint(0, 5, size=(1, 4)).repeat(3, axis=0)
print("pop\n",pop)
# pop
#  [[0 0 3 1]
#  [0 0 3 1]
#  [0 0 3 1]]

二維數組在第一維和第二維分別重復三次

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\n",pop_reshape)
print("pop_reshape_repeataxis0\n",pop_reshape_repeataxis0)
print("pop_reshape_repeataxis1\n",pop_reshape_repeataxis1)
# pop_reshape
#  [[0 0 3 1 0 0]
#  [3 1 0 0 3 1]]
# pop_reshape_repeataxis0
#  [[0 0 3 1 0 0]
#  [0 0 3 1 0 0]
#  [0 0 3 1 0 0]
#  [3 1 0 0 3 1]
#  [3 1 0 0 3 1]
#  [3 1 0 0 3 1]]
# pop_reshape_repeataxis1
#  [[0 0 0 0 0 0 3 3 3 1 1 1 0 0 0 0 0 0]
#  [3 3 3 1 1 1 0 0 0 0 0 0 3 3 3 1 1 1]]


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM