Min-Max Scaling Data Normalization Function

  • Share this:

Code introduction


This function uses the NumPy module from the SciPy library to normalize the input array using Min-Max scaling, which subtracts the minimum value of each element in the specified axis from it, and then divides by the difference between the maximum and minimum values of that axis.


Technology Stack : NumPy

Code Type : Function

Code Difficulty : Intermediate


                
                    
def normalize_data(data, axis=0):
    """
    Normalize the data along the specified axis using Min-Max scaling.

    Args:
    data (numpy.ndarray): Input data to normalize.
    axis (int): Axis along which to scale the data. Default is 0.

    Returns:
    numpy.ndarray: Normalized data.
    """
    import numpy as np

    min_vals = np.min(data, axis=axis, keepdims=True)
    max_vals = np.max(data, axis=axis, keepdims=True)
    normalized_data = (data - min_vals) / (max_vals - min_vals)
    return normalized_data                
              
Tags: