返回介绍

broadcast

发布于 2021-06-12 09:43:38 字数 1271 浏览 1014 评论 0 收藏 0

如前所述,NumPy内置了对广播的支持。 该功能模仿广播机制。 它返回一个对象,该对象封装了一个数组与另一个数组相互广播的结果。

该函数将两个数组作为输入参数。 以下示例说明了它的用法。

例子 (Example)

import numpy as np 
x = np.array([[1], [2], [3]]) 
y = np.array([4, 5, 6])  
# tobroadcast x against y 
b = np.broadcast(x,y)  
# it has an iterator property, a tuple of iterators along self's "components." 
print 'Broadcast x against y:' 
r,c = b.iters 
print r.next(), c.next() 
print r.next(), c.next() 
print '\n'  
# shape attribute returns the shape of broadcast object 
print 'The shape of the broadcast object:' 
print b.shape 
print '\n'  
# to add x and y manually using broadcast 
b = np.broadcast(x,y) 
c = np.empty(b.shape) 
print 'Add x and y manually using broadcast:' 
print c.shape 
print '\n'  
c.flat = [u + v for (u,v) in b] 
print 'After applying the flat function:' 
print c 
print '\n'  
# same result obtained by NumPy's built-in broadcasting support 
print 'The summation of x and y:' 
print x + y

其输出如下 -

Broadcast x against y:
1 4
1 5
The shape of the broadcast object:
(3, 3)
Add x and y manually using broadcast:
(3, 3)
After applying the flat function:
[[ 5. 6. 7.]
 [ 6. 7. 8.]
 [ 7. 8. 9.]]
The summation of x and y:
[[5 6 7]
 [6 7 8]
 [7 8 9]]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文