A tutorial on building different regression models using the PyTorch modules
A machine learning project that implements neural network regression models to predict temperature based on weather features using PyTorch.
Source: Weather Dataset from Kaggle
Dataset Description: This project uses a comprehensive weather dataset containing 8,784 hourly weather observations with multiple meteorological features.
- Dew Point Temperature (°C): Primary predictor variable
- Relative Humidity (%): Secondary predictor variable
- Temperature (°C): The variable we aim to predict
- Removed non-numeric columns (
Date/Time,Weather) - Selected the two most correlated features based on exploratory data analysis
- Split data into 80% training and 20% testing sets
The project includes comprehensive EDA with:
- 2D scatter plots analyzing individual feature relationships with temperature
- 3D visualizations exploring combined feature effects
- Correlation analysis to identify the strongest predictors
Key findings:
- Dew Point Temperature shows the strongest linear relationship with Temperature (R ≈ 1.0)
- Relative Humidity has a moderate negative correlation with Temperature
- Other features (Wind Speed, Visibility, Pressure) show weaker relationships
Input (2) → Linear(20) → ReLU → Linear(10) → ReLU → Linear(1) → Output
- Activation Function: ReLU
- Hidden Layers: 2 layers (20, 10 neurons)
- Optimizer: Stochastic Gradient Descent (SGD)
- Learning Rate: 0.0001
Input (2) → Linear(15) → Sigmoid → Linear(8) → Sigmoid → Linear(1) → Output
- Activation Function: Sigmoid
- Hidden Layers: 2 layers (15, 8 neurons)
- Optimizer: Adam
- Learning Rate: 0.001
Both models achieved exceptional performance:
| Metric | Model 1 (ReLU) | Model 2 (Sigmoid) |
|---|---|---|
| MAE | 0.1432 | 0.1432 |
| MSE | 0.0434 | 0.0434 |
| RMSE | 0.2082 | 0.2082 |
| R² | 0.9997 | 0.9997 |
pip install torch torchvision
pip install pandas numpy matplotlib
pip install scikit-learn- Clone the repository:
git clone https://github.com/yourusername/weather-temperature-regression.git
cd weather-temperature-regression-
Download the dataset:
- Place
Weather Data.csvin the project directory - Or download from the Kaggle link provided above
- Place
-
Run the Jupyter notebook:
Jupyter Notebook regression-with-pythrch.ipynbThe notebook is structured in the following sections:
- Data Loading and Exploration
- Exploratory Data Analysis with Visualizations
- Data Preprocessing and Feature Selection
- Model 1 Implementation (ReLU)
- Model 2 Implementation (Sigmoid)
- Performance Evaluation and Comparison
- Analysis and Recommendations
- Strong Linear Relationship: Dew point temperature is an excellent predictor of air temperature
- Model Performance: Both activation functions (ReLU and Sigmoid) performed equally well for this regression task
- Feature Engineering: Careful feature selection based on correlation analysis significantly improved model performance
- Generalization: High R² score (99.97%) indicates excellent model fit without apparent overfitting
- PyTorch Framework: Built using PyTorch's nn.Module
- Custom Model Classes: Implemented two distinct architectures
- Multiple Optimizers: Compared SGD vs Adam optimization
- Comprehensive Metrics: MAE, MSE, RMSE, and R² evaluation
- Epochs: 100,000 for both models
- Loss Function: Mean Squared Error (MSE)
- Data Split: 80/20 train-test split with random_state=42
Based on the analysis:
- Model Choice: Either model is suitable for this task, given identical performance
- Activation Function: ReLU might be preferred for scalability to larger networks
- Feature Selection: Dew point temperature alone could potentially achieve similar results
- Production Deployment: Consider the ReLU model for computational efficiency
This project was completed as part of:
- Course: ANLY-6500: Advanced Data Analytics
- Module: 3 - Regression Using PyTorch
Author: Ebube Ndubuisi
For questions or collaborations, please open an issue or submit a pull request.
This project is available under the MIT License. See LICENSE file for details.
This project demonstrates the application of deep learning techniques for weather prediction, showcasing the effectiveness of neural networks in capturing meteorological relationships.