Merge pull request #1105 from MrCodYrohit/main

Added Neural Network Regression ML Algorithm in Python
pull/1110/head^2
Ashita Prasad 2024-06-22 15:58:50 +05:30 zatwierdzone przez GitHub
commit 19a9ff6c7f
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: B5690EEEBB952194
2 zmienionych plików z 85 dodań i 0 usunięć

Wyświetl plik

@ -21,3 +21,4 @@
- [Transformers](transformers.md)
- [K-Means](kmeans.md)
- [K-nearest neighbor (KNN)](knn.md)
- [Neural network regression](neural-network-regression.md)

Wyświetl plik

@ -0,0 +1,84 @@
# Neural Network Regression in Python using Scikit-learn
## Overview
Neural Network Regression is used to predict continuous values based on input features. Scikit-learn provides an easy-to-use interface for implementing neural network models, specifically through the `MLPRegressor` class, which stands for Multi-Layer Perceptron Regressor.
## When to Use Neural Network Regression
### Suitable Scenarios
1. **Complex Relationships**: Ideal when the relationship between features and the target variable is complex and non-linear.
2. **Sufficient Data**: Works well with large datasets that can support training deep learning models.
3. **Feature Extraction**: Useful in cases where the neural network's feature extraction capabilities can be leveraged, such as with image or text data.
### Unsuitable Scenarios
1. **Small Datasets**: Less effective with small datasets due to overfitting and inability to learn complex patterns.
2. **Low-latency Predictions**: Might not be suitable for real-time applications with strict latency requirements.
3. **Interpretability**: Not ideal when model interpretability is crucial, as neural networks are often seen as "black-box" models.
## Implementing Neural Network Regression in Python with Scikit-learn
### Step-by-Step Implementation
1. **Import Libraries**
```python
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neural_network import MLPRegressor
from sklearn.metrics import mean_absolute_error
```
2. **Load and Prepare Data**
For illustration, let's use a synthetic dataset.
```python
# Generate synthetic data
np.random.seed(42)
X = np.random.rand(1000, 3)
y = X[:, 0] * 3 + X[:, 1] * -2 + X[:, 2] * 0.5 + np.random.randn(1000) * 0.1
# Split the data
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# Standardize the data
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
```
3. **Build and Train the Neural Network Model**
```python
# Create the MLPRegressor model
mlp = MLPRegressor(hidden_layer_sizes=(64, 64), activation='relu', solver='adam', max_iter=500, random_state=42)
# Train the model
mlp.fit(X_train, y_train)
```
4. **Evaluate the Model**
```python
# Make predictions
y_pred = mlp.predict(X_test)
# Calculate the Mean Absolute Error
mae = mean_absolute_error(y_test, y_pred)
print(f"Test Mean Absolute Error: {mae}")
```
### Explanation
- **Data Generation and Preparation**: Synthetic data is created, split into training and test sets, and standardized to improve the efficiency of the neural network training process.
- **Model Construction and Training**: An `MLPRegressor` is created with two hidden layers, each containing 64 neurons and ReLU activation functions. The model is trained using the Adam optimizer for a maximum of 500 iterations.
- **Evaluation**: The model's performance is evaluated on the test set using Mean Absolute Error (MAE) as the performance metric.
## Conclusion
Neural Network Regression with Scikit-learn's `MLPRegressor` is a powerful method for predicting continuous values in complex, non-linear scenarios. However, it's essential to ensure that you have enough data to train the model effectively and consider the computational resources required. Simpler models may be more appropriate for small datasets or when model interpretability is necessary. By following the steps outlined, you can build, train, and evaluate a neural network for regression tasks in Python using Scikit-learn.