0% found this document useful (0 votes)
15 views5 pages

20BCP021 - Assignment - 5

Uploaded by

chatgptplus4us
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)
15 views5 pages

20BCP021 - Assignment - 5

Uploaded by

chatgptplus4us
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/ 5

Name: Yash Prajapati

Roll No.: 20BCP021


Batch: G1
Assignment: 5
Title: Implement simple and multi-linear regression to predict profits for a food
truck. Compare the performance of the model on linear and multi-linear
regression

Objective: The objective of this lab assignment is to implement simple and


multi-linear regression models to predict profits for a food truck business. By
comparing the performance of these two regression models, you will gain
insights into when and how to use simple and multi-linear regression
techniques.
Task:
1. Simple Linear Regression

Code:
import pandas as pd
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt

# Create the dataset


data = {
'Population': [10000, 15000, 20000, 9000],
'Years_in_business': [5, 6, 6, 5],
'Profit': [10000, 12000, 13000, 12000]
}
df = pd.DataFrame(data)

# Simple Linear Regression using 'Population'


X_population = df[['Population']]
y = df['Profit']

simple_lr_population = LinearRegression()
simple_lr_population.fit(X_population, y)

# Equation: Profit = m * Population + c


m_population = simple_lr_population.coef_[0]
c_population = simple_lr_population.intercept_
print(f"Equation using 'Population': Profit = {m_population} * Population +
{c_population}")

# Simple Linear Regression using 'Years_in_business'


X_years = df[['Years_in_business']]
simple_lr_years = LinearRegression()
simple_lr_years.fit(X_years, y)

# Equation: Profit = m * Years_in_business + c


m_years = simple_lr_years.coef_[0]
c_years = simple_lr_years.intercept_
print(f"Equation using 'Years_in_business': Profit = {m_years} *
Years_in_business + {c_years}")

# Predict Profit for the missing data point using 'Population'


missing_data_population = pd.DataFrame({'Population': [12000]})
predicted_profit_population =
simple_lr_population.predict(missing_data_population)
print(f"Predicted Profit using 'Population' for 12,000 population:
{predicted_profit_population[0]}")

# Predict Profit for the missing data point using 'Years_in_business'


missing_data_years = pd.DataFrame({'Years_in_business': [4]})
predicted_profit_years = simple_lr_years.predict(missing_data_years)
print(f"Predicted Profit using 'Years_in_business' for 4 years in business:
{predicted_profit_years[0]}")

Output:

2. Multilinear Regression

Code:
import pandas as pd
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score

# Create the dataset


data = {
'Population': [10000, 15000, 20000, 9000],
'Years_in_business': [5, 6, 6, 5],
'Profit': [10000, 12000, 13000, 12000]
}
df = pd.DataFrame(data)

# Separate features and target variable


X = df[['Population', 'Years_in_business']]
y = df['Profit']

# Multi-Linear Regression
multi_lr = LinearRegression()
multi_lr.fit(X, y)

# Predict Profit for the missing data point


missing_data = pd.DataFrame({'Population': [12000], 'Years_in_business':
[4]})
predicted_profit = multi_lr.predict(missing_data)
print(f"Predicted Profit for 12,000 population and 4 years in business:
{predicted_profit[0]}")

# Performance Evaluation (Multi-Linear Regression)


y_pred = multi_lr.predict(X)
mse = mean_squared_error(y, y_pred)
r2 = r2_score(y, y_pred)
print(f"Multi-Linear Regression - MSE: {mse}, R2: {r2}")

Output:

3. Model Comparison and Interpretation

1. Predicted Profit for Missing Data Point:


Multi-Linear Regression: 10711.5384
Simple Linear Regression (using Population): 11487
Simple Linear Regression (using Years_in_business): 9500
2. R2 Scores:
Multi-Linear Regression: 0.5101
Simple Linear Regression (using Population): 0.4982
Simple Linear Regression (using Years_in_business): 0.4737

You might also like