A machine learning project that predicts the State of Charge (SOC) of a lithium-ion battery using a Random Forest Regression model — achieving an R² of 1.00 and an RMSE of 0.08%.
Accurate SOC estimation is critical for electric vehicle battery management systems. This project builds a regression model that predicts battery SOC from real operating parameters — voltage, current, elapsed time, energy, and remaining capacity — and evaluates it against actual SOC calculated from remaining capacity.
| Metric | Value | Interpretation |
|---|---|---|
| RMSE | 0.08% | Average prediction error of 0.08 percentage points |
| R² | 1.00 | Model explains 100% of SOC variation |
The predicted SOC line sits almost perfectly on top of the actual SOC line across two full charge and discharge cycles — 260 samples total.
The chart shows two complete battery cycles (discharge → charge → discharge → charge). The Random Forest model tracks the actual SOC curve with near-perfect accuracy throughout both cycles.
Reference SOC is calculated from remaining battery capacity:
SOC(%) = (Remaining Capacity (Ah) / Nominal Capacity (Ah)) × 100
With a nominal capacity of 30 Ah:
SOC(%) = (Remaining Capacity (Ah) / 30) × 100
| Feature | Description |
|---|---|
| Voltage (V) | Terminal voltage of the battery |
| Current (A) | Charge/discharge current |
| Time (Sec) | Elapsed time |
| Energy (Wh) | Cumulative energy |
| Remaining Capacity (Ah) | Remaining charge in the battery |
Target variable: SOC (%)
Algorithm: Random Forest Regressor (scikit-learn)
model = RandomForestRegressor(
n_estimators=100, # 100 decision trees
random_state=42 # reproducible results
)Train/Test Split: 80% training, 20% testing
- Python — core language
- Pandas — data loading and manipulation
- NumPy — numerical operations
- Scikit-learn — Random Forest model, train/test split, RMSE, R²
- Matplotlib — actual vs predicted SOC visualisation
- OpenPyXL — reading Excel dataset and exporting predictions
SOC-Estimation/
│
├── RandomForest.py # Main script
├── battery_data.xlsx # Input dataset
├── SOC_predictions_sorted_9.xlsx # Output predictions
├── ActualSOC_vs_PredictedSOC.png # Result visualisation
└── README.md
Install dependencies:
pip install pandas numpy scikit-learn matplotlib openpyxlUpdate the dataset path in RandomForest.py:
df = pd.read_excel('battery_data.xlsx')Run the script:
python RandomForest.pyThe script will:
- Load and display the dataset
- Calculate reference SOC from remaining capacity
- Train the Random Forest model on 80% of data
- Predict SOC for the remaining 20%
- Print RMSE and R² scores
- Export predictions to Excel
- Display the actual vs predicted SOC chart
- Use only voltage, current, time, and energy as features (excluding remaining capacity) for a fully independent SOC estimator
- Test with multiple battery chemistries
- Add temperature as an input feature
- Compare with LSTM or other time-series models
Shrinithi Sellam VP
MSc Data Science — Hochschule Fulda, Germany
LinkedIn | GitHub
