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

Program7 Explaination

The document outlines a program demonstrating Linear Regression using the Boston Housing Dataset and Polynomial Regression with the Auto MPG Dataset. It details the steps for loading data, training models, making predictions, and evaluating performance using Mean Squared Error and R² Score. Additionally, it highlights important errors in the code, such as indentation issues and missing imports.

Uploaded by

tulasi
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)
3 views3 pages

Program7 Explaination

The document outlines a program demonstrating Linear Regression using the Boston Housing Dataset and Polynomial Regression with the Auto MPG Dataset. It details the steps for loading data, training models, making predictions, and evaluating performance using Mean Squared Error and R² Score. Additionally, it highlights important errors in the code, such as indentation issues and missing imports.

Uploaded by

tulasi
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

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 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
from [Link] import make_pipeline

 Used for data handling, plotting, and machine learning

Note: You used mean_squared_error and r2_score later but didn’t import them.
You should add:

from [Link] import mean_squared_error, r2_score

2. Linear Regression (California Housing)

Load Data

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

 X (input) → Average number of rooms


 y (output) → House price

Split Data- train_test_split(X, y, test_size=0.2) 80% training, 20% testing

Train Model

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

 Fits a straight line equation

Predict

y_pred = [Link](X_test) * Predicts house prices


Plot

[Link](X_test, y_test)
[Link](X_test, y_pred)

 Scatter → actual values


 Line → predicted values

3. Polynomial Regression (Auto MPG)

Load Data

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

 Reads dataset
 Removes missing values

Select Features

X = data[["displacement"]]
y = data["mpg"]

 Input → engine size


 Output → fuel efficiency

Train Model

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


poly_model.fit(X_train, y_train)

 Converts input to degree 2 polynomial


 Fits a curved model (parabola)

Predict

y_pred = poly_model.predict(X_test)

Plot [Link](X_test, y_test)


[Link](X_test, y_pred)

 Blue → actual
 Red → predicted

🔹 4. Evaluation Metrics

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


print("R^2 Score:", r2_score(y_test, y_pred))
✔ Mean Squared Error (MSE)

 Measures average error


 Lower value = better model

✔ R² Score

 Measures accuracy (0 to 1)
 Closer to 1 = better fit

Important Errors in Your Code

1. Indentation error

print("Polynomial Regression - Auto MPG Dataset")


print(...)

Remove extra spaces before print

2. Missing import
Add:

from [Link] import mean_squared_error, r2_score

Final Summary

 Linear Regression → straight line prediction (house price)


 Polynomial Regression → curved prediction (mpg)
 Graphs visualize results
 MSE & R² evaluate model performance

You might also like