Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Python Numpy Broadcast() Class



The Numpy Broadcast() Class mimics the broadcasting mechanism and returns an object that encapsulates the result of broadcasting one array against another. This class takes arrays as input parameters.

This class allows operations on arrays of different shapes by virtually expanding the smaller array to match the shape of the larger array without actually creating a larger array in memory.

Syntax

The syntax for the numpy.broadcast() class is as follows −

numpy.broadcast(*array_like)

Parameter

The Numpy Broadcast() class takes *array_like as the input parameter, which are the input arrays that need to be broadcast against each other.

Return Value

It returns a numpy.broadcast object that can be used to iterate over the arrays.

Example 1

Following is the example of creating a broadcast array using Numpy Broadcast() class. Here, the arrays a and b are broadcasted to a common shape (3, 3) and iterated over element-wise −

import numpy as np
scalar = 5
array_1d = np.array([1, 2, 3])
broadcast_obj = np.broadcast(scalar, array_1d)
print("Broadcasted Shape:", broadcast_obj.shape)
print("Broadcasted Elements:")
for x, y in broadcast_obj:
    print(f"({x}, {y})")

Output

Broadcasted Shape: (3,)
Broadcasted Elements:
(5, 1)
(5, 2)
(5, 3)

Example 2

Below example shows broadcasting a 1D array [1, 2, 3] with a 2D array [[4], [5], [6]]. The 1D array is broadcast across the second dimension of the 2D array and creating a compatible shape for element-wise operations −

import numpy as np

array_1d = np.array([1, 2, 3])
array_2d = np.array([[4], [5], [6]])

broadcast_obj = np.broadcast(array_1d, array_2d)

print("Broadcasted Shape:", broadcast_obj.shape)
print("Broadcasted Elements:")
for x, y in broadcast_obj:
    print(f"({x}, {y})")

Output

Broadcasted Shape: (3, 3)
Broadcasted Elements:
(1, 4)
(2, 4)
(3, 4)
(1, 5)
(2, 5)
(3, 5)
(1, 6)
(2, 6)
(3, 6)

Example 3

Here in this example we are comparing numpy.broadcast() class with the built-in broadcast of Numpy library −

import numpy as np

x = np.array([[1], [2], [3]])
y = np.array([4, 5, 6])

# Broadcasting x against y
b = np.broadcast(x, y)

# Using nditer to iterate over the broadcasted object
print('Broadcast x against y:')
for r, c in np.nditer([x, y]):
    print(r, c)
print('\n')

# Shape attribute returns the shape of the broadcast object
print('The shape of the broadcast object:')
print(b.shape)
print('\n')

# Adding 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')

# Compute the addition manually
c.flat = [u + v for (u, v) in np.nditer([x, y])]
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)

Output

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]]
numpy_array_manipulation.htm
Advertisements