Learn Python – Python SimpleImputer module- Basic and advance

In this tutorial, we are going to research about the SimpleImputer module of the Sklearn library, and it was once beforehand known as impute module however up to date in the modern variations of the Sklearn library. We will discuss the SimpleImputer class and how we can use it to take care of lacking statistics in a dataset and exchange the lacking values internal the dataset the usage of a Python program.

SimpleImputer class

A scikit-learn type that we can use to deal with the missing values in the statistics from the dataset of a predictive mannequin is called SimpleImputer class. With the assist of this class, we can change NaN (missing values) values in the dataset with a precise placeholder. We can put into effect and use this module category by way of using the SimpleImputer() approach in the program.

Syntax for SimpleImputer() method:

To put in force the SimpleImputer() category technique into a Python program, we have to use the following syntax:

SimpleImputer(missingValues, strategy)  

Parameters: Following are the parameters which has to be described whilst the use of the SimpleImputer() method:

missingValues: It is the missing values placeholder in the SimpleImputer() method which has to be imputed during the execution, and by default, the value for missing values placeholder is NaN.
strategy: It is the data that is going to replace the missing values (NaN values) from the dataset, and by default, the value method for this parameter is 'Mean'. The strategy parameter of the SimpleImputer() method can take 'Mean', 'Mode', Median' (Central tendency measuring methods) and 'Constant' value input in it.
fillValue: This parameter is used only in the strategy parameter if we give 'Constant' as replacing value method. We have to define the constant value for the strategy parameter, which is going to replace the NaN values from the dataset.

SimpleImputer type is the module class of Sklearn library, and to use this class, first we have to installation the Sklearn library in our system if it is no longer current already.

Installation of Sklearn library:

We can installation the Sklearn through the usage of the following command inner the command terminal instantaneous of our system:

pip install sklearn  

After pressing the enter key, the sklearn module will start installing in our device, as we can see below:

Now, the Sklearn module is mounted in our system, and we can go in advance with the SimpleImputer type function.

Handling NaN values in the dataset with SimpleImputer class

Now, we will use the SimpleImputer category in a Python software to manage the missing values current in the dataset (that we will use in the program). We will define a dataset in the example application while giving some missing values in it, and then we use the SimpleImputer class technique to cope with those values from the dataset through defining its parameters. Let’s understand the implementation of this thru an example Python program.

Example 1: Look at the following Python program with a dataset having NaN values described in it:

# Import numpy module as nmp  
import numpy as nmp  
# Importing SimpleImputer class from sklearn impute module  
from sklearn.impute import SimpleImputer  
# Setting up imputer function variable  
imputerFunc = SimpleImputer(missing_values = nmp.nan, strategy ='mean')  
# Defining a dataset  
dataSet = [[32, nmp.nan, 34, 47], [17, nmp.nan, 71, 53], [19, 29, nmp.nan, 79], [nmp.nan, 31, 23, 37], [19, nmp.nan, 79, 53]]  
# Print original dataset  
print("The Original Dataset we defined in the program: \n", dataSet)  
# Imputing dataset by replacing missing values  
imputerFunc = imputerFunc.fit(dataSet)  
dataSet2 = imputerFunc.transform(dataSet)  
# Printing imputed dataset  
print("The imputed dataset after replacing missing values from it: \n", dataSet2)  


The Original Dataset we defined in the program: 
 [[32, nan, 34, 47], [17, nan, 71, 53], [19, 29, nan, 79], [nan, 31, 23, 37], [19, nan, 79, 53]]
The imputed dataset after replacing missing values from it: 
 [[32.   30.   34.   47.  ]
 [17.   30.   71.   53.  ]
 [19.   29.   51.75 79.  ]
 [21.75 31.   23.   37.  ]
 [19.   30.   79.   53.  ]]


We have first off imported the numpy module (to outline a dataset) and sklearn module (to use the SimpleImputer classification method) into the program. Then, we described the imputer to manage the lacking values the use of the SimpleImputer category method, and we used the ‘mean’ strategy to change the missing values from the dataset. After that, we have defined a dataset in the program the use of the numpy module function and gave some lacking values (NaN values) in the dataset. Then, we printed the authentic dataset in the output. After that, we have imputed and replaced the lacking values from the dataset with the imputer that we have described previously in the software with SimpleImputer class. After imputing the dataset and replacing the lacking values from it, we have printed the new dataset as a result.

As we can see in the output, the imputed cost dataset having suggest values in the area of lacking values, and that’s how we can use the SimpleImputer module class to take care of NaN values from a dataset.


We have study about the SimpleImputer category approach in this method, and we learned how we may want to use it to manage the NaN values current in a dataset. We discovered about the strategy price parameter, which we use to define the technique for changing the NaN values of the dataset. We have also realized about the set up of the Sklearn library, and then last, we used the SimpleImputer classification approach in an instance to impute the dataset.