0% found this document useful (0 votes)
2 views3 pages

PGM 7

The document outlines a program that demonstrates Linear Regression using the California Housing Dataset and Polynomial Regression using the Auto MPG Dataset. It includes functions to fetch, process, and visualize the data, as well as to evaluate model performance using Mean Squared Error and R^2 Score. The program utilizes libraries such as NumPy, Pandas, Matplotlib, and Scikit-learn for data manipulation and modeling.

Uploaded by

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

PGM 7

The document outlines a program that demonstrates Linear Regression using the California Housing Dataset and Polynomial Regression using the Auto MPG Dataset. It includes functions to fetch, process, and visualize the data, as well as to evaluate model performance using Mean Squared Error and R^2 Score. The program utilizes libraries such as NumPy, Pandas, Matplotlib, and Scikit-learn for data manipulation and modeling.

Uploaded by

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

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 linear_regression_california():

# Fetching California Housing dataset

housing = fetch_california_housing(as_frame=True)

X = housing.data[["AveRooms"]]

y = housing.target

# Train-test split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Linear Regression model

model = LinearRegression()

model.fit(X_train, y_train)

y_pred = model.predict(X_test)

# Plotting
plt.scatter(X_test, y_test, color="blue", label="Actual")

plt.plot(X_test, y_pred, color="red", label="Predicted")

plt.xlabel("Average number of rooms (AveRooms)")

plt.ylabel("Median value of homes ($100,000)")

plt.title("Linear Regression - California Housing Dataset")

plt.legend()

plt.show()

# Model performance

print("Linear Regression - California Housing Dataset")

print("Mean Squared Error:", mean_squared_error(y_test, y_pred))

print("R^2 Score:", r2_score(y_test, y_pred))

def polynomial_regression_auto_mpg():

# URL of Auto MPG dataset

url = "https://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data"

column_names = ["mpg", "cylinders", "displacement", "horsepower", "weight", "acceleration",


"model_year", "origin"]

# Loading and cleaning the dataset

data = pd.read_csv(url, sep='\s+', names=column_names, na_values="?")

data = data.dropna()

# Feature and target variables

X = data["displacement"].values.reshape(-1, 1)

y = data["mpg"].values

# Train-test split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)


# Polynomial Regression model

poly_model = make_pipeline(PolynomialFeatures(degree=2), StandardScaler(), LinearRegression())

poly_model.fit(X_train, y_train)

y_pred = poly_model.predict(X_test)

# Plotting

plt.scatter(X_test, y_test, color="blue", label="Actual")

plt.scatter(X_test, y_pred, color="red", label="Predicted")

plt.xlabel("Displacement")

plt.ylabel("Miles per gallon (mpg)")

plt.title("Polynomial Regression - Auto MPG Dataset")

plt.legend()

plt.show()

# Model performance

print("Polynomial Regression - Auto MPG Dataset")

print("Mean Squared Error:", mean_squared_error(y_test, y_pred))

print("R^2 Score:", r2_score(y_test, y_pred))

if __name__ == "__main__":

print("Demonstrating Linear Regression and Polynomial Regression\n")

linear_regression_california()

polynomial_regression_auto_mpg()

You might also like