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

Experiment 7 ml vtu

The document outlines an experiment to demonstrate Linear and Polynomial Regression using the Boston Housing Dataset and Auto MPG Dataset, respectively. It explains regression analysis, types of regression models, their workings, and applications. Additionally, it provides sample Python code for implementing both regression techniques and visualizing the results.

Uploaded by

vikasvikki158
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)
4 views5 pages

Experiment 7 ml vtu

The document outlines an experiment to demonstrate Linear and Polynomial Regression using the Boston Housing Dataset and Auto MPG Dataset, respectively. It explains regression analysis, types of regression models, their workings, and applications. Additionally, it provides sample Python code for implementing both regression techniques and visualizing the results.

Uploaded by

vikasvikki158
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

Experiment 7

Develop a program to demonstrate the working of Linear Regression and Polynomial


Regression. Use Boston Housing Dataset for Linear Regression and Auto MPG Dataset
(for vehicle fuel efficiency prediction) for Polynomial Regression.

Introduction to Regression Analysis


What is Regression?
Regression is a fundamental statistical and machine learning technique used to model
relationships between variables. It helps in predicting a dependent variable (target) based
on one or more independent variables (features).

Types of Regression Models


1. Linear Regression – Assumes a linear relationship between independent and
dependent variables.
2. Polynomial Regression – Extends linear regression by introducing polynomial terms to
capture non-linearity.

Linear Regression
Definition
Linear Regression models the relationship between an independent variable (x) and a
dependent variable ( y ) using a straight-line equation:
y = mx + c
where:
• m is the slope (coefficient) of the line,
• c is the intercept,
• x is the independent variable,
• y is the dependent variable (predicted value).

Working of Linear Regression


1. Identify the best-fitting line: Uses the least squares method to minimize the error
between actual and predicted values.
2. Compute the cost function: Measures how well the model fits the data using Mean
Squared Error (MSE)
3. Optimize the model parameters: Uses Gradient Descent or other optimization
techniques to find the best m and c.

Applications of Linear Regression


• Predicting sales revenue based on advertising spend.
• Estimating house prices based on size and location.
• Forecasting demand in supply chain management

Polynomial Regression
Polynomial regression is a type of regression analysis used in statistics and machine learning
when the relationship between the independent variable (input) and the dependent
variable (output) is not linear. While simple linear regression models the relationship as a
straight line, polynomial regression allows for more flexibility by fitting a polynomial
equation to the data.
Polynomial Regression is an extension of Linear Regression where the relationship between
variables is modeled using a polynomial equation:

where n represents the degree of the polynomial.

Importance of Polynomial Regression


• When the relationship between variables is non-linear and a straight line does not fit
well.
• Captures curved patterns in data by introducing higher-degree polynomial terms.

Working of Polynomial Regression


1. Transform the input features by introducing polynomial terms.
2. Apply Linear Regression to fit the transformed dataset.
3. Choose the optimal polynomial degree to balance underfitting and overfitting.

Choosing the Right Degree (n)


Degree 1: Equivalent to Linear Regression.
Degree 2-3: Captures slight curves in data while preventing overfitting.
Degree >3: More flexible but risks overfitting (too much complexity).
Applications of Polynomial Regression
Predicting fuel efficiency based on vehicle characteristics.
Modeling economic growth trends over time.
Analyzing the effect of temperature on crop yields.

Sample Program
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures

# Sample dataset
X = np.array([1, 2, 3, 4, 5]).reshape(-1, 1)
y = np.array([2.1,2.9, 3.7, 4.5, 6.1])

# Linear Regression
linear_model = LinearRegression()
linear_model.fit(X, y)
y_linear_pred = linear_model.predict(X)

# Polynomial Regression (degree = 2)


poly_features = PolynomialFeatures(degree=2)
X_poly = poly_features.fit_transform(X)
poly_model = LinearRegression()
poly_model.fit(X_poly, y)
y_poly_pred = poly_model.predict(X_poly)

# Plot results
plt.scatter(X, y, color='blue', label='Original Data')
plt.plot(X, y_linear_pred, color='red', label='Linear Regression')
plt.plot(X, y_poly_pred, color='green', linestyle='dashed', label='Polynomial Regression
(degree=2)')
plt.xlabel("X")
plt.ylabel("y")
plt.legend()
plt.title("Linear vs Polynomial Regression")
plt.show()

# Print coefficients
print("Linear Regression Coefficients:", linear_model.coef_)
print("Linear Regression Intercept:", linear_model.intercept_)
print("Polynomial Regression Coefficients:", poly_model.coef_)
print("Polynomial Regression Intercept:", poly_model.intercept_)

Lab Program-7

Develop a program to demonstrate the working of Linear Regression and Polynomial


Regression. Use Boston Housing Dataset for Linear Regression and Auto MPG Dataset
(for vehicle fuel efficiency prediction) for Polynomial Regression.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.datasets import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures, StandardScaler
from sklearn.pipeline import make_pipeline
from sklearn.metrics import mean_squared_error, r2_score

def plot_results(X_test, y_test, y_pred, xlabel, ylabel, title):


plt.scatter(X_test, y_test, color="blue", label="Actual")
plt.plot(X_test, y_pred, color="red", label="Predicted")
plt.xlabel(xlabel)
plt.ylabel(ylabel)
plt.title(title)
plt.legend()
plt.show()

print(f"{title}")
print("Mean Squared Error:", mean_squared_error(y_test, y_pred))
print("R^2 Score:", r2_score(y_test, y_pred))

def linear_regression():
data = fetch_california_housing(as_frame=True)
X, y = data.data[["AveRooms"]], data.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = LinearRegression().fit(X_train, y_train)
plot_results(X_test, y_test, model.predict(X_test), "AveRooms", "Median Home Value
($100K)", "Linear Regression - California Housing")

def polynomial_regression():
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-
mpg.data"
cols = ["mpg", "cyl", "disp", "hp", "wt", "acc", "yr", "origin"]
data = pd.read_csv(url, sep='\s+', names=cols, na_values="?").dropna()
X, y = data[["disp"]], data["mpg"]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = make_pipeline(PolynomialFeatures(2), StandardScaler(),
LinearRegression()).fit(X_train, y_train)
plot_results(X_test, y_test, model.predict(X_test), "Displacement", "MPG", "Polynomial
Regression - Auto MPG")

if __name__ == "__main__":
linear_regression()
polynomial_regression()

You might also like