Sitemap
VIJAY RAGUL

VIJAY RAGUL

SHARING WITH LOVE, WRITING WITH PASSION 😊

DATA PREPROCESSING

4 min readOct 6, 2024

--

STOP A MOMENT. If you don’t know about data in machine learning and what is dataset. Then this story is going to be different.

SO, take a moment and go to my story are any other stories about data in machine learning.

Here is a link to my stories: DATASET IN MACHINE LEARNING.

OKAY! Let’s get back to the topic.

Press enter or click to view image in full size

WHAT YOU WILL LEARN:

  1. WHAT ARE THE THINGS TO CHECK BEFORE GIVING DATASET AS AN INPUT TO THE MACHINE LEARNING MODEL.
  2. WHAT ARE THE WAYS TO SOLVE THEM

SO, let’s start learning.

A DATASET MAY CONTAIN:

EMPTY FIELD or MISSING VALUE

If you think the dataset, we use to train our model will be perfect. Then you are going to be disappointed. Because the dataset may have fields where the data is not registered in the dataset, it may have an empty space in the field. This may decrease the performance the machine learning model.

EX: Here is an example of a dataset with missing value.

Press enter or click to view image in full size
DATASET WITH MISSING VALUES

In these times, we have to rely on someone who have all the things that can solve this type of problems.

THAT SOMEONE IS NONE OTHER THAN THE SCIKIT LIBRARY.

SCIKIT LIBRARY

The scikit library is a data science library consist of all the modules and these modules consist of classes and functions that can be used to manipulate and process the data in the dataset.

Here is the link to the sklearn library: CLICK ME TO GO THERE

I GOING TO SHOW YOU THE PROGRAM TO DEAL WITH THIS MISSING DATA PROBLEM.

1.from sklearn.impute import SimpleImputer
2.imputer=SimpleImputer(missing_values=np.nan,statergy='mean')
3.imputer.fit_transform(x)

DON’T WORRY, I WILL EXPLAIN THE CODE

Press enter or click to view image in full size
CODE FOR IMPORTING LIBRARY
CODE FOR IMPORTING LIBRARY

In the above you can see the code for importing the necessary library to handle the missing data.

Get VIJAY RAGUL’s stories in your inbox

Join Medium for free to get updates from this writer.

We are importing the necessary class “Simple imputer” from the module called “impute” which is present in “sklearn” library.

Press enter or click to view image in full size
CREATING OBJECT FOR THE CLASS
CREATING OBJECT AND DEALING WITH MISSING VALUES

In the above code. I created a object for this class called Imputer To deal with the missing “numerical” values.

SimpleImputer

This class is used to handle the missing data in the dataset. These allows us to handle the missing data by doing different strategies to substitute ‘effective values’ in the place of missing data. Some of the strategies offered by the SimpleImputer class is mean,average,median,most_frequent and constant.

This SimpleImputer class need two things(parameters) to do the job.

  1. Missing values.
  2. strategy

Missing value and strategy

This missing value is used to tell the SimpleImputer class what we consider as the missing value. We can consider the missing value as specific numbers such as 1,2,3…Etc,If we have no values then we can give the value as np.nan Which stands for “nan-not a number". Then it will check for the value in the dataset. If we have a missing value as a character then the value also is np.nan.

The strategy tells the SimpleImputer class what to do with the value we get from the missing value parameter. If we give the value to the Strategy parameter as mean. It will compute the mean for the specific column and substitute that in that place of missing value.

Press enter or click to view image in full size
FITTING THE CLASS ON THE DATASET
FITTING THE CLASS ON THE DATASET

GUYS, DON’T WORRY THIS IS THE LAST.PLEASE HOLD ON

This line of code above consists of object called Imputer for the class SimpleImputer.Here we are calling the Object (Imputer) to connect with the dataset with the help of method called “fit_transform”.

Fit_transform

It is a combination of two methods:

  1. fit
  2. transform

Fit and transform

Fit method is used to fit the class to the dataset so that we can compute the mean for the dataset. We know finding the mean is not enough we have to substitute the value of mean in the place of missing values. So, we are using the transform method to transform the dataset.

FINALLY, IT IS OVER.

the value for mean for the age column is “31.2” which will be substituted in the place of missing values. The value for most_frequent will be “c85" which is most frequently came in that cabin column and it will be replaced in the place of missing values in the cabin column.

GUYS THANK YOU FOR SPENDING YOUR VALUBLE TIME WITH ME.

--

--

VIJAY RAGUL
VIJAY RAGUL

Written by VIJAY RAGUL

SHARING WITH LOVE, WRITING WITH PASSION 😊