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

House Price Prediction

The document outlines a Python script that loads the California housing dataset and implements a linear regression model to predict house prices. It splits the data into training and testing sets, trains the model, and evaluates its performance using mean squared error. The script also calculates and displays the regression coefficients, intercept, and the final regression equation.

Uploaded by

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

House Price Prediction

The document outlines a Python script that loads the California housing dataset and implements a linear regression model to predict house prices. It splits the data into training and testing sets, trains the model, and evaluates its performance using mean squared error. The script also calculates and displays the regression coefficients, intercept, and the final regression equation.

Uploaded by

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

#Loading dataset

from sklearn.datasets import fetch_california_housing


california = fetch_california_housing()

#Load the model we will use, the linear regression model


from sklearn.linear_model import LinearRegression

#Load the package to split the dataset into training and testing sets
from sklearn.model_selection import train_test_split

#Load the package to validate the model


from sklearn.metrics import mean_squared_error

#Description of the dataset


print(california.DESCR)

#Load the dataset and put it into my_data


my_data = california

#We will split the data into two groups, with 80% as the training set and 20% as
the test set.
#(train_x, train_y) is the 80% training set, used to train the model,
#where x represents the data and y represents the answers (i.e., the actual house
prices).
#(test_x, test_y) is the 20% test set, used to validate the model's predictive
ability.
#random_state: Setting a random_state ensures that your results are reproducible
#shuffle: If shuffle is set to True, the data will be mixed up randomly before
splitting it into training and testing sets.
train_x, test_x, train_y, test_y = train_test_split(my_data.data,
my_data.target, test_size=0.2, random_state=42, shuffle=True)

#Retrieve the preloaded package 'Linear Regression Model' and store it in the
variable my_model.
my_model = LinearRegression()

#Train the model using the training set (train_x, train_y).


my_model.fit(train_x, train_y)
#Generating predicted values and storing them in the variable pred
pred = my_model.predict(test_x)

#We can use the evaluation metric MSE (mean squared error) to assess the model's
actual error.
score = mean_squared_error(pred, test_y)
#A lower MSE score is better, indicating that the predicted values are close to the
actual answers
print(" MSE: ", score)

#Calculate the coefficients & intercept


coefficients = my_model.coef_
intercept = my_model.intercept_

#Show the coefficients & intercept


print(coefficients)
print(intercept)

#Generate the equation


equation = ' + '.join([f'{coeff}*{feat}' for feat, coeff in
zip(california.feature_names, coefficients)])
equation = f'y = {equation} + {intercept}'

print("Regression Equation:")
print(equation)

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