2.3. NumPy Solution#

2.3.1. Task#

In machine learning tasks, when we deal with data from different features, it is very common that we need to normalize data so that the different units among features would not affect the performances of machine learning algorithms.

Now we have temperature and precipitation observations from 7 sites. Write a line of code to accomplish Min-Max normalization for each feature.

Hint: The equation of Min-Max normalization to \(x\) is \( \begin{align} x_{norm} = \frac{x-\min(x)}{\max(x)-\min(x)} \end{align} \)

import numpy as np
data = np.array([[25.1, 27.5, 32.3, 24.4, 28.2, 29.8, 30.1],  # a row of temperature
                 [20.0,  2.5,  8.5, 10.0,  0.5, 28.4, 12.5]]) # a row of precipitation

# Your solution goes here.
norm = (data - np.min(data, axis=1).reshape(2, 1)) / np.ptp(data, axis=1).reshape(2, 1)
array([[0.08860759, 0.39240506, 1.        , 0.        , 0.48101266,
        0.6835443 , 0.72151899],
       [0.69892473, 0.07168459, 0.28673835, 0.34050179, 0.        ,
        1.        , 0.43010753]])