TensorFlow/Keras:Predicting House Prices | LearnMuchMore

TensorFlow/Keras:Predicting House Prices

In this project, you will use TensorFlow/Keras to predict house prices based on features like square footage, number of bedrooms, location, and more. The dataset will be split into training and testing subsets, and you’ll evaluate the model's performance using metrics like mean squared error (MSE).


Objective

  • Train a regression model to predict house prices.
  • Understand how to preprocess data and evaluate the model.

Requirements

Install the necessary libraries:

bash
pip install tensorflow pandas numpy matplotlib scikit-learn

Step 1: Load and Explore the Dataset

Use the California Housing Dataset from Scikit-learn.

python
from sklearn.datasets import fetch_california_housing
import pandas as pd
# Load dataset
data = fetch_california_housing(as_frame=True)
df = data['data']
df['Price'] = data['target']
# Display the first few rows
print(df.head())
# Features and target
X = df.drop(columns=['Price'])
y = df['Price']

Step 2: Preprocess the Data

Normalize features to improve model performance.

python
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# Split data
X_train, X_test, y_train, y_test = train_test_split(X, y, 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)

Step 3: Build the TensorFlow Model

Create a simple regression model using Keras.

python
import tensorflow as tf from tensorflow.keras
import Sequential from tensorflow.keras.layers
import Dense
# Build the model
model = Sequential([ Dense(64, activation='relu',
input_shape=(X_train.shape[1],)),
Dense(32, activation='relu'), Dense(1)
# Single output for regression
])
# Compile the model
model.compile(optimizer='adam', loss='mse', metrics=['mae'])
# Summary
model.summary()

Step 4: Train the Model

Train the model and validate it on the test set.

python
# Train the model
history = model.fit(X_train_scaled, y_train, validation_split=0.2, epochs=50, batch_size=32, verbose=1)
# Evaluate the model
loss, mae = model.evaluate(X_test_scaled, y_test)
print(f"Mean Absolute Error: {mae}")

Step 5: Visualize Training Performance

Plot the loss and mean absolute error over epochs.

python
import matplotlib.pyplot as plt
# Plot training and validation loss
plt.figure(figsize=(10, 6))
plt.plot(history.history['loss'], label='Training Loss')
plt.plot(history.history['val_loss'], label='Validation Loss')
plt.title('Model Loss Over Epochs')
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()
plt.show()

Step 6: Make Predictions

Predict prices for the test set and visualize the results.

python
# Make predictions
y_pred = model.predict(X_test_scaled)
# Scatter plot of actual vs predicted prices
plt.figure(figsize=(10, 6))
plt.scatter(y_test, y_pred, alpha=0.5)
plt.title('Actual vs Predicted House Prices')
plt.xlabel('Actual Prices')
plt.ylabel('Predicted Prices')
plt.grid()
plt.show()

Step 7: Save and Load the Model

Save the trained model for reuse.

python
# Save the model
model.save('house_price_model.h5')
# Load the model
loaded_model = tf.keras.models.load_model('house_price_model.h5')

Extensions

  1. Add More Features:
    • Engineer additional features like proximity to amenities, crime rates, etc.
  2. Hyperparameter Tuning:
    • Use Keras Tuner to optimize the model’s architecture.
  3. Deploy the Model:
    • Deploy as a web app using Flask or Streamlit for user interaction.

Conclusion

This project provides a foundational understanding of regression with TensorFlow/Keras. By applying this process to real-world datasets, learners can create scalable solutions for predicting numerical outcomes in various domains.