This project trains and evaluates a neural network to predict house prices using the Boston Housing dataset. The script uses PyTorch for building and training the neural network and scikit-learn for data preprocessing.
- Python 3.x
- PyTorch
- scikit-learn
-
Clone the repository:
git clone https://github.com/your-repository/house-price-prediction.git cd house-price-prediction
-
Install the required packages:
pip install torch scikit-learn
The compute
function trains and evaluates a neural network model for house price prediction.
epoch
(int): The number of training epochs.
result
(dict): A dictionary containing the average loss on the validation set.
from your_module import compute
result = compute(epoch=100)
print(result)
The test
function runs a simple test to ensure that the compute
function works correctly.
from your_module import test
test()
- Loading the Dataset: The Boston Housing dataset is loaded using scikit-learn's
load_boston
function. - Preprocessing: The features are standardized using
StandardScaler
. The dataset is split into training and testing sets usingtrain_test_split
. - Creating Datasets and DataLoaders: The data is converted to PyTorch tensors and wrapped in
DataLoader
objects for easy batch processing.
A simple feedforward neural network with one hidden layer is defined using PyTorch's nn.Module
. The network consists of:
- An input layer
- A hidden layer with ReLU activation
- An output layer
The network is trained using mean squared error (MSE) loss and the Adam optimizer. The training loop iterates through the data for the specified number of epochs, updating the model parameters to minimize the loss.
The trained model is evaluated on the test set, and the average loss is computed and returned.