<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Srishilesh PS on Medium]]></title>
        <description><![CDATA[Stories by Srishilesh PS on Medium]]></description>
        <link>https://medium.com/@srishilesh?source=rss-a2e495acb964------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*CCtBJRRulE6FDIlUgTn2Lw.jpeg</url>
            <title>Stories by Srishilesh PS on Medium</title>
            <link>https://medium.com/@srishilesh?source=rss-a2e495acb964------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 17:33:42 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@srishilesh/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Data Preprocessing made simple]]></title>
            <link>https://medium.com/@srishilesh/data-preprocessing-made-simple-f86ab9e0366f?source=rss-a2e495acb964------2</link>
            <guid isPermaLink="false">https://medium.com/p/f86ab9e0366f</guid>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[data-pre-processing]]></category>
            <category><![CDATA[tutorial]]></category>
            <category><![CDATA[data-science]]></category>
            <dc:creator><![CDATA[Srishilesh PS]]></dc:creator>
            <pubDate>Tue, 14 May 2019 07:05:08 GMT</pubDate>
            <atom:updated>2019-05-14T07:05:08.835Z</atom:updated>
            <content:encoded><![CDATA[<blockquote>The content of this article is inspired by Udemy Machine Learning A-Z course by Kirill Eremenko and Hadelin De Ponteves.</blockquote><p>If you are a Data Scientist, the most important part of your journey is Data Preprocessing. This part determines the rest of your work. During this stage of Data Collection, the data we acquire may not be always correct, we may have some missing values, unwanted features or string data. But for any good Machine learning model, we will have to choose only the required features for good accuracy of our model. For good accuracy of the model, we will have to have clean data. This stage of your journey is called Data Preprocessing.</p><figure><img alt="Image result for data preprocessing" src="https://cdn-images-1.medium.com/proxy/1*FXy7iopWSzMTJX1LoAlr5A.png" /><figcaption>Data scientist Journey</figcaption></figure><p>We can divide our process into 4 stages:</p><ol><li>Splitting the Train and Test dataset</li><li>Handling Missing values</li><li>Encoding of Categorical features</li><li>Feature Scaling</li></ol><p>Now, let’s look into each of the above-mentioned topics with deeper insights. This tutorial also includes the code.</p><h4><strong>Splitting the Train and Test dataset</strong></h4><p>We perform this operation for validating a Model. Many people make a huge mistake by predicting the accuracy of the trained dataset. They make predictions from their trained data and compare those with the actual values of the trained data. This kind of approach fails sometimes. We will have both good and bad predictions. So, our machine learning model will not be so accurate for new data.</p><p>Suppose, you have a dataset for House price prediction which is unrelated to the door color. In the sample of data, all the homes with green color door are expensive. The model’s job is to predict based on this sample, but here it finds a pattern that doors with Green color give High cost. The model might be accurate for the training data. But, this doesn’t work out with the test data. So, we indirectly measure the feature which is not required to build the model. Thus, the accuracy of the test data becomes very low.</p><p>Thus, the most straightforward method to solve this is to exclude some data while training. And this is known as <strong>Validation data.</strong></p><pre># Importing the libraries<br>import numpy as np<br>import matplotlib.pyplot as plt<br>import pandas as pd </pre><pre># Importing the dataset<br>dataset = pd.read_csv(&#39;./Data.csv&#39;)<br>X = dataset.iloc[:, :-1].values<br>y = dataset.iloc[:, 3].values </pre><pre># Splitting the dataset into the Training set and Test set<br>from sklearn.model_selection import train_test_split</pre><pre>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)</pre><p>The above code imports the necessary packages for Data manipulation. Then, the dataset is imported as .csv file(Comma Separated Values). We later split the Target variable ‘y’ and the remaining features into ‘X’.</p><p>Later, we import the mostly used Python Package for Data manipulation known as Sklearn. The below line imports necessary packages for the splitting of the data as the Train and Test set.</p><pre>from sklearn.model_selection import train_test_split</pre><pre>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)</pre><p>Since the whole dataset is split into Train and Test datasets, we have to use 4 separate variables for handling the data known as X_train, X_test, y_train, and y_test. The train_test_split() function splits according to the parameters passed. Here, test_size = 0.2 denotes that 20% of the whole dataset is split as Test data, with a random split. If random_state is not mentioned, the splitting will be done sequentially. Sometimes, this would create a problem in certain datasets, where the data is inadequate.</p><p>Splitting of Train and Test datasets is always not necessary if both are given separately.</p><h4>Handling Missing data</h4><p>Here, there are different methods that can be followed. But, let’s go with the trivial one.</p><pre># Taking care of missing data</pre><pre>from sklearn.preprocessing import Imputer</pre><pre>imputer = Imputer(missing_values = &#39;NaN&#39;, strategy = &#39;mean&#39;, axis = 0) #Mean strategy by replacing NaN along the columns(axis=0)</pre><pre>imputer = imputer.fit(X[:, 1:3]) # Fit the replace data</pre><pre>X[:, 1:3] = imputer.transform(X[:, 1:3]) # replace the original data</pre><p>Imputer of sklearn.preprocessing package is used for handling missing data. The working of Imputer is by finding the values = ‘NaN’ throughout the axis =0 (Columns) and fill the missing data by the average of remaining data present.</p><h4>Encoding of Categorical features</h4><p>In any Machine learning model, numerical data makes more sense to the Computer than the String data. So, the main objective of this process is to convert the similar repeating Categorical values into numerical data.</p><p>This is usually done by Encoding the data with LabelEncoder</p><pre># Encoding categorical data</pre><pre># Encoding the Independent Variable</pre><pre>from sklearn.preprocessing import LabelEncoder</pre><pre>labelencoder_X = LabelEncoder()</pre><pre>X[:, 0] = labelencoder_X.fit_transform(X[:, 0])</pre><p>We import LabelEncoder from sklearn.preprocessing and create a LabelEncoder() function. Now, fit and transform the training categorical data into this function.</p><p>Now, all the string data is converted into numerical data, which is easy to fit the model.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/146/0*k8qchL2yUHwJ0spN.png" /><figcaption>Categories</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/131/0*45XB7HLshd6hIKxF.png" /><figcaption>Label Encoded</figcaption></figure><p>Here, a major problem occurs when there are too many categories. We can resolve that problem by One Hot Encoding, where each label is encoded as Binary representation for decimals.</p><figure><img alt="Image result for label encoding" src="https://cdn-images-1.medium.com/proxy/0*T5jaa2othYfXZX9W." /></figure><p>If you have a look at the above example, the Label encoded is converted to One Hot Encoding, where if <strong>Apple</strong> can be represented as <strong>100, Chicken </strong>as <strong>010</strong></p><pre># Use Onr hot encoding to prevent the problem of ordering in the #label encoder</pre><pre>&#39;&#39;&#39;<br>France Germany England</pre><pre>1       0       0</pre><pre>0       1       0</pre><pre>0       0       1</pre><pre>&#39;&#39;&#39;</pre><pre>from sklearn.preprocessing import OneHotEncoder<br>onehotencoder = OneHotEncoder(categorical_features = [0])</pre><pre>X = onehotencoder.fit_transform(X).toarray()</pre><p>The above code can One Hot Encode the training data and later you can easily play with those data, which is relatively easy to represent.</p><h4>Feature Scaling</h4><p>This is one of the most useful technique when you handle a huge amount of data. In certain, Machine learning models, the objective functions do not perform properly without Normalization. Example, Classification is done by calculating the Eucleadian Distance between two points. If huge data, then the calculation of the distance changes, then the distance will be governed by a particular feature.</p><pre>from sklearn.preprocessing import StandardScaler</pre><pre>sc_X = StandardScaler()</pre><pre>X_train = sc_X.fit_transform(X_train)</pre><pre>X_test = sc_X.transform(X_test)</pre><pre>sc_y = StandardScaler()</pre><pre>y_train = sc_y.fit_transform(y_train)</pre><p>This is can be achieved by Standard Scaling of the data. Fit and transform your training data and test data into the StandardScaler() function. This is easily done by sklearn packages.</p><p>Now, we come to the end of this tutorial on Data Preprocessing. The above-followed methods are primitive methods which are generally used by Data Scientists. But, better methods can also be used for improving your accuracy by tuning the hyperparameters of these functions.</p><p>I hope you got some idea about Data Preprocessing. Hope you enjoyed the read. Thank you.</p><p>Any queries can be posted in the comments section.</p><p><em>Author: Srishilesh P S <br>Email: srishilesh@gmail.com<br>LinkedIn: </em><a href="https://www.linkedin.com/in/srishilesh/"><em>https://www.linkedin.com/in/srishilesh/</em></a><em><br>GitHub:</em><a href="https://github.com/srishilesh"><em>https://github.com/srishilesh</em></a><em><br>Kaggle: </em><a href="https://www.kaggle.com/srishilesh"><em>https://www.kaggle.com/srishilesh</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f86ab9e0366f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>