# Linear Regression

In this post i gonna wet your hands with coding part too, Before we drive further. let me show what type of  examples we gonna solve today.

## 1) Predicting house price for ZooZoo. • ZooZoo gonna buy new house, so we have to find how much it will cost a particular house.

# 2) Predicting which Television Show will have more viewers for next week The Flash and Arrow are my favorite Television ( Tv ) shows. i want to find which Tv show will get more viewers in upcoming  week. Frankly i am so excited to know which show will get more viewers.

# 3) Replacing missing values using linear Regression

You have to solve this problem i will explain every thing you have to do. so please try to solve this problem.

## So let’s drive into coding part

I believe you have installed all the required packages which i specified in my previous post. If not please take some time and install all the packages in this post  python packages for datamining. It would better once your go through post.

### 1) Predicting cost price of  a house for ZooZoo. ZooZoo have the following data set

No.             square_feet                 price
1 150 6450
2 200 7450
3 250 8450
4 300 9450
5 350 11450
6 400 15450
7 600 18450

About data set:

• Square feet is the  Area of house.
• Price is the corresponding cost of  that house.

Steps to Follow :

• As we learn linear regression we know that we have to find linear line for this data so that we can get  θ0 and θ1.
• As you remember our hypothesis equation looks like this where:

• hθ(x) is nothing but the value price(which we are going to predicate ) for particular square_feet  ( means price is a linear function of square_feet)
• θ0 is a constant
• θ1 is the regression coefficient

As we clear what we have to do, let’s start coding.

STEP – 1 :

• First open your favorite text editor and name it as predict_house_price.py.
• The below packages we gonna use in our program ,so  copy them in your predict_house_price.py file.
```
# Required Packages

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn import datasets, linear_model

```
• Just run your code once. if your program is error free then most of the job was done. If you facing  any errors , this means you missed some  packages so please go to this
• Install all the packages in that blog post and run your code once again . This time most probably you will never face any problem.
• Means your program is error free now so we can go to STEP – 2.

STEP – 2

• I stored our data set in to a csv file with name input_data.csv
• So let’s write a function to get our data into X values ( square_feet  ) Y values (Price)
```
# Function to get data
def get_data(file_name):
data = pd.read_csv(file_name)
X_parameter = []
Y_parameter = []
for single_square_feet ,single_price_value in zip(data['square_feet'],data['price']):
X_parameter.append([float(single_square_feet)])
Y_parameter.append(float(single_price_value))
return X_parameter,Y_parameter
```

Line 3:

Reading csv data to pandas DataFrame.

Line 6-9:

Converting pandas dataframe data to X_parameter and Y_parameter data returning them

So let’s print our X_parameters and Y_parameters

```
X,Y = get_data('input_data.csv')
print X
print Y
```

Script Output

```[[150.0], [200.0], [250.0], [300.0], [350.0], [400.0], [600.0]]
[6450.0, 7450.0, 8450.0, 9450.0, 11450.0, 15450.0, 18450.0]
[Finished in 0.7s]```

Step – 3

we converted data to X_parameters and Y_parameter so let’s fit our X_parameters and Y_parameters to Linear Regression model

So we gonna write a function which will take  X_parameters ,Y_parameter and the value you gonna predict  as input and return the θ0 ,θ1  and predicted value

```
# Function for Fitting our data to Linear model
def linear_model_main(X_parameters,Y_parameters,predict_value):

# Create linear regression object
regr = linear_model.LinearRegression()
regr.fit(X_parameters, Y_parameters)
predict_outcome = regr.predict(predict_value)
predictions = {}
predictions['intercept'] = regr.intercept_
predictions['coefficient'] = regr.coef_
predictions['predicted_value'] = predict_outcome
return predictions
```

Line 5-6:

First we are creating an linear model and the training it with our X_parameters and Y_parameters

Line 8-12:

we are creating one dictionary with name predictions and storing θ0 ,θ1  and predicted values. and returning predictions dictionary as an output.

So let’s call our function with predicting value as 700

```
X,Y = get_data('input_data.csv')
predictvalue = 700
result = linear_model_main(X,Y,predictvalue)
print "Intercept value " , result['intercept']
print "coefficient" , result['coefficient']
print "Predicted value: ",result['predicted_value']
```

Script Output:

