जैसा कि पहले देखा गया है, NumPy में प्रसारण के लिए अंतर्निहित समर्थन है। यह फ़ंक्शन प्रसारण तंत्र की नकल करता है। यह एक ऐसी वस्तु को लौटाता है जो एक सरणी को दूसरे के खिलाफ प्रसारित करने के परिणाम को संलग्न करता है।
फ़ंक्शन इनपुट पैरामीटर के रूप में दो सरणियों लेता है। निम्नलिखित उदाहरण इसके उपयोग को दर्शाता है।
उदाहरण
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]]