Broadcasting
Contents
Broadcasting#
There are times when you might want to carry out an operation between an array and a single number (also called an operation between a vector and a scalar) or between arrays of two different sizes.
NumPy understands that the multiplication should happen with each cell. That concept is called broadcasting. Broadcasting is a mechanism that allows NumPy to perform operations on arrays of different shapes. The dimensions of your array must be compatible, for example, when the dimensions of both arrays are equal or when one of them is a 1D-array. If the dimensions are not compatible, you will get a ValueError.
import numpy as np #Run this cell or none of the np commands will work
data_array = np.array([2, 6])
print(data_array * 2)
[ 4 12]
data_array_other = np.array([[5., 1.], [3., 4.]])
print(data_array * data_array_other) # try to figure out the order of operations
[[10. 6.]
[ 6. 24.]]
data_array_diff = np.array([[5., 1., 7.], [3., 4., 6.]])
print(data_array * data_array_diff)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Input In [4], in <module>
1 data_array_diff = np.array([[5., 1., 7.], [3., 4., 6.]])
----> 2 print(data_array * data_array_diff)
ValueError: operands could not be broadcast together with shapes (2,) (2,3)
Exercise 6.1#
Without using np.full create a function that has two inputs
size, an int.value, a float or an int.
You function should return a one-dimensional numpy array so that it has the given size (from the input) and all its elements are equal to value.
Exercise 6.2#
In the cell below try to use broadcasting with between two array of different shape. If you get the ValueError, make sure that you understand why it occured.
Stacking#
To allow broadcasting we may need to stack our 1d array (single row) to represent it as a 2d array (single column), we do the following:
one_dim_array = np.array([1, 2, 3, 4])
vert_stack = np.vstack(one_dim_array)
print(f"Original array before verfical stacking is {one_dim_array}")
print(f"Verfically stacked array is {vert_stack}")
Original array before verfical stacking is [1 2 3 4]
Verfically stacked array is [[1]
[2]
[3]
[4]]
Vertical stacking#
You may also vertically stack two arrays as demonstrated by this example:
first_array = np.array([[1, 2], [3, 4]])
sec_array = np.array([[5, 6]])
vert_stack = np.vstack((first_array, sec_array))
print(f"Verfically stacked array is {vert_stack}")
Verfically stacked array is [[1 2]
[3 4]
[5 6]]
Exercise 6.3#
a) Create a one dimensional array and stack it to form a two dimensional array. Print your results.
b) Create two two dimensional arrays with the same number of columns (same width) then stack them into one array. Print your results.
We can also do horizontal stacking
org_array = [[1, 2], [2, 3], [3, 4]]
horz_stack = np.hstack(org_array)
print(f"Original array before horizontal stacking is {org_array}")
print(f"Horizontally stacked array is {horz_stack}")
Original array before horizontal stacking is [[1, 2], [2, 3], [3, 4]]
Horizontally stacked array is [1 2 2 3 3 4]
You can also horizontally stack two arrays:
first_array = np.array([[1, 6], [3, 4]])
sec_array = np.array([[5, 4], [2, 3]])
horz_stack = np.hstack((first_array, sec_array))
print(f"Horizontally stacked array is {horz_stack}")
Horizontally stacked array is [[1 6 5 4]
[3 4 2 3]]
Exercise 6.4#
a) Create a two dimensional array and stack it horizontally to form a one dimensional array. Print your result.
b) Create two two dimensional arrays with the same number of rows (same height) then stack them horizontally into one array. Print your results