關於:
numpy.repeat(arr,repetitions, axis = None):重複數組的元素-arr。
參數:
array : [array_like]Input array. repetitions : No. of repetitions of each array elements along the given axis. axis : Axis along which we want to repeat values. By default, it returns a flat output array.
返回:
An array with repetitions of array - arr elements as per repetitions, number of times we want to repeat arr
代碼1:
# Python Program illustrating
# numpy.repeat()
import numpy as geek
#Working on 1D
arr = geek.arange(5)
print("arr : \n", arr)
repetitions = 2
a = geek.repeat(arr, repetitions)
print("\nRepeating arr 2 times : \n", a)
print("Shape : ", a.shape)
repetitions = 3
a = geek.repeat(arr, repetitions)
print("\nRepeating arr 3 times : \n", a)
# [0 0 0 ..., 4 4 4] means [0 0 0 1 1 1 2 2 2 3 3 3 4 4 4]
# since it was long output, so it uses [ ... ]
print("Shape : ", a.shape)
輸出:
arr : [0 1 2 3 4] Repeating arr 2 times : [0 0 1 1 2 2 3 3 4 4] Shape : (10,) Repeating arr 3 times : [0 0 0 ..., 4 4 4] Shape : (15,)
代碼2:
# Python Program illustrating
# numpy.repeat()
import numpy as geek
arr = geek.arange(6).reshape(2, 3)
print("arr : \n", arr)
repetitions = 2
print("\nRepeating arr : \n", geek.repeat(arr, repetitions, 1))
print("arr Shape : \n", geek.repeat(arr, repetitions).shape)
repetitions = 2
print("\nRepeating arr : \n", geek.repeat(arr, repetitions, 0))
print("arr Shape : \n", geek.repeat(arr, repetitions).shape)
repetitions = 3
print("\nRepeating arr : \n", geek.repeat(arr, repetitions, 1))
print("arr Shape : \n", geek.repeat(arr, repetitions).shape)
輸出:
arr : [[0 1 2] [3 4 5]] Repeating arr : [[0 0 1 1 2 2] [3 3 4 4 5 5]] arr Shape : (12,) Repeating arr : [[0 1 2] [0 1 2] [3 4 5] [3 4 5]] arr Shape : (12,) Repeating arr : [[0 0 0 ..., 2 2 2] [3 3 3 ..., 5 5 5]] arr Shape : (18,)
參考文獻:
https://docs.scipy.org/doc/numpy/reference/generated/numpy.repeat.html
相關用法
注:本文由純淨天空篩選整理自 numpy.repeat() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。