Data Preprocessing in Python using Scikit-Learn

Nidhi Gajjar
4 min readSep 20, 2021

In one of the previous blog of mine I had talked about data preprocessing using the Orange library in python. Now in this blog I would be describing another way of performing data preprocessing in python using the Scikit-Learn library.

Data Preprocessing

Data preprocessing is a step in the data mining and data analysis process that takes raw data and transforms it into a format that can be understood and analyzed by computers and machine learning.

Dataset used during the process

The Iris flower data set or Fisher’s Iris data set is a multivariate data set. The data set consists of 50 samples from each of three species of Iris (Iris setosa, Iris virginica and Iris versicolor). Four features were measured from each sample: the length and the width of the sepals and petals, in centimeters. Based on the combination of these four features, Fisher developed a linear discriminant model to distinguish the species from each other.

Standardization

Data standardization is the process of rescaling one or more attributes so that they have a mean value of 0 and a standard deviation of 1. Standardization of datasets is a common requirement for many machine learning estimators implemented in scikit-learn. The preprocessing module provides the StandardScaler utility class, which is a quick and easy way to perform Standardization.

Code for Standardization
Output

Normalization

Normalization is used to scale the data of an attribute so that it falls in a smaller range, such as -1.0 to 1.0 or 0.0 to 1.0. It is generally useful for classification algorithms. The function normalize provides a quick and easy way to perform this operation on a single array-like dataset, either using the l1, l2, or max norms.

Function used for Normalization
Output

Encoding

In label encoding in Python, we replace the categorical value with a numeric value between 0 and the number of classes minus 1. If the categorical variable value contains 5 distinct classes, we use (0, 1, 2, 3, and 4). We can’t have text in our data if we’re going to run any kind of model on it. So to convert this kind of categorical text data into model-understandable numerical data, we use the Label Encoder class.

Code for Label Encoding
Output

Discretization

Data discretization refers to a method of converting a huge number of data values into smaller ones so that the evaluation and management of data become easy. Discretization (otherwise known as quantization or binning) provides a way to partition continuous features into discrete values. Certain datasets with continuous features may benefit from discretization, because discretization can transform the dataset of continuous attributes to one with only nominal attributes.

Imputation of missing values

For various reasons, many real world datasets contain missing values, often encoded as blanks, NaNs or other placeholders. Such datasets however are incompatible with many estimators which assume that all values in an array are numerical, and that all have and hold meaning. A basic strategy to use incomplete datasets is to discard entire rows and/or columns containing missing values. However, this comes at the price of losing data which may be valuable (even though incomplete). A better strategy is to impute the missing values, i.e., to infer them from the known part of the data.

The SimpleImputer class provides basic strategies for imputing missing values. Missing values can be imputed with a provided constant value, or using the statistics (mean, median or most frequent) of each column in which the missing values are located. This class also allows for different missing values encodings.

Checking for null values in dataset
Performing Imputation
No null values after imputation in the dataset

Code: https://github.com/nidhi2802/DataScience/tree/master/Practical2-Data%20Preprocessing

--

--

Nidhi Gajjar

Site Reliability Engineer, 2X AWS Certified, AWS Cloud Enthusiastic