AI Lec 3
AI Lec 3
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 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 ?