Python numpy.tile()
numpy.tile()函数通过重复数组-‘arr’来构造一个新的数组,我们想要重复的次数为repetitions。结果数组的尺寸是max(arr.ndim, repetitions),其中,repetitions是重复的长度。如果arr.ndim > repetitions,reps将通过预置1而被提升到arr.ndim。如果arr.ndim < repetitions,reps通过预置新轴被提升到arr.ndim。
语法 :
numpy.tile(arr, repetitions)
参数 :
array : [array_like]输入阵列。
repetitions :Arr在每个轴上的重复次数。
返回 :
一个带有重复数组的数组–Arr按照d,我们想重复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)