當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python numpy.tile()用法及代碼示例

關於:
numpy.tile(arr, repetitions) : 通過重複數組“ arr”來構造新的數組,“ arr”是我們希望根據重複重複的次數。結果數組的維數為max(arr.ndim,repetitions),其中,repeats是重複的長度。
如果arr.ndim>重複,則通過在其前麵加1來將代表提升為arr.ndim。
如果arr.ndim

參數:

array       : [array_like]Input array. 
repetitions : No. of repetitions of arr along each axis. 

返回:


An array with repetitions of array - arr as per d, number of times we want to repeat arr  

代碼1:

# Python Program illustrating 
# numpy.tile() 
  
import numpy as geek 
  
#Working on 1D 
arr = geek.arange(5) 
print("arr : \n", arr) 
  
repetitions = 2
print("Repeating arr 2 times : \n", geek.tile(arr, repetitions)) 
  
repetitions = 3
print("\nRepeating arr 3 times : \n", geek.tile(arr, repetitions)) 
# [0 1 2 ..., 2 3 4] means [0 1 2 3 4 0 1 2 3 4 0 1 2 3 4] 
# since it was long output, so it uses [ ... ]

輸出:

arr : 
 [0 1 2 3 4]
Repeating arr 2 times : 
 [0 1 2 3 4 0 1 2 3 4]

Repeating arr 3 times : 
 [0 1 2 ..., 2 3 4]

代碼2:

# Python Program illustrating 
# numpy.tile() 
  
import numpy as geek 
  
arr = geek.arange(3) 
print("arr : \n", arr) 
  
a = 2  
b = 2  
repetitions = (a, b) 
print("\nRepeating arr : \n", geek.tile(arr, repetitions)) 
print("arr Shape : \n", geek.tile(arr, repetitions).shape) 
  
a = 3  
b = 2   
repetitions = (a, b) 
print("\nRepeating arr : \n", geek.tile(arr, repetitions)) 
print("arr Shape : \n", geek.tile(arr, repetitions).shape) 
  
a = 2
b = 3  
repetitions = (a, b) 
print("\nRepeating arr : \n", geek.tile(arr, repetitions)) 
print("arr Shape : \n", geek.tile(arr, repetitions).shape)

輸出:

arr : 
 [0 1 2]

Repeating arr : 
 [[0 1 2 0 1 2]
 [0 1 2 0 1 2]]
arr Shape : 
 (2, 6)

Repeating arr : 
 [[0 1 2 0 1 2]
 [0 1 2 0 1 2]
 [0 1 2 0 1 2]]
arr Shape : 
 (3, 6)

Repeating arr : 
 [[0 1 2 ..., 0 1 2]
 [0 1 2 ..., 0 1 2]]
arr Shape : 
 (2, 9)

代碼3 :(重複== arr.ndim)== 0

# Python Program illustrating 
# numpy.tile() 
  
import numpy as geek 
  
arr = geek.arange(4).reshape(2, 2) 
print("arr : \n", arr) 
  
a = 2  
b = 1  
repetitions = (a, b) 
print("\nRepeating arr : \n", geek.tile(arr, repetitions)) 
print("arr Shape : \n", geek.tile(arr, repetitions).shape) 
  
a = 3  
b = 2   
repetitions = (a, b) 
print("\nRepeating arr : \n", geek.tile(arr, repetitions)) 
print("arr Shape : \n", geek.tile(arr, repetitions).shape) 
  
a = 2
b = 3  
repetitions = (a, b) 
print("\nRepeating arr : \n", geek.tile(arr, repetitions)) 
print("arr Shape : \n", geek.tile(arr, repetitions).shape)

輸出:

arr : 
 [[0 1]
 [2 3]]

Repeating arr : 
 [[0 1]
 [2 3]
 [0 1]
 [2 3]]
arr Shape : 
 (4, 2)

Repeating arr : 
 [[0 1 0 1]
 [2 3 2 3]
 [0 1 0 1]
 [2 3 2 3]
 [0 1 0 1]
 [2 3 2 3]]
arr Shape : 
 (6, 4)

Repeating arr : 
 [[0 1 0 1 0 1]
 [2 3 2 3 2 3]
 [0 1 0 1 0 1]
 [2 3 2 3 2 3]]
arr Shape : 
 (4, 6)

參考文獻:
https://docs.scipy.org/doc/numpy/reference/generated/numpy.tile.html



相關用法


注:本文由純淨天空篩選整理自 numpy.tile() in Python。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。