<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 CSAL4243
Step1:  Plot data
Step2:  Train model
Step3:  Predict output using trained model
Step4:  Plot results
Step5:  Do it yourself
Step6:  Predict labels using model and print it
<ASSISTANT_TASK:>
Python Code:
import pandas as pd
from sklearn import linear_model
import matplotlib.pyplot as plt
# read data in pandas frame
dataframe = pd.read_csv('datasets/house_dataset1.csv')
# assign x and y
x_feature = dataframe[['Size']]
y_labels = dataframe[['Price']]
# check data by printing first few rows
dataframe.head()
Explanation: CSAL4243: Introduction to Machine Learning
Muhammad Mudassir Khan (mudasssir.khan@ucp.edu.pk)
Lecture 2: Linear Regression
Overview
What is Machine Learning?
Definition
The three different types of machine learning
Learning from labled data with supervised learning
Regression for predicting continuous outcomes
Classification for predicting class labels
Unsupervised Learning
Reinforcement Learning
Machine Learning pipeline
Goal of Machine Learning algorithm
# Linear Regression with one variable
Model Representation
Cost Function
Simple case when $\theta_0$ = 0
When both $\theta_0$ and $\theta_1$ can vary
So what is the price of the house?
Read data
Plot data
Train Model
Predict output using trained model
Plot results
Resources
Credits
<br>
<br>
What is Machine Learning? <a name="what-is-ml"></a>
Machine Learning is making computers/machcines learn from data
Learning improve over time with more data
Definition
Mitchell ( 1997 ) define Machine Learning as “A computer
program is said to learn from experience E with respect to some class of tasks T
and performance measure P , if its performance at tasks in T , as measured by P ,
improves with experience E .”
Example: playing checkers.
T = the task of playing checkers.
E = the experience of playing many games of checkers
P = the probability that the program will win the next game.
<br>
<br>
The three different types of machine learning
<img style="float: left;" src="images/01_01.png", width=500>
<br>
<br>
Supervised Learning
<img style="float: left;" src="images/01_02.png", width=500>
<br>
<br>
Regression for predicting continuous outcomes
<img style="float: left;" src="images/01_04.png", width=300> <img style="float: right;" src="images/01_11.png", width=500>
Classification for predicting class labels
<img style="float: left;" src="images/01_03.png", width=300> <img style="float: right;" src="images/01_12.png", width=500>
<br>
<br>
Unsupervised Learning
<img style="float: left;" src="images/01_06.png" width=300>
<br>
<br>
Reinforcement Learning
<img style="float: left;" src="images/01_05.png", width=300>
<br>
<br>
Machine Learning pipeline
<img style="float: left;" src="images/model.png">
x is called input variables or input features.
y is called output or target variable. Also sometimes known as label.
h is called hypothesis or model. 
pair (x<sup>(i)</sup>,y<sup>(i)</sup>) is called a sample or training example
dataset of all training examples is called training set.
m is the number of samples in a dataset.
n is the number of features in a dataset excluding label.
<img style="float: left;" src="images/02_02.png", width=400> 
<img style="float: right;" src="images/02_03.png", width=400>
Question ?
What is x<sup>(2)</sup> and y<sup>(2)</sup>?
<br>
<br>
Goal of Machine Learning algorithm
How well the algorithm will perform on unseen data. 
Also called generalization.
<br>
<br>
Linear Regression with one variable
Model Representation
Model is represented by h<sub>$\theta$</sub>(x) or simply h(x)
For Linear regression with one input variable h(x) =  $\theta$<sub>0</sub> + $\theta$<sub>1</sub>x
<img style="float: left;" src="images/02_01.png">
<img style="float: left;" src="images/02_05.png">
$\theta$<sub>0</sub> and $\theta$<sub>1</sub> are called weights or parameters.
Need to find $\theta$<sub>0</sub> and $\theta$<sub>1</sub> that maximizes the performance of model.
Question
<img style="float: left;" src="images/02_15.jpg", width=600>
<br>
<br>
<br>
Cost Function
<img style="float: left;" src="images/02_14.png", width=700>
Let $\hat{y}$ = h(x) =  $\theta$<sub>0</sub> + $\theta$<sub>1</sub>x
Error in single sample (x,y) = $\hat{y}$ - y = h(x) - y 
Cummulative error of all m samples = $\sum_{i=1}^{m} (h(x^i) - y^i)^2$
Finally mean error or cost function = J($\theta$) = $\frac{1}{2m}\sum_{i=1}^{m} (h(x^i) - y^i)^2$
<br>
<br>
Simple case when $\theta_0$ = 0
<img style="float: center;" src="images/02_06.png", width=700>
<br>
Question
<img style="float: center;" src="images/02_15.png", width=700>
<br>
<img style="float: center;" src="images/02_07.png", width=700>
<img style="float: center;" src="images/02_08.png", width=700>
<img style="float: center;" src="images/02_09.png", width=700>
<b>
When both $\theta_0$ and $\theta_1$ can vary
<br>
<img style="float: center;" src="images/02_10.png", width=700>
<img style="float: center;" src="images/02_11.png", width=700>
<img style="float: center;" src="images/02_12.png", width=700>
<img style="float: center;" src="images/02_13.png", width=700>
<br>
<br>
So what is the price of the house?
Read data
End of explanation
#visualize results
plt.scatter(x_feature, y_labels)
plt.show()
y_labels.shape
Explanation: Plot data
End of explanation
#train model on data
body_reg = linear_model.LinearRegression()
body_reg.fit(x_feature, y_labels)
print ('theta0 = ',body_reg.intercept_)
print ('theta1 = ',body_reg.coef_)
Explanation: Train model
End of explanation
hx = body_reg.predict(x_feature)
Explanation: Predict output using trained model
End of explanation
plt.scatter(x_feature, y_labels)
plt.plot(x_feature, hx)
plt.show()
Explanation: Plot results
End of explanation
theta0 = 0
theta1 = 0
inc = 1.0
#loop over all values of theta1 from 0 to 1000 with an increment of inc and find cost. 
# The one with minimum cost is the answer.
m = x_feature.shape[0]
n = x_feature.shape[1]
# optimal values to be determined
minCost = 100000000000000
optimal_theta = 0
while theta1 < 1000:
    cost = 0;
    for indx in range(m):
        hx = theta1*x_feature.values[indx,0] + theta0
        cost += pow((hx - y_labels.values[indx,0]),2)
               
    cost = cost/(2*m)        
#     print(theta1)
#     print(cost)
    
    if cost < minCost:
        minCost =  cost
        optimal_theta = theta1
    theta1 += inc
        
print ('theta0 = ', theta0)        
print ('theta1 = ',optimal_theta)
Explanation: Do it yourself
End of explanation
hx = optimal_theta*x_feature
plt.scatter(x_feature, y_labels)
plt.plot(x_feature, hx)
plt.show()
Explanation: Predict labels using model and print it
End of explanation
<END_TASK>