Back to Writing
NOTESmachine-learningpythonneural-networksscikit-learntutorial

House Price Prediction - Part 3: Making Predictions

August 17, 2020Updated Feb 17, 2026

House Price Prediction

In the previous posts, we covered data preparation, building the model architecture, and training the model.

This post will focus on making predictions using the trained model.

Both model training and model prediction use more data than the model training phase alone.

As mentioned in this post, we proceed in order: data preparation, model architecture building, model training, model utilization.

Let's start by preparing the data.

As we saw before, we use the house price dataset. This dataset contains 13 features (attributes) about houses and their corresponding prices.

import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

# Load the dataset
data = pd.read_csv('house_price.csv')

# Split into training and test sets
X_train, X_test, y_train, y_test = train_test_split(
data.drop('price', axis=1),
data['price'],
test_size=0.2,
random_state=42
)

# Normalize features
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)

Building the Neural Network Model

Now let's create a neural network for prediction.

from sklearn.neural_network import MLPRegressor

# Create the model
model = MLPRegressor(
hidden_layer_sizes=(100, 50),
max_iter=1000,
learning_rate_init=0.001,
random_state=42
)

# Train the model
model.fit(X_train_scaled, y_train)

# Check training and test performance
train_score = model.score(X_train_scaled, y_train)
test_score = model.score(X_test_scaled, y_test)

print(f"Training score: {train_score:.4f}")
print(f"Test score: {test_score:.4f}")

Making Predictions

Now let's use the trained model to make predictions on new data.

# Predict on the test set
predictions = model.predict(X_test_scaled)

# Compare predictions with actual values
for i in range(5):
print(f"Actual: {y_test.iloc[i]:.2f}, Predicted: {predictions[i]:.2f}")

# Calculate prediction error
from sklearn.metrics import mean_absolute_error
mae = mean_absolute_error(y_test, predictions)
print(f"Mean Absolute Error: {mae:.2f}")

Evaluating Predictions

It's important to evaluate how well the predictions perform.

from sklearn.metrics import mean_squared_error, r2_score

# Calculate various metrics
mse = mean_squared_error(y_test, predictions)
rmse = np.sqrt(mse)
r2 = r2_score(y_test, predictions)

print(f"Mean Squared Error: {mse:.2f}")
print(f"Root Mean Squared Error: {rmse:.2f}")
print(f"R-squared Score: {r2:.4f}")

The R-squared score is particularly important. It ranges from 0 to 1, where 1 represents a perfect prediction.

Visualizing Results

Let's visualize how well the predictions match the actual values.

import matplotlib.pyplot as plt

plt.figure(figsize=(10, 6))
plt.scatter(y_test, predictions, alpha=0.5)
plt.xlabel('Actual Price')
plt.ylabel('Predicted Price')
plt.title('Actual vs Predicted House Prices')
plt.plot([y_test.min(), y_test.max()], [y_test.min(), y_test.max()], 'r--', lw=2)
plt.show()

If the predictions are good, the points should cluster close to the red diagonal line.

Practical Usage

Once you have a trained model, you can use it to predict new house prices.

# New house features
new_house = np.array([[2500, 4, 2, 1, 1, 1, 5, 5, 1, 1, 1, 1, 1]])
new_house_scaled = scaler.transform(new_house)

predicted_price = model.predict(new_house_scaled)
print(f"Predicted price for the new house: ${predicted_price[0]:,.2f}")

This concludes the house price prediction tutorial.

We covered the entire machine learning pipeline from data preparation to real-world predictions.

In the next post, we'll explore advanced techniques to improve model performance.