Python numpy.broadcast_to()函数
numpy.broadcast_to()函数将一个数组广播到一个新的形状。
语法: numpy.broadcast_to(array, shape, subok = False)
参数 :
array : [array_liket] 要广播的阵列。
shape : [tuple] 所需数组的形状。
subok : [bool, optional] 如果为真,则子类将被通过,否则默认情况下,返回的数组将被强制为基类数组。
返回: [array] 输出数组。
代码 #1 :
# Python program explaining
# numpy.broadcast_to() function
# importing numpy as geek
import numpy as geek
arr = geek.array([1, 2, 3, 4])
gfg = geek.broadcast_to(arr, (4, 4))
print(gfg)
输出 :
[[1 2 3 4]
[1 2 3 4]
[1 2 3 4]
[1 2 3 4]]
代码#2 :
# Python program explaining
# numpy.broadcast_to() function
# importing numpy as geek
import numpy as geek
arr = geek.array([1, 2, 3, 4, 5])
gfg = geek.broadcast_to(arr, (5, 5))
print(gfg)
输出 :
[[1 2 3 4 5]
[1 2 3 4 5]
[1 2 3 4 5]
[1 2 3 4 5]
[1 2 3 4 5]]