```Intercept value 1771.80851064
coefficient [ 28.77659574]
Predicted value: [ 21915.42553191]
[Finished in 0.7s]```

Here Intercept value is nothing but   θ0 value and coefficient value is nothing but  θ1 value.

We got the predicted values as 21915.4255 means we done our job of predicting the house price.

For checking purpose we have to see how our data fit to linear regression.So we have to write a function which takes X_parameters and Y_parameters as input and show the linear line fitting for our data.

```
# Function to show the resutls of linear fit model
def show_linear_line(X_parameters,Y_parameters):
# Create linear regression object
regr = linear_model.LinearRegression()
regr.fit(X_parameters, Y_parameters)
plt.scatter(X_parameters,Y_parameters,color='blue')
plt.plot(X_parameters,regr.predict(X_parameters),color='red',linewidth=4)
plt.xticks(())
plt.yticks(())
plt.show()

```

So let call our show_linear_line Function

```
show_linear_line(X,Y)

```

Script Output: ### 2) Predicting which Television Show will have more viewers ### About The FLASH Tv show: The Flash is an American television series developed by writer/producers Greg Berlanti, Andrew Kreisberg and Geoff Johns, airing on The CW. It is based on the DC Comics character Flash (Barry Allen), a costumed superhero crime-fighter with the power to move at superhuman speeds, who was created by Robert Kanigher, John Broome andCarmine Infantino. It is a spin-off from Arrow, existing in the same universe. The pilot for the series was written by Berlanti, Kreisberg and Johns, and directed by David Nutter. The series premiered in North America on October 7, 2014, where the pilot became the most watched telecast for The CW.

### About Arrow Tv Show: Arrow is an American television series developed by writer/producers Greg Berlanti, Marc Guggenheim, and Andrew Kreisberg. It is based on the DC Comics characterGreen Arrow, a costumed crime-fighter created by Mort Weisinger and George Papp. It premiered in North America on The CW on October 10, 2012, with international broadcasting taking place in late 2012. Primarily filmed in Vancouver, British Columbia, Canada, the series follows billionaire playboy Oliver Queen, portrayed by Stephen Amell, who, after five years of being stranded on a hostile island, returns home to fight crime and corruption as a secret vigilante whose weapon of choice is a bow and arrow. Unlike in the comic books, Queen does not initially go by the alias “Green Arrow”.

As both of these are my best-loved Tv show when ever i am watching these shows i feel myself which show have more viewers and i am so interested to guess which show will have more viewers.

So lets write a program which guess( predict ) which Tv Show will have more viewers.

For free drive of our program we need some dataset which having both shows viewers for each episode. Luckly i got this data from Wikipidia and prepared an csv file. It’s looks  like this.

flash_episode flash_us_viewers arrow_episode arrow_us_viewers
1 4.83 1 2.84
2 4.27 2 2.32
3 3.59 3 2.55
4 3.53 4 2.49
5 3.46 5 2.73
6 3.73 6 2.6
7 3.47 7 2.64
8 4.34 8 3.92
9 4.66 9 3.06

About data set:

Us viewers ( millions )

### Step by steps to solving this problem:

• First we have to convert our data to X_parameters and Y_parameters but here we have two X_parameters and Y_parameters so lets’s name them as flash_x_parameter, flash_y_parameter, arrow_x_parameter , arrow_y_parameter.
• Then we have to fit our data to two different  linear regression models first for flash and other for arrow.
• Then we have to predict the number of viewers for next episode for both of the Tv shows.
• Then we can compare the results and we can guess which Tv Shows will have more viewers.

Let’s drive to code this interesting problem.

Step-1

We have to import our packages

```
# Required Packages
import csv
import sys
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn import datasets, linear_model

```

Step-2

Converting our data to flash_x_parameter,flash_y_parameter,arrow_x_parameter ,arrow_y_parameter so lets write a function which will take our data set as input and returns lash_x_parameter,flash_y_parameter,arrow_x_parameter ,arrow_y_parameter values.

```
# Function to get data
def get_data(file_name):
data = pd.read_csv(file_name)
flash_x_parameter = []
flash_y_parameter = []
arrow_x_parameter = []
arrow_y_parameter = []
for x1,y1,x2,y2 in zip(data['flash_episode_number'],data['flash_us_viewers'],data['arrow_episode_number'],data['arrow_us_viewers']):
flash_x_parameter.append([float(x1)])
flash_y_parameter.append(float(y1))
arrow_x_parameter.append([float(x2)])
arrow_y_parameter.append(float(y2))
return flash_x_parameter,flash_y_parameter,arrow_x_parameter,arrow_y_parameter
```

