Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

We can use regression models in Python to predict housing prices using various techniques. Here is an example using linear regression:

  1. Import the necessary libraries:
import pandas as pd import numpy as np from sklearn.linear_model import LinearRegression from sklearn.model_selection import train_test_split 
  1. Load the housing dataset:
df = pd.read_csv('housing.csv') 
  1. Create the feature matrix (X) and target vector (y):
X = df.drop(['Price'], axis=1) y = df['Price'] 
  1. Split the 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=0) 
  1. Train the linear regression model:
regressor = LinearRegression() regressor.fit(X_train, y_train) 
  1. Predict the housing prices using the test data:
y_pred = regressor.predict(X_test) 
  1. Evaluate the model performance by calculating the mean squared error (MSE) and the coefficient of determination (R²):
from sklearn.metrics import mean_squared_error, r2_score mse = mean_squared_error(y_test, y_pred) r2 = r2_score(y_test, y_pred) print("Mean squared error (MSE):", mse) print("Coefficient of determination (R²):", r2) 
  1. Optionally, visualize the predicted values against the actual values:
import matplotlib.pyplot as plt plt.scatter(y_test, y_pred) plt.xlabel("Actual Prices") plt.ylabel("Predicted Prices") plt.title("Actual vs Predicted Prices") plt.show() 

Note that there are various other regression models and techniques that can be used for housing price prediction, including decision trees, random forests, and support vector regression (SVR).