Machine Learning: Data Preprocessing
It will be fun if we relate this with our real life. Remember the task of washing and then chopping raw vegetables before cooking a dish. It is somewhat similar to this task. Data Preprocessing is a way of converting raw form of data to a much more usable form. It looks similar right? The only difference between the two task is that one helps to cook tasty dish and other a Machine Learning model for prediction of classes of new observations. Indeed, the step of Data Preprocessing step would not sound interesting for most of the people but, it is the very first important step because every time you build a model you always have Data Preprocessing phase to work on. The step of Preprocessing should be done in such a way that the model can be trained in right way because the efficiency of the model is dependent on this step.
Steps Involved in Data Preprocessing: This step is completely dependent on some preprocessing tools:
- Importing Libraries: We need to import basically 3 libraries(NumPy, Matplotlib, pandas) in the beginning to start the phase of preprocessing. But, what a library is? A library is symbol of module which contains functions and classes through which you can perform actions and operations.
i)NumPy- You need this library to work with array. As future model will require arrays for implementation.
ii)Matplotlib- You need this library to create some beautiful charts.
iii)pandas- This library is not only helpful to import the data set but, also create matrix of features and the dependent variable vectors.
2. Importing The Dataset: Here comes the steps of importing the data set, which is one of the important phase.
The dataset variable here acts as a data frame of rows and columns similar to the data set(Data.csv).
i) Creating Matrix of Features and Dependent Variable Vector:
All we need to know is to play with the indexes of Matrix and we can easily create these two feature matrices. X here acts as independent variable matrix and Y as dependent variable vector. These two feature matrices will be the first need of the furthers steps of preprocessing.
3. Take Care of Missing Data: Do look out the above picture and the rows containing value NaN which signify that the values are missing and it can affect the learning of the model.
It could be done in many ways like Deleting/Ignoring the rows with missing values. But, it is beneficial when the number of rows with missing values are at most 1%.
Get Gaurav Parihar’s stories in your inbox
Join Medium for free to get updates from this writer.
The other ways are replacing the missing values with median or mean value of that column.
But, the most preferred is replacing with average sum value(Mean) of the column. It is done with the help of best data science library scikit-learn.
4. Encoding Categorical Data: The above feature matrices contains the column with string values which is tough to handle and work with. As the computers works well with binary values. So, we will convert string values into binary values.
i) Encoding The Independent Variables:
We have three categories of data so, the encoded values are:
“India as 0, 0, 0", “China as 0, 0, 1”, and “America as 1, 0, 0”.
ii) Encoding The Dependent Variables:
Here, we have two categories of values so, the encoded values are:
“No as 0” and “Yes as 1”
5. Splitting of Dataset into Training set and Test set: In this step we work on generalization. The purpose of splitting to avoid overfitting. Overfitting happens when a model learns the detail and noise in the training data to the extent that it negatively impacts the performance of the model on new data. It optimizes the training accuracy. We need our model to predict well the outcome of new observation(Test set), which is similar to training set.
6. Feature Scaling:
Feature Scaling is important to bring the variety of data in the same scale. It involves the step of normalising the data in the particular range.
The two most commonly used scaling techniques are Normalization and Standardization. But, we prefer normalization technique.
Conclusion: The above mentioned steps are really important to work with as the data preprocessing is one of the standing pillar of the Model building process. I hope the mentioned details will help many of the beginners to develop the understanding of data preprocessing.