now we have  flash_x_parameters,flash_y_parameters,arrow_x_parameters,arrow_y_parameters. so let’s write a function which will take these above parameters as input and gives an output as which show will have more views.

```
# Function to know which Tv show will have more viewers
def more_viewers(x1,y1,x2,y2):
regr1 = linear_model.LinearRegression()
regr1.fit(x1, y1)
predicted_value1 = regr1.predict(9)
print predicted_value1
regr2 = linear_model.LinearRegression()
regr2.fit(x2, y2)
predicted_value2 = regr2.predict(9)
#print predicted_value1
#print predicted_value2
if predicted_value1 > predicted_value2:
print "The Flash Tv Show will have more viewers for next week"
else:
print "Arrow Tv Show will have more viewers for next week"

```

So let’s write every thing in one file open your editor and name it as prediction.py and copy this total code into prediction.py file.

```
# Required Packages
import csv
import sys
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn import datasets, linear_model

# Function to get data
def get_data(file_name):
data = pd.read_csv(file_name)
flash_x_parameter = []
flash_y_parameter = []
arrow_x_parameter = []
arrow_y_parameter = []
for x1,y1,x2,y2 in zip(data['flash_episode_number'],data['flash_us_viewers'],data['arrow_episode_number'],data['arrow_us_viewers']):
flash_x_parameter.append([float(x1)])
flash_y_parameter.append(float(y1))
arrow_x_parameter.append([float(x2)])
arrow_y_parameter.append(float(y2))
return flash_x_parameter,flash_y_parameter,arrow_x_parameter,arrow_y_parameter

# Function to know which Tv show will have more viewers
def more_viewers(x1,y1,x2,y2):
regr1 = linear_model.LinearRegression()
regr1.fit(x1, y1)
predicted_value1 = regr1.predict(9)
print predicted_value1
regr2 = linear_model.LinearRegression()
regr2.fit(x2, y2)
predicted_value2 = regr2.predict(9)
#print predicted_value1
#print predicted_value2
if predicted_value1 > predicted_value2:
print "The Flash Tv Show will have more viewers for next week"
else:
print "Arrow Tv Show will have more viewers for next week"

x1,y1,x2,y2 = get_data('input_data.csv')
#print x1,y1,x2,y2
more_viewers(x1,y1,x2,y2)

```

Run this program and see which Tv show will have more viewers.

## 3) Replacing missing values using linear Regression

some times we have a situation where we have to do analysis on data which consists of missing values. Some people will remove these missing values and they continue  analysis and some people replace them with min value or max value or mean value it’s good to replace missing value with mean value but so time it’s not the right way to replace missing with mean value so we can use linear regression to replace those missing value very effectively.

This approach goes some thing like this.

First we have find which column we gonna replace missing values and we have to find on which columns this missing values column values more depends on ,then we have to remove the missing value rows. Then consider missing values column as Y_parameters and consider the columns on which this missing values more depend as X_parameters and fit this data to Linear regression model . Now predict the missing values in missing values column by consider the columns on which this missing values column more depends.

Once all this process completed  we will get data without any missing values so we are free to analysis data.

For practice i leave this problem to you so please kindly get some missing values data from online and solve this problem. Leave your comments once you completed .i so happy to view them.

#### Small personal note:

I want to share my personal experience with data mining. I remember in my introductory  datamining classes The instructor starts slow and explains some interesting areas where we can apply datamining and some very basic concepts so i and my friends understand every thing and we show more interest towards datamining. Then suddenly the difficulty leave will sky rocket. This makes a lot of my friends in class feel like extremely frustrated and intimated by course and ultimately they left interest on datamining. So i want to avoid this thing in my blog posts. In my blog post i want to make thing more easygoing this would be possible only when i explain things  with some interest examples moreover i want to make my blog viewers more comfortable learning without any boring so i am in that spirit which leads me towards use this examples.

## Follow us:

###### FACEBOOK| QUORA |TWITTER| REDDIT | FLIPBOARD | MEDIUM | GITHUB

