Training a DATASET Using Linear Regression(An ML technique)
Tags: machinelearning, linearregression, datascience, eventdriven
Published: 2020-10-11 11:16:54

<!-- wp:paragraph -->
<p>Few days back  when I was doing a DS course from <em>uaceit.com </em>,I learned making my very first data science project on Jupyter Notebook using an effective ML algorithm i.e. <strong>Linear Regression</strong>. I learnt understanding data and how we can relate it to get better  and desired outcomes. Linear Regression can be defined as "<em>An approach that models a change in 1 or more predictor variables(say x) that produces a linear change in the response variable(y)</em>"</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Lets get started step by step from scratch </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>STEP 1</strong> : <strong>READING AND ARRANGING DATASET IN  PROPER FORMAT</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In the 1st step ,I imported pandas library to read dataset with its read.csv() function. The dataset is taken as a link in the address variable. Since the data was raw containing unnamed &amp; unseparated columns ,So it was converted in a readable form. Then stored as Dataset.csv onto my local machine.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":190,"width":780,"height":515,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large is-resized"><img src="https://alvi15home.files.wordpress.com/2020/10/step1.png?w=836" alt="" class="wp-image-190" width="780" height="515"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>STEP 2: MAKING EXPLORATORY ANALYSIS ON DATA</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>EDA generally called as <strong>Exploratory  Data Analysis</strong> is a kind of statistical study that is done on data sets to figure out main characteristics and perform hypothesis as required. For that ,we require data visualization library i.e. <strong>seaborn </strong>to understand the relationship between the different data points/predictors. <strong>Info()</strong> and <strong>describe() </strong>functions were used to see where the distribution points lie and the range of the dataset. Since the graph showed symmetrical sign it is observed that the predictors are linearly related with the target variable.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":220,"width":500,"height":242,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large is-resized"><img src="https://alvi15home.files.wordpress.com/2020/10/kdeplot.png?w=500" alt="" class="wp-image-220" width="500" height="242"/><figcaption>A kde plot depicts the probability density at different values in a continuous variable</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>STEP 3: ANALYZING THE CO RELATION BETWEEN VALUES IN  A DATASET</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To visualize co relation values<strong>(from -1 to 1)</strong>  between predictor and target variable we use <strong>heat map() </strong>function and <strong>corr()</strong> function of our data frame to see results.<br> 1 means  =>perfect positive linear relationship<br>-1 means =>perfect negative linear relationship<br> 0 means  =>no relationship at all</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Consider the last row for better understanding , MLDGP shares the highest positive linear relationship with target variable(0.54) and GATS1p shares the highest  negative linear relationship (-0.31)</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":216,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://alvi15home.files.wordpress.com/2020/10/corele.png?w=418" alt="" class="wp-image-216"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>STEP 4: MODELLING DATASET USING LINEAR REGRESSION</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>For training and modelling dataset, we will use <strong>sklearn </strong> python library. We will import <strong>StandardScaler ()  </strong>and <strong> train_test_split()</strong> function from preprocessing and model_selection sub modules of sklearn.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The dataset is divided into 2 parts for training:</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>X =&gt; for displaying the feature variables(independent)</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Y =&gt; for displaying the response variable(dependent)</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Here, another important concept that needs to be understand is of <strong>Scaling </strong>.We use the feature scaling technique to standardize the independent variables present in a dataset to a fixed range of  minimum and maximum values of 0 to 1.This is done using StandardScaler() function.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":200,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://alvi15home.files.wordpress.com/2020/10/scaler.png?w=360" alt="" class="wp-image-200"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Moreover, the train_test_split() will divide the dataset into two subsets  <strong>70% </strong>for training and <strong>30%</strong> for testing model.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":202,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large"><img src="https://alvi15home.files.wordpress.com/2020/10/split.png?w=822" alt="" class="wp-image-202"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>After that, we will apply <strong>LinearRegression()</strong> method from model selection sub module. The fit method takes 2 arguments for training(input and output) as supervised learning gives predictions on mapping  input to output based on randomly organized training sets. We will then apply <strong>predict()</strong> method for our model to make predictions.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>By checking residuals i.e. difference between the observed&nbsp;<em>y</em>-value (from scatter plot) and the predicted&nbsp;<em>y</em>-value (from regression equation line),we will see  how appropriate our model and assumptions are .</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":206,"width":480,"height":382,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large is-resized"><img src="https://alvi15home.files.wordpress.com/2020/10/residuals.png?w=336" alt="" class="wp-image-206" width="480" height="382"/><figcaption>scatter plot to see residual values</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Since, residuals are randomly placed, above and below x-axis(regression line), it is confirmed that our model is <strong>Linear Appropriate</strong>. This means that a residual may have a positive, negative or zero values.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>A&nbsp;<strong>linear regression</strong>&nbsp;requires&nbsp;<strong>residuals</strong>&nbsp;to be&nbsp;<strong>normally distributed</strong> with a mean of 0. <strong>Residuals</strong>&nbsp;have the same&nbsp;<strong>distribution</strong>&nbsp;for all values of the explanatory variables. But here we see that our distributed values differ means that the model is biased and we need to revise the model. Problems like overfitting or multi collinearity limits the effectiveness of this algorithm.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"id":210,"width":323,"height":205,"sizeSlug":"large","linkDestination":"none"} -->
<figure class="wp-block-image size-large is-resized"><img src="https://alvi15home.files.wordpress.com/2020/10/distribution.png?w=390" alt="" class="wp-image-210" width="323" height="205"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Each time the model is fitted ,it trains a random subset and gives a different accuracy score. The model is able to predict only <strong>40 to 60 % </strong>of the target variable which is quite low. In short, the fact that the residuals were not equal to 0 has affected the accuracy rate of the model .You can find the code link at the end of the blog.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>One way to address the problem is simply removing those data points which are too distant from the mean of data or we can increase the complexity by adding the number of predictors into dataset.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Other algorithms like KNN-regression, LASSO or Ridge Regression can also be fitted on the model as alternatives to apply regularization to the dataset and improves its accuracy. To learn more about this technique you can follow the links below.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Until then stay connected  for new data insight stories!</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Have a Good Day</strong> :) </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><em>Source:</em> </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Project:</strong>-->https://uaceit.com/courses/your-first-data-science-project/ &amp;                                                                                         </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>              -->https://datatofish.com/multiple-linear-regression-python/</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Linear Regression:</strong> https://en.wikipedia.org/wiki/Linear_regression</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>GITHUB repo: </strong> https://github.com/ToobaAhmedAlvi/1stDSproject</p>
<!-- /wp:paragraph -->