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


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

numpy.broadcast_to()函數將數組廣播為新形狀。

用法: numpy.broadcast_to(array, shape, subok = False)

參數:



array:[array_liket]要廣播的數組。

shape:[元組]所需數組的形狀。

subok:[bool,可選]如果為True,則子類將為passed-through,否則默認情況下,返回的數組將被強製為base-class數組。

Return:[array]輸出數組。

代碼1:

Python3

# 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:

Python3

# 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]]

相關用法


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