I hope you liked todays post. If you have any questions then feel free to comment below.  If you want me to write on one specific topic then do tell it to me in the comments below.

Advertisements

# ## ￼￼Introduction to Linear Regression:

Linear Regression means predicting scores of one variable from the scores of second variable. The variable we are predicting is called the criterion variable and is referred to as Y. The variable we are basing our predictions is called the predictor variable and is referred to as X. When there is only one predictor variable, the prediction method is called simple regression.The aim of linear regression is to finding the best-fitting straight line through the points. The best-fitting line is called a regression line. The above equation is hypothesis equation

where:

hθ(x) is nothing but the value Y(which we are going to predicate )  for particular x ( means Y is a linear function of x)

θ0 is a constant

θ1 is  the regression coefficient

X is value of the independent variable

## Properties of the Linear Regression Line

Linear Regression line has the following properties:

1. The line minimizes the sum of squared differences between observed values (the y values) and predicted values (the hθ(x) values computed from the regression equation).
2. The regression line passes through the mean of the X values (x) and through the mean of the Y values ( hθ(x) ).
3. The regression constant (θ0) is equal to the y intercept of the regression line.
4. The regression coefficient (θ1) is the average change in the dependent variable (Y) for a 1-unit change in the independent variable (X). It is the slope of the regression line.

The least squares regression line is the only straight line that has all of these properties.

## Goal of  Hypothesis Function

Goal of Hypothesis is to choose θ0 and θ1 , so that hθ(x) is close to Y for our training data,while choosing θ0 and θ1 we have to consider the cost function( J(θ) ) where we are getting low value for cost function( J(θ) ).

The below function is called as cost function, cost function ( J(θ) ) is nothing but just a Squared error function. ## Let’s Understand Linear Regression with Example

Before going to explain linear Regression let me summarize  the things we learn  ## Suppose we have data some thing look’s like this

No. Year Population
1 2000 1,014,004,000
2 2001 1,029,991,000
3 2002 1,045,845,000
4 2003 1,049,700,000
5 2004 1,065,071,000
6 2005 1,080,264,000
7 2006 1,095,352,000
8 2007 1,129,866,000
9 2008 1,147,996,000
10 2009 1,166,079,000
11 2010 1,173,108,000
12 2011 1,189,173,000
13 2012 1,205,074,000

## Now our task is to answer the below questions

No. Year Population
1 2014 ?
2 ?  2,205,074,000

## Let me draw a graph for our data ## Python Code for graph

```
# Required Packages

import plotly.plotly as pyfrom plotly.graph_objs import *

py.sign_in("username", "API_authentication_code")

from datetime import datetime

x = [

datetime(year=2000,month=1,day=1),
datetime(year=2001,month=1,day=1),
datetime(year=2002,month=1,day=1),
datetime(year=2003,month=1,day=1),
datetime(year=2004,month=1,day=1),
datetime(year=2005,month=1,day=1),
datetime(year=2006,month=1,day=1),
datetime(year=2007,month=1,day=1),
datetime(year=2008,month=1,day=1),
datetime(year=2009,month=1,day=1),
datetime(year=2010,month=1,day=1),
datetime(year=2011,month=1,day=1),
datetime(year=2012,month=1,day=1)
]

data = Data([

Scatter(

x = x,

y = [

1014004000,

1029991000,

1045845000,

1049700000,

1065071000,

1080264000,

1095352000,

1129866000,

1147996000,

1166079000,

1173108000,

1189173000,

1205074000]

)

])

plot_url = py.plot(data, filename='DataAspirant')

``` • Now what we will do is we will find the most suitable value for our  θ0 and θ1 using hypotheses equation.
• Where x is nothing but the years , and the hθ(X) is the prediction value for our hypotheses .
• Once we done finding θ0 and θ1 we can find any value.
• Keep in mind we fist find the θ0 and θ1 for our training data.
• Later we will use these θ0 and θ1 values to do prediction for  test data.

Don’t think too much about how to  find  θ0 and θ1 values, in coming posts i will explain how we can find θ0 and θ1 values with nice example and i will explain the coding part too.

## Follow us:

###### FACEBOOK| QUORA |TWITTER| REDDIT | FLIPBOARD | MEDIUM | GITHUB

I hope you liked todays post. If you have any questions then feel free to comment below.  If you want me to write on one specific topic then do tell it to me in the comments below.