how do you view a numpy broadcast object?

  • Last Update :
  • Techknowledgy :

Another option is to use np.broadcast_arrays to see what x and y look like after being broadcasted to the same shape:

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

In[37]: x, y = np.broadcast_arrays(x, y)

In[38]: x
Out[38]:
   array([
      [1, 1, 1],
      [2, 2, 2],
      [3, 3, 3]
   ])

In[39]: y
Out[39]:
   array([
      [4, 5, 6],
      [4, 5, 6],
      [4, 5, 6]
   ])

In[40]: x + y
Out[40]:
   array([
      [5, 6, 7],
      [6, 7, 8],
      [7, 8, 9]
   ])

You can look at the contents of a numpy.broadcast object by converting it to a list.

>>> import numpy as np
>>> x = np.array([[1], [2], [3]])
>>> y = np.array([4, 5, 6])
>>> b = np.broadcast(x, y)
>>> b
<numpy.broadcast object at 0xad0310>
   >>> list(b)
   [(1, 4), (1, 5), (1, 6), (2, 4), (2, 5), (2, 6), (3, 4), (3, 5), (3, 6)]

The broadcast using your 1st arrays is:

In[350]: a = np.array([1, 2, 3]);
b = np.array([2, 2, 2])
In[351]: ab = list(np.broadcast(a, b))
#[(1, 2), (2, 2), (3, 2)]
In[353]: list([i * j
   for i, j in ab
])
Out[353]: [2, 4, 6]

Suggestion : 2

Broadcast the input parameters against one another, and return an object that encapsulates the result. Amongst others, it has shape and nd properties, and may be used as an iterator.,Number of iterators possessed by the broadcasted result.,Reset the broadcasted result's iterator(s).,Produce an object that mimics broadcasting.

>>> x = np.array([
      [1],
      [2],
      [3]
   ]) >>>
   y = np.array([4, 5, 6]) >>>
   b = np.broadcast(x, y)
>>> out = np.empty(b.shape) >>>
   out.flat = [u + v
      for (u, v) in b
   ] >>>
   out
array([
   [5., 6., 7.],
   [6., 7., 8.],
   [7., 8., 9.]
])
>>> x + y
array([
   [5, 6, 7],
   [6, 7, 8],
   [7, 8, 9]
])

Suggestion : 3

The broadcast() function in NumPy's Python library is used to create an object that mimics broadcasting. ,Broadcasting in NumPy describes how NumPy treats arrays with different shapes by making use of arithmetic operations. To know more about broadcasting in NumPy, see this shot.,The broadcast() function broadcasts the input arrays against each other and returns an object encapsulating the result.,The broadcast() function takes the parameter values, in1 in2 ..., which represents the input arrays.

numpy.broadcast(in1, in2, ...)
import numpy as np
# creating the input arrays
a = np.array([
   [1],
   [2],
   [3],
   [4]
])
b = np.array([5, 6, 7, 8])

# calling the broadcast()
function
np.broadcast(a, b)

# adding the two arrays using broadcasting
print(a + b)

Suggestion : 4

To determine if two arrays are broadcast-compatible, align the entries of their shapes such that their trailing dimensions are aligned, and then check that each pair of aligned dimensions satisfy either of the following conditions:,The two arrays are broadcast-compatible if either of these conditions are satisfied for each pair of aligned dimensions.,We will now summarize the rules that determine if two arrays are broadcast-compatible with one another, and what the shape of the resulting array will be after the mathematical operation between the two arrays is performed.,Given the following pairs of array-shapes, determine what the resulting broadcasted shapes will be. Indicate if a pair is broadcast-incompatible.

>>>
import numpy as np

# a shape - (3, 4) array >>>
   x = np.array([
      [-0., -0.1, -0.2, -0.3],
      ...[-0.4, -0.5, -0.6, -0.7],
      ...[-0.8, -0.9, -1., -1.1]
   ])

# a shape - (4, ) array >>>
   y = np.array([1, 2, 3, 4])

# multiplying a shape - (4, ) array with a shape - (3, 4) array
# `y`
is multiplied by each row of `x` >>>
   x * y
array([
   [-0., -0.2, -0.6, -1.2],
   [-0.4, -1., -1.8, -2.8],
   [-0.8, -1.8, -3., -4.4]
])
# Broadcast multiplications between a
# shape - (3, 1, 2) array and a shape - (3, 1)
# array. >>>
   x = np.array([
      [
         [0, 1]
      ],
      ...
      ...[
         [2, 3]
      ],
      ...
      ...[
         [4, 5]
      ]
   ])

   >>>
   y = np.array([
      [0],
      ...[1],
      ...[-1]
   ])

# shape - (3, 1, 2) broadcast - multiply with
# shape - (3, 1) produces shape - (3, 3, 2) >>>
   x * y
array([
   [
      [0, 0],
      [0, 1],
      [0, -1]
   ],

   [
      [0, 0],
      [2, 3],
      [-2, -3]
   ],

   [
      [0, 0],
      [4, 5],
      [-4, -5]
   ]
])

# an example of broadcast - incompatible arrays
# a shape - (2, ) array with a shape - (3, ) array >>>
   np.array([1, 2]) * np.array([0, 1, 2])
ValueError: operands could not be broadcast together with shapes(2, )(3, )
     array - 1: 4 x 3
     array - 2: 3
     result - shape: 4 x 3
     array - 1: 8
     array - 2: 5 x 2 x 8
     result - shape: 5 x 2 x 8

     array - 1: 5 x 2
     array - 2: 5 x 4 x 2
     result - shape: INCOMPATIBLE

     array - 1: 4 x 2
     array - 2: 5 x 4 x 2
     result - shape: 5 x 4 x 2

     array - 1: 8 x 1 x 3
     array - 2: 8 x 5 x 3
     result - shape: 8 x 5 x 3

     array - 1: 5 x 1 x 3 x 2
     array - 2: 9 x 1 x 2
     result - shape: 5 x 9 x 3 x 2

     array - 1: 1 x 3 x 2
     array - 2: 8 x 2
     result - shape: INCOMPATIBLE

     array - 1: 2 x 1
     array - 2: 1
     result - shape: 2 x 1
# Demonstrating `np.broadcast_to` >>>
   x = np.array([
      [0, 1, 2, 3],
      ...[4, 5, 6, 7],
      ...[8, 9, 10, 11]
   ])

# Explicitly broadcast a shape - (3, 4) array
# to a shape - (2, 3, 4) array. >>>
   np.broadcast_to(x, (2, 3, 4))
array([
   [
      [0, 1, 2, 3],
      [4, 5, 6, 7],
      [8, 9, 10, 11]
   ],

   [
      [0, 1, 2, 3],
      [4, 5, 6, 7],
      [8, 9, 10, 11]
   ]
])
# grades
for 6 students who have taken 3 exams
# axis - 0(rows): student
# axis - 1(columns): exams >>>
   import numpy as np >>>
   grades = np.array([
      [0.79, 0.84, 0.84],
      ...[0.87, 0.93, 0.78],
      ...[0.77, 1.00, 0.87],
      ...[0.66, 0.75, 0.82],
      ...[0.84, 0.89, 0.76],
      ...[0.83, 0.71, 0.85]
   ])

Suggestion : 5

Last Updated : 24 Jun, 2022

output: 
 

array([
   [2.4, 8.7, 31.2],
   [157.2, 70.8, 292],
   [165.6, 95.1, 191.2],
   [43.2, 33, 39.2]
])

Inputs: Array A with m dimensions and array B with n dimensions 
 

p = max(m, n)
if m < p:
   left - pad A 's shape with 1s until it also has p dimensions

else if n < p:
   left - pad B 's shape with 1s until it also has p dimensions
result_dims = new list with p elements

for i in p - 1...0:
   A_dim_i = A.shape[i]
B_dim_i = B.shape[i]
if A_dim_i != 1 and B_dim_i != 1 and A_dim_i != B_dim_i:
   raise ValueError("could not broadcast")
else:
   # Pick the Array which is having maximum Dimension
result_dims[i] = max(A_dim_i, B_dim_i)

Output: 
 

[17 11 19]
3
   [20 14 22]