Broadcasting - SOLVED
Contents
Broadcasting - SOLVED#
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 1. 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 guess how did it work
[[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.
def make_array(size,value):
arr_1 = np.ones(size)
return arr_1 * value
new_arr = make_array(10,5)
print(new_arr)
[5. 5. 5. 5. 5. 5. 5. 5. 5. 5.]
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.
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 guess how did it work
[[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 [9], 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)
Stacking#
To allow broadcasting we may need to stack our 1d array to represent it as a 2d array, we do it as follows:
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]]
You may also vertically stack two arrays
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.
#a)
my_array = np.arange(10)
stack_array = np.vstack(my_array)
print(f"Here is the first array stacked: {stack_array}")
#b)
my_array1 = np.array([[1, 2, 3], [3, 4, 5]])
my_array2 = np.array([[9, 3, 4], [1, 2, 3], [3, 4, 5]])
stack_array = np.vstack((my_array1, my_array2))
print(f"Here is the second array stacked: {stack_array}")
Here is the first array stacked: [[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]]
Here is the second array stacked: [[1 2 3]
[3 4 5]
[9 3 4]
[1 2 3]
[3 4 5]]
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
#a)
my_array = np.array([[1, 2, 3, 7],[3, 4, 5, 6]])
stack_array = np.hstack(my_array)
print(f"Horizontal stack of first array: {stack_array}")
#b)
my_array1 = np.array([[1, 2, 3,], [3, 4, 5,]])
my_array2 = np.array([[9, 3, 4, 4, 6, 8], [1, 2, 3, 4, 9, 8]])
stack_array = np.hstack((my_array1, my_array2))
print(f"""
Horizontal stack of second array:
{stack_array}""")
Horizontal stack of first array: [1 2 3 7 3 4 5 6]
Horizontal stack of second array:
[[1 2 3 9 3 4 4 6 8]
[3 4 5 1 2 3 4 9 8]]