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

7 A

The document outlines a process for predicting California housing prices using a linear regression model. It includes loading the dataset, selecting features, splitting the data into training and testing sets, training the model, and evaluating its performance using Mean Squared Error and R-squared metrics. Additionally, it visualizes the predicted versus actual housing prices with a scatter plot and a regression line.
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)
14 views2 pages

7 A

The document outlines a process for predicting California housing prices using a linear regression model. It includes loading the dataset, selecting features, splitting the data into training and testing sets, training the model, and evaluating its performance using Mean Squared Error and R-squared metrics. Additionally, it visualizes the predicted versus actual housing prices with a scatter plot and a regression line.
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/ 2

import pandas as pd

from sklearn.datasets import fetch_california_housing


from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
import matplotlib.pyplot as plt

# Load the California housing dataset


housing = fetch_california_housing(as_frame=True)
df = housing.frame

# Select features and target variable


X = df[['MedInc', 'HouseAge', 'AveRooms', 'AveBedrms', 'Population', 'AveOccup', 'Latitude',
'Longitude']]
y = df['MedHouseVal']

# Split data into training and testing sets


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

# Train the linear regression model


model = LinearRegression()
model.fit(X_train, y_train)

# Make predictions on the test set


y_pred = model.predict(X_test)

# Evaluate the model


mse = mean_squared_error(y_test, y_pred)
r2 = r2_score(y_test, y_pred)

print(f"Mean Squared Error: {mse}")


print(f"R-squared: {r2}")

# Plotting predicted vs actual values


plt.scatter(y_test, y_pred)
plt.xlabel("Actual Values")
plt.ylabel("Predicted Values")
plt.title("Actual vs. Predicted Housing Prices")

# Add a regression line for visual assessment


plt.plot([min(y_test), max(y_test)], [min(y_test), max(y_test)], color='red')
plt.show()

Output
Mean Squared Error: 0.5558915986952435
R-squared: 0.5757877060324514

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy