0% found this document useful (0 votes)
8 views36 pages

AI Lec 3

The document discusses the process of predicting house prices using linear regression, highlighting the importance of independent and dependent variables. It explains how to derive a regression line that best fits the data by minimizing the sum of squared errors and introduces Python libraries like NumPy, Pandas, and Matplotlib for data handling and visualization. Additionally, it covers concepts such as multi-variate regression, polynomial regression, and the normal equation for model training and prediction.

Uploaded by

Yousef Sayed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views36 pages

AI Lec 3

The document discusses the process of predicting house prices using linear regression, highlighting the importance of independent and dependent variables. It explains how to derive a regression line that best fits the data by minimizing the sum of squared errors and introduces Python libraries like NumPy, Pandas, and Matplotlib for data handling and visualization. Additionally, it covers concepts such as multi-variate regression, polynomial regression, and the normal equation for model training and prediction.

Uploaded by

Yousef Sayed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

Artificial Intelligence

Lec.3
Assoc. Prof. Anwer Sayed
Lecture 4
Example
• Here, single variable means there is only one independent
variable (you will see what is independent variable later). For example
predicting the price of a house based on the area of house. Given below is
a sample of data showing area of the house and corresponding price for
houses in Monroe, New Jersey, USA.
Solution
• Using this data (in the above
table) we will build a
model/function that can tell
us the prices of the homes
whose are is 3300 sq.ft. and
5000 sq.

• The blue points in the above


figure represent the price of
the house for a given area.
The black line which is going
through the data points
(some of them) is the one
which best fits the given data.
It is also called the regression
line.
Solution
• Once we get the line, we can
predict the price of a new
house with a given area. For
example, the price of a house
whose area is 3300 sq.ft., its
price will be 628715$.
Cont.
• Now you might be thinking
how did we get the black line
(regression line)? The line is
not clearly going through
(touching) all the data points
and there are number of
ways we can draw the lines,
green and violet, as shown
below.
Cont.
• How to find the best fit (line) is, we calculate J(𝜃0 , 𝜃1 ), which is an error
between the actual data point and the line which is predicted by the
linear equation. We square the individual errors and sum them up and
we try to minimize those. So, we repeat this process for different lines
and come up with the best line that has the least value for the sum of
squared errors.
Using Python

• NumPy and Pandas libraries are used to load and perform


operations on the data. Matplotlib library is used to plot
graphs. Sklearn or also called scikit-learn is used here to build our
model.
Using Python
• First, we will load our dataset using Pandas.
Cont.
• If you run the above line you should see a scatter plot like
below.
Cont.
• From the previous figure we can see that the distribution is
approximately linear, and we can use linear regression.
• First, we need to create an object of Linear Regression. Now we will
fit our data using fit method. Fitting the data means we are training
the linear regression model using the available data in our dataset.

The first argument for the fit method is the independent variable and
the second argument will be the dependent variable
Cont.
• Now, let’s try to predict the price of house with area as 3300
sq.ft. The code for that is:
Exercise
• Canada’s per capita income
The Normal Equation
• Gradient descent is an iterative algorithm that must be conducted
repeatedly until convergence. Especially for a small number of
features, alternatively, we may use a much faster method called the
'normal equation.
• We know that for a function of f(θ), the θ values that make the
derivative of f(θ) zero are the maximum or minimum point for f(θ).
The Normal Equation
Multi-Variate Regression
Gradient descent verse Normal Equation
Which to use ?
Example
• In this example, we will predict house prices based on several factors:
square footage, number of bedrooms, and age of the house.
Python Code
Python Code
Output:
• The output will show the test set features, actual prices, and the
model's predicted prices for comparison.
Polynomial Regression
• Another regression algorithm is polynomial, which is suitable when
the data isn't linear, and its graph looks like a curve:
Polynomial Regression
• Python code still uses the Linear Regression model for this but
transforms the features to fit the polynomial formula.
Example
• the position salary data concerning the position and salary of
employees. In this dataset, we have three columns Position,
Level and Salary
Solution
• Step 1: Import the required python packages
Solution
• Step 2: Load the dataset
Solution
• Step 3: Data analysis
Cont.
Cont.
Step 4: Split the dataset into dependent/independent variables
• Experience (X) is the independent variable
Salary (y) is dependent on experience
Cont.
Step 5: Train the regression model
We are going to train the Polynomial Regression model along with the Linear
Regression model to compare both results in the end. Pass the X and y data
into the regressor models.
Step 6: Predict the result
• Here comes the interesting part, when we are all set and ready to
predict any value of y (Salary) dependent on X (Position, Level) with
the trained model using regressor.predict
Visualize predictions
• Its time to test our predicted results by plotting graphs
• Prediction with Linear Regression
First, we plot the graph between actual data and predicted values by Linear
Regression to the regressor line.
Prediction with Polynomial Regression
• Let’s check with an example of Level 7.5 and see what Salary our
models predict and how accurate it is.
Any
Questions ?

You might also like