0% found this document useful (0 votes)
1 views

Lab # 9

The document is a lab manual for the CS3151 Artificial Intelligence Lab at the University of Management and Technology, Lahore, for Spring 2024. It covers the objectives, task distribution, and detailed steps for performing linear regression, including key concepts, assumptions, applications, and a coding example using scikit-learn. Students are required to complete an exercise and follow specific submission instructions for their work.

Uploaded by

hamidraza
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Lab # 9

The document is a lab manual for the CS3151 Artificial Intelligence Lab at the University of Management and Technology, Lahore, for Spring 2024. It covers the objectives, task distribution, and detailed steps for performing linear regression, including key concepts, assumptions, applications, and a coding example using scikit-learn. Students are required to complete an exercise and follow specific submission instructions for their work.

Uploaded by

hamidraza
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

University Of Management and Technology, Lahore

Lab Manual 09
CS3151-Artificial Intelligence Lab

Course Instructor Mr. Junaid Abdullah


Mansoor
Lab Instructor (s) Mr. Junaid Abdullah
Mansoor
Section V6
Semester Spring 2024
CS3151: Artificial Intelligence Lab

Table of Contents
1 Objectives ............................................................................................................................... 3
2 Task Distribution ..................................................................................................................... 3
3 Linear Regression ................................................................................................................... 3
3.1 Key Concepts ................................................................................................................... 4
3.2 Assumptions of Linear Regression .................................................................................. 4
4 Steps in Performing Linear Regression: ................................................................................. 4
4.1 Applications of Linear Regression ................................................................................... 4
5 Example .................................................................................................................................. 5
6 Code ........................................................................................................................................ 5
7 Exercise (50 Marks) ................................................................................................................ 6
8 Submission Instructions .......................................................................................................... 6
CS3151: Artificial Intelligence Lab

1 Objectives
After performing this lab, students shall be able to understand the following:
 Linear Regression
 Linear Regression model with scikit-learn

2 Task Distribution
Total Time 170 Minutes

Linear Regression 15 Minutes

Linear regression model with scikit- 25 Minutes


learn

Exercise 120 Minutes

Online Submission 10 Minutes

3 Linear Regression
Linear regression uses the relationship between the data-points to draw a straight line through all
them. Linear regression is a statistical method used to model the relationship between a dependent
variable and one or more independent variables. The goal is to find a linear equation that best
predicts the dependent variable based on the independent variables. The simplest form is simple
linear regression, which involves a single independent variable. Multiple linear regression involves
two or more independent variables.
This line can be used to predict future values
CS3151: Artificial Intelligence Lab

3.1 Key Concepts


 Dependent Variable (Y): The outcome or the variable being predicted or explained.
 Independent Variable (X): The variable(s) used to predict the dependent variable.
 Linear Equation: In simple linear regression, the relationship is modeled using the equation
Y=a+bX where:

 Y is the predicted value.


 X is the independent variable.
 a (intercept) is the value of Y when X=0.
 b (slope) represents the change in Y for a one-unit change in X.

3.2 Assumptions of Linear Regression

 Linearity: The relationship between the dependent and independent variables is linear.
 Independence: The residuals (errors) are independent.
 Homoscedasticity: The residuals have constant variance at every level of XXX.
 Normality: The residuals of the model are normally distributed.

4 Steps in Performing Linear Regression:


 Collect Data: Gather data for the dependent and independent variables.
 Plot Data: Create a scatter plot to visualize the relationship between variables.
 Calculate the Line of Best Fit: Use methods like the least squares method to determine the
linear equation that best fits the data.
 Evaluate the Model: Assess the fit of the model using metrics like R-squared, residual plots,
and hypothesis tests for the coefficients.
 Make Predictions: Use the linear equation to make predictions for new data points.

4.1 Applications of Linear Regression


 Predicting economic indicators like GDP growth based on various factors.
 Estimating the relationship between advertising spend and sales revenue.
 Modeling the impact of education and experience on salary.
CS3151: Artificial Intelligence Lab

5 Example

Consider a simple linear regression model predicting house prices (Y) based on square footage
(X):

If the computed model is House Price=50,000+200×Square Footage\text{House Price} = 50,000


+ 200 \times \text{Square Footage}House Price=50,000+200×Square Footage, this implies that
for every additional square foot, the house price increases by $200, with a base price of $50,000
when the square footage is zero.

Linear regression is a foundational technique in statistics and machine learning, widely used for
its simplicity and interpretability.

6 Code
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
import matplotlib.pyplot as plt

# Generate synthetic data


np.random.seed(0)
X = 2 * np.random.rand(100, 1)
y = 4 + 3 * X + np.random.randn(100, 1)

# Convert to pandas DataFrame for convenience


data = pd.DataFrame(data=np.hstack((X, y)), columns=['Square_Footage', 'House_Price'])

# Split the data into training and testing sets


X = data[['Square_Footage']]
y = data['House_Price']
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)

# Create and train the model


model = LinearRegression()
model.fit(X_train, y_train)

# Make predictions on the test set


CS3151: Artificial Intelligence Lab

y_pred = model.predict(X_test)

# Evaluate the model


mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)

print(f'Mean Squared Error: {mse}')


print(f'R^2 Score: {r2}')

# Plot the results


plt.scatter(X_test, y_test, color='blue', label='Actual')
plt.plot(X_test, y_pred, color='red', linewidth=2, label='Predicted')
plt.xlabel('Square Footage')
plt.ylabel('House Price')
plt.legend()
plt.show()

7 Exercise (50 Marks)

8 Submission Instructions
Always read the submission instructions carefully.
• Rename your Jupyter notebook to your roll number and download the notebook as
.ipynb extension.
• To download the required file, go to File->Download .ipynb
• Only submit the .ipynb file. DO NOT zip or rar your submission file.
• Submit this file on Google Classroom under the relevant assignment.
• Late submissions will not be accepted

You might also like