9951 explained code solutions for 126 technologies


python-scipyHow do I normalize a numpy array using Python?


Normalizing a Numpy array is a common operation in data science and machine learning. It is used to ensure that all values in an array are within a specific range, typically between 0 and 1. To normalize a Numpy array, you can use the following code:

import numpy as np

# Create an array of random numbers
arr = np.random.rand(5)

# Normalize the array
arr_norm = (arr - np.min(arr)) / (np.max(arr) - np.min(arr))

print(arr_norm)

The output of this code will be a normalized array of values between 0 and 1.

The code works by first creating an array of random numbers using the np.random.rand function. Then, the np.min and np.max functions are used to find the minimum and maximum values in the array. Finally, the array is normalized by subtracting the minimum value and dividing by the difference between the maximum and minimum values.

Code explanation

  • np.random.rand: This function is used to create an array of random numbers.
  • np.min and np.max: These functions are used to find the minimum and maximum values in the array.
  • arr - np.min(arr) and np.max(arr) - np.min(arr): These are used to normalize the array by subtracting the minimum value and dividing by the difference between the maximum and minimum values.

Helpful links

Edit this code on GitHub