How To Standardise Features In Python? - ProjectPro

Recipe Objective

It is very rare to find a raw dataset which perfectly follows certain specific distribution. Usually every dataset needs to be standarize by any means.

So this is the recipe on how we can standardise features in Python.

Master the Art of Data Cleaning in Machine Learning

Table of Contents

  • Recipe Objective
    • Step 1 - Import the library
    • Step 2 - Setting up the Data
    • Step 3 - Using StandardScaler

Step 1 - Import the library

from sklearn import preprocessing import numpy as np

We have only imported numpy and preprocessing which is needed.

Step 2 - Setting up the Data

We have created an numpy array with different values. x = np.array([[-500.5], [-100.1], [0], [100.1], [900.9]])

Step 3 - Using StandardScaler

StandardScaler is used to remove the outliners and scale the data by making the mean of the data 0 and standard deviation as 1. So we are creating an object scaler to use standardScaler. We have fitted the fit data and transformed train and test data form standard scaler. Finally we have printed the dataset. scaler = preprocessing.StandardScaler() standardized_x = scaler.fit_transform(x) print(x) print(standardized_x) As an output we get

[[-500.5] [-100.1] [ 0. ] [ 100.1] [ 900.9]] [[-1.26687088] [-0.39316683] [-0.17474081] [ 0.0436852 ] [ 1.79109332]]

Tag » How To Standardize Data In Python