9951 explained code solutions for 126 technologies


python-kerasHow do I normalize data using Python and Keras?


Normalizing data in Python and Keras is a common pre-processing step for machine learning. It is used to scale all values in a given dataset to a range between 0 and 1. This helps to ensure that all features in the dataset are treated equally by the model.

To normalize data using Python and Keras, you can use the MinMaxScaler class from the sklearn.preprocessing library.

# example code
from sklearn.preprocessing import MinMaxScaler

# define scaler
scaler = MinMaxScaler()

# fit scaler on data
scaler.fit(data)

# transform data
data_scaled = scaler.transform(data)

# print scaled data
print(data_scaled)

Output example

[[0.1 0.5 0.3]
 [0.7 0.2 0.6]]

The code above does the following:

  1. Imports the MinMaxScaler class from the sklearn.preprocessing library.
  2. Defines the scaler object.
  3. Fits the scaler on the data.
  4. Transforms the data using the scaler.
  5. Prints the scaled data.

Helpful links

Edit this code on GitHub