0% found this document useful (0 votes)
1 views2 pages

Program 7

This program demonstrates Linear Regression using the Boston Housing Dataset and Polynomial Regression using the Auto MPG Dataset. It trains a linear model on the Boston data and evaluates its performance with Mean Squared Error and R² Score, followed by a polynomial model on the Auto MPG data. Visualizations are provided for both regression analyses to illustrate the predictions against actual values.

Uploaded by

harshitharg058
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)
1 views2 pages

Program 7

This program demonstrates Linear Regression using the Boston Housing Dataset and Polynomial Regression using the Auto MPG Dataset. It trains a linear model on the Boston data and evaluates its performance with Mean Squared Error and R² Score, followed by a polynomial model on the Auto MPG data. Visualizations are provided for both regression analyses to illustrate the predictions against actual values.

Uploaded by

harshitharg058
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:

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_openml


from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from [Link] import mean_squared_error, r2_score
# Load Boston Housing dataset
boston = fetch_openml(name='boston', version=1, as_frame=True)

# Convert all features and target to numeric


X = [Link](pd.to_numeric)
y = pd.to_numeric([Link])

# Train-test split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
linear_model = LinearRegression()
linear_model.fit(X_train, y_train)
y_pred = linear_model.predict(X_test)
print("LINEAR REGRESSION – BOSTON HOUSING DATASET")
print("Mean Squared Error:", mean_squared_error(y_test, y_pred))
print("R² Score:", r2_score(y_test, y_pred))
[Link](figsize=(6,5))
[Link](y_test, y_pred)
[Link]("Actual House Prices")
[Link]("Predicted House Prices")
[Link]("Linear Regression – Boston Housing Dataset")
[Link](True)
[Link]()

auto_mpg = fetch_openml(name='autoMpg', version=1, as_frame=True)

# Feature data
X_full = auto_mpg.data

# Target data
y_full = auto_mpg.[Link](float)

# Combine features and target


df = X_full.copy()
df['mpg'] = y_full

# Remove missing values


df = [Link]()

# Select feature and target


X = df[['horsepower']].astype(float)
y = df['mpg'].astype(float)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
from [Link] import PolynomialFeatures

poly = PolynomialFeatures(degree=2)
X_train_poly = poly.fit_transform(X_train)
X_test_poly = [Link](X_test)
poly_model = LinearRegression()
poly_model.fit(X_train_poly, y_train)
y_poly_pred = poly_model.predict(X_test_poly)

print("\nPOLYNOMIAL REGRESSION – AUTO MPG DATASET")


print("Mean Squared Error:", mean_squared_error(y_test, y_poly_pred))
print("R² Score:", r2_score(y_test, y_poly_pred))
X_range = [Link]([Link](), [Link](), 100).reshape(-1, 1)
X_range_poly = [Link](X_range)
y_range_pred = poly_model.predict(X_range_poly)

[Link](figsize=(6,5))
[Link](X, y, color='gray', label="Actual Data")
[Link](X_range, y_range_pred, color='red', label="Polynomial Fit")
[Link]("Horsepower")
[Link]("Miles Per Gallon (MPG)")
[Link]("Polynomial Regression – Auto MPG Dataset")
[Link]()
[Link](True)
[Link]()

You might also like