Skip to content

About

Machine learning-based battery State of Charge estimation using charge–discharge data, Coulomb counting, and Random Forest regression in Python.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

🔋 Battery State of Charge Prediction using Random Forest

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%.


📌 Project Overview

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.


🏆 Results

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.


📊 Actual vs Predicted SOC

Actual vs Predicted SOC

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.


🔧 SOC Calculation

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

📥 Input Features

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 (%)


🤖 Model Configuration

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


🛠️ Tools & Libraries

  • 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

📂 Project Structure

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

🚀 How to Run

Install dependencies:

pip install pandas numpy scikit-learn matplotlib openpyxl

Update the dataset path in RandomForest.py:

df = pd.read_excel('battery_data.xlsx')

Run the script:

python RandomForest.py

The script will:

  1. Load and display the dataset
  2. Calculate reference SOC from remaining capacity
  3. Train the Random Forest model on 80% of data
  4. Predict SOC for the remaining 20%
  5. Print RMSE and R² scores
  6. Export predictions to Excel
  7. Display the actual vs predicted SOC chart

💡 Potential Improvements

  • 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

👩‍💻 Author

Shrinithi Sellam VP
MSc Data Science — Hochschule Fulda, Germany
LinkedIn | GitHub

About

Machine learning-based battery State of Charge estimation using charge–discharge data, Coulomb counting, and Random Forest regression in Python.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages