kopia lustrzana https://github.com/animator/learn-python
Merge pull request #1105 from MrCodYrohit/main
Added Neural Network Regression ML Algorithm in Pythonpull/1110/head^2
commit
19a9ff6c7f
|
@ -21,3 +21,4 @@
|
|||
- [Transformers](transformers.md)
|
||||
- [K-Means](kmeans.md)
|
||||
- [K-nearest neighbor (KNN)](knn.md)
|
||||
- [Neural network regression](neural-network-regression.md)
|
||||
|
|
|
@ -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.
|
Ładowanie…
Reference in New Issue