2025-plovdiv-data/exercises/numpy_broadcasting/broadcasting_solutions.ipynb

28 KiB

Broadcasting exercises

In [1]:
import numpy as np

Exercise 1

What is the expected output shape for each operation?
In [2]:
a = np.arange(5)
b = 5

np.shape(a - b)
Out[2]:
(5,)
In [3]:
a = np.ones((7, 1))
b = np.arange(7)
np.shape(a * b)
Out[3]:
(7, 7)
In [4]:
a = np.random.randint(0, 50, (2, 3, 3))
b = np.random.randint(0, 10, (3, 1))

np.shape(a - b)
Out[4]:
(2, 3, 3)
In [5]:
a = np.arange(100).reshape(10, 10)
b = np.arange(0, 10)

np.shape(a + b)
Out[5]:
(10, 10)

Exercise 2

1. Create a random 2D array of dimension (5, 3)
2. Calculate the maximum value of each row
3. Divide each row by its maximum

Remember to use broadcasting : NO FOR LOOPS!

In [6]:
x = np.random.random((5,3))
x
Out[6]:
array([[0.04512288, 0.29887872, 0.71583331],
       [0.96876287, 0.89086747, 0.53374129],
       [0.80630616, 0.05463225, 0.23587356],
       [0.12837335, 0.29644576, 0.71527555],
       [0.0672898 , 0.99300472, 0.49310665]])
In [7]:
m = x.max(axis=1)
y = x / m[:, None]
y
Out[7]:
array([[0.06303546, 0.41752558, 1.        ],
       [1.        , 0.91959291, 0.55095143],
       [1.        , 0.06775621, 0.29253598],
       [0.17947398, 0.41444974, 1.        ],
       [0.06776383, 1.        , 0.49658036]])
In [8]:
# check
y.max(axis=1)[:, None]
Out[8]:
array([[1.],
       [1.],
       [1.],
       [1.],
       [1.]])

Exercise 3

Task: Find the closest cluster to the observation.

Again, use broadcasting: DO NOT iterate cluster by cluster

In [9]:
observation = np.array([30.0, 99.0]) #Observation

#Clusters
clusters = np.array([
    [102.0, 203.0],
    [132.0, 193.0],
    [45.0, 155.0], 
    [57.0, 173.0]
])

Let's plot this data

In the plot below, + is the observation and dots are the cluster coordinates

In [10]:
import matplotlib.pyplot as plt 

plt.scatter(clusters[:, 0], clusters[:, 1]) #Scatter plot of clusters
for n, x in enumerate(clusters):
    print('cluster %d' %n)
    plt.annotate('cluster%d' %n, (x[0], x[1])) #Label each cluster
plt.plot(observation[0], observation[1], 'r+'); #Plot observation
cluster 0
cluster 1
cluster 2
cluster 3

Closest cluster as seen in the plot is 2. Your task is to write a function to calculate this

hint: Find the distance between the observation and each row in the cluster. The cluster to which the observation belongs to is the row with the minimum distance.

distance = $\sqrt {\left( {x_1 - x_2 } \right)^2 + \left( {y_1 - y_2 } \right)^2 }$

In [11]:
np.argmin( np.sqrt( np.sum((clusters - observation)**2, axis=1) ) )
Out[11]:
np.int64(2)
In [12]:
(clusters - observation).__pow__(2).sum(axis=1).__pow__(0.5).argmin()
Out[12]:
np.int64(2)