7.9 KiB
import numpy as np
def print_info(a):
""" Print the content of an array, and its metadata. """
txt = f"""
dtype\t{a.dtype}
ndim\t{a.ndim}
shape\t{a.shape}
strides\t{a.strides}
"""
print(a)
print(txt)
Mind-on exercises
Exercise 1: warm up¶
What is the expected output shape for each operation?
a = np.arange(5)
b = 5
np.shape(a-b)
a = np.ones((7, 1))
b = np.arange(7)
np.shape(a*b)
a = np.random.randint(0, 50, (2, 3, 3))
b = np.random.randint(0, 10, (3, 1))
np.shape(a-b)
a = np.arange(100).reshape(10, 10)
b = np.arange(1, 10)
np.shape(a+b)
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!
## Your code here
Exercise 3¶
Task: Find the closest cluster to the observation.
Again, use broadcasting: DO NOT iterate cluster by cluster
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]])
Lets plot this data
In the plot below, + is the observation and dots are the cluster coordinates
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], '+'); #Plot observation
Closest cluster as seen by 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 }$
## Your code here
Sources + Resources¶
ASPP 2016 - Stéfan van der Walt - https://github.com/ASPP/2016_numpy
Basic Numpy: http://scipy-lectures.org/intro/numpy/index.html
Advanced Numpy: http://scipy-lectures.org/advanced/advanced_numpy/index.html
Numpy chapter in "Python Data Science Handbook" https://jakevdp.github.io/PythonDataScienceHandbook/02.00-introduction-to-numpy.html