Table of Contents
- What is pre-processing
- categorical encoding
- One hot encoding
- handling missing values
- Video Tutorial ( 2 Videos )
1 What is pre-processing?
- A fact in a data scientist job is that often spends 80% of his time in data pre-processing than in modelingit is one of the very major important steps in the data science life cycle. The data that we get from a real-life scenario has a lot of noises like features may have different scaling, anomalies, categorical values, NAN values. So it’s very important to handle all the cases and we have to make sure data is clean and ready to feed the model.
- Pre-processing can improve the performance of the model effectively. So model totally depends on how good the pre-processing steps have been taken to parse and clean the data
- scikit-learn makes our processes easy we’ll just import that one specific class from scikit-learn as the pre-processing.
2 Scaling the value
- If a feature1 from the dataset having in a range of 0 to 1 and feature2 in the range of 0 to thousand now by the time you feed that data to the model it creates a change in the performance.
- So we’ll need to scale all features in data to a certain range .Let’s check how we can do that.
One of the most used scaling technique in machine learning.After Standardization each feature is transformed such that mean is centered to zero and standard deviation will be one
Magical line in sklearn to implement Standardization
b Min-Max scaling
After Min-Max scaling each feature is transformed such that all the values are ranged from 0 to 1.
Magical line in sklearn to implement minmaxscalar
- Converting a scale value to a unit vector is known as normalization
- Normalization is a very important concept since the way the data is fed into the model is usually in the form of an array or as a part of a vector now.
Magical line in sklearn to implement minmaxscalar:
4 categorical encoding
- If the features in dataset have discrete values then they considered as categorical features . Example:(Months ,weeks ,countries)
- Similarly if features in dataset have continuous values then they considered as numerical features .Example:(height , prices ………)
- Most of the time these categorical features in the form of strings
- machine learning model cannot take an input of a string some how we need to convert those as a numerical values for that we are using OrdinalEncoder from Sklearn.
Where Ordinal encoder will give one numerical values like 0,1,2… for string .In above example each feature has two distinct strings Ordinal encoder label male as 1 and female as 0 do same to other features . So using this fitted values we can transform new data.
5 One hot encoding
- when we tried to do ordinal encoding it is done in a specific category 0 to 1 or 0 to 1 or 0 1 2 3.
- With ordinal encoding, model can’t understand it as a categorical variable it assumes
- features as a numerical value that may affect the performance of the model.
- To handle this scenario we need a special type of encoding called one-hot encoding.
- We can perform One hot encoding in scikit-learn from feature extraction we have called the specific function of dict factorization.
- Consider a list of dictionaries which is containing 3 major cities that we have in our data set that is London, Paris, and New York that is done we need to convert them in a specific value such that it doesn’t get converted into a vector value
- we do the fit transform of the data that we ha
- ve and convert it into an array format what happens with the same is we have now three values London Paris now it gets converted as London representing 1 0–0 Paris representing 0 0 1 and new-york being represented as 0 1 0
- Now, what happened is one hot encoding returned us three features 2 through which a combination of them gives us the categorical value once that is being done the computer is able to now differentiate since it’s a categorical value instead of a vector value.
- the only drawback with one-hot encoding is that it increases the size of the data
6 Handling the missing values
- one of the major issues in the data present in a real-life scenario is handling the missing values
- For example, if you take any of the consideration of any historical data or any of the IoT data there’s a specific chance of error or missing values if human interaction is being involved.
- The model cannot take an empty value so you have to replace the data with the minimum error-free and the maximum suitable value for that.
- While Handling the missing values one should be very careful about selecting the strategy in the missing value which can lead to different kinds of biases and errors. The main objective is to remove the unnecessary values in processing so that our model works perfectly.
- Let’s take an example of data frame df with nan ( not a number) values
- Null values in your data sets which cannot be directly fed to the computer for that we need to impute them
- For that, we will call from scikit-learn. impute import simple imputed
- you need to give is the missing values which we are having now in this
- Missing value always cannot be interpreted as nan in our specific case we have nan missing value can be represented as zero can be represented as a very high number, for example, nine nine-nine can be represented by an operator simple like plus-minus.
7 Video Tutorial