📜  Python中的numpy.tile

📅  最后修改于: 2020-06-10 01:09:36             🧑  作者: Mango

numpy.tile(arr, repetitions) : 通过重复数组–“ arr”(每次重复我们要重复的次数)来构造一个新数组。结果数组的维数为max(arr.ndim,repetitions),其中,repeats是重复的长度。
如果arr.ndim>重复,则通过在其前面加上1来将代表提升为arr.ndim。
如果arr.ndim <重复,则通过在新轴前添加reps来将其提升为arr.ndim。

 

参数:

array:[array_like]输入数组。
repetitions:每个轴上arr的重复次数。

返回:

具有重复数组的数组-按照d的arr,我们要重复arr的次数

代码1:

# Python程序说明numpy.tile()
  
import numpy as geek 
  
# 处理一维
arr = geek.arange(5) 
print("arr : \n", arr) 
  
repetitions = 2
print("重复两次arr : \n", geek.tile(arr, repetitions)) 
  
repetitions = 3
print("\n重复arr 3次 : \n", geek.tile(arr, repetitions)) 
# [0 1 2 ...,2 3 4]表示[0 1 2 3 4 0 1 2 3 4 0 1 2 3 4]由于它是长输出,因此使用[...]

输出:

arr: 
 [0 1 2 3 4]
重复两次arr: 
 [0 1 2 3 4 0 1 2 3 4]

重复arr 3次: 
 [0 1 2 ...,2 3 4]

代码2:

# Python程序,说明numpy.tile()
  
import numpy as geek 
  
arr = geek.arange(3) 
print("arr : \n", arr) 
  
a = 2  
b = 2  
repetitions = (a, b) 
print("\n重复arr : \n", geek.tile(arr, repetitions)) 
print("形状 : \n", geek.tile(arr, repetitions).shape) 
  
a = 3  
b = 2   
repetitions = (a, b) 
print("\n重复arr : \n", geek.tile(arr, repetitions)) 
print("形状 : \n", geek.tile(arr, repetitions).shape) 
  
a = 2
b = 3  
repetitions = (a, b) 
print("\n重复arr : \n", geek.tile(arr, repetitions)) 
print("形状 : \n", geek.tile(arr, repetitions).shape) 

输出:

arr: 
 [0 1 2]

重复arr: 
 [[0 1 2 0 1 2]
 [0 1 2 0 1 2]
形状: 
 (2、6)

重复arr: 
 [[0 1 2 0 1 2]
 [0 1 2 0 1 2]
 [0 1 2 0 1 2]
形状: 
 (3,6)

重复arr: 
 [[0 1 2 ...,0 1 2]
 [0 1 2 ...,0 1 2]]
形状: 
 (2、9)

代码3:(repetitions== arr.ndim)== 0

# ython程序,说明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("\n重复arr : \n", geek.tile(arr, repetitions)) 
print("形状 : \n", geek.tile(arr, repetitions).shape) 
  
a = 3  
b = 2   
repetitions = (a, b) 
print("\n重复arr : \n", geek.tile(arr, repetitions)) 
print("形状 : \n", geek.tile(arr, repetitions).shape) 
  
a = 2
b = 3  
repetitions = (a, b) 
print("\n重复arr : \n", geek.tile(arr, repetitions)) 
print("形状 : \n", geek.tile(arr, repetitions).shape)

输出:

arr: 
 [[0 1]
 [2 3]

重复arr: 
 [[0 1]
 [2 3]
 [0 1]
 [2 3]
形状: 
 (4,2)

重复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]
形状: 
 (6、4)

重复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]
形状: 
 (4、6)