0% found this document useful (0 votes)
39 views4 pages

Program - 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 train models, visualize results, and evaluate performance metrics such as Mean Squared Error and R^2 Score. The program is executed in the main block to showcase both regression techniques.

Uploaded by

sadiqhamuskan4
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)
39 views4 pages

Program - 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 train models, visualize results, and evaluate performance metrics such as Mean Squared Error and R^2 Score. The program is executed in the main block to showcase both regression techniques.

Uploaded by

sadiqhamuskan4
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

Program 7

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 [Link] as plt
from [Link] import fetch_california_housing
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from [Link] import PolynomialFeatures, StandardScaler
from [Link] import make_pipeline
from [Link] import mean_squared_error, r2_score

def linear_regression_california():
housing = fetch_california_housing(as_frame=True)
X = [Link][["AveRooms"]]
y = [Link]

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

model = LinearRegression()
[Link](X_train, y_train)

y_pred = [Link](X_test)

[Link](X_test, y_test, color="blue", label="Actual")


[Link](X_test, y_pred, color="red", label="Predicted")
[Link]("Average number of rooms (AveRooms)")
[Link]("Median value of homes ($100,000)")
[Link]("Linear Regression - California Housing Dataset")
[Link]()
[Link]()

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 = "[Link]
column_names = ["mpg", "cylinders", "displacement", "horsepower", "weight", "acceleration",
"model_year", "origin"]
data = pd.read_csv(url, sep='\s+', names=column_names, na_values="?")
data = [Link]()

X = data["displacement"].[Link](-1, 1)
y = data["mpg"].values

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

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


poly_model.fit(X_train, y_train)

y_pred = poly_model.predict(X_test)

[Link](X_test, y_test, color="blue", label="Actual")


[Link](X_test, y_pred, color="red", label="Predicted")
[Link]("Displacement")
[Link]("Miles per gallon (mpg)")
[Link]("Polynomial Regression - Auto MPG Dataset")
[Link]()
[Link]()

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

output:

You might also like