cloud-ken

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]]

分类:

技术点:

相关文章:

  • 2021-11-26
  • 2021-12-20
  • 2022-12-23
  • 2021-04-05
  • 2022-12-23
  • 2021-09-04
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2023-03-20
  • 2022-12-23
  • 2022-02-17
  • 2022-12-23
  • 2021-08-18
  • 2021-11-05
相关资源
相似解决方案