EV Charging Logistic Regression
PyTorch logistic regression predicting whether to charge an EV based on battery level and electricity price.
Training script: logistic_regression.py
Data generator: generate_dataset.py
Dataset
Synthetic EV charging dataset (300 samples, linearly separable by design).
| Feature | Range | Description |
|---|---|---|
battery_percent |
0.0 β 100.0 | Battery state of charge (%) |
electricity_price |
0.050 β 0.500 | Price per kWh ($) |
charge_now |
{0, 1} | Target: 1 = charge now |
Class balance: ~40% positive (charge_now=1)
Decision boundary (generative):
score = -0.08 * battery_percent - 3.0 * electricity_price + N(0, 0.8)
charge_now = 1 if score > -4.0 else 0
- Lower battery β more likely to charge
- Lower price β more likely to charge
- Linear boundary (no interaction terms)
Training
| Setting | Value |
|---|---|
| Epochs | 200 |
| Batch size | 64 |
| Optimizer | SGD |
| Learning rate | 0.01 |
| Loss | BCEWithLogitsLoss |
| Train/val split | 80/20 (random) |
| Normalization | Z-score (fit on train) |
| Device | 5060ti |
Checkpointing: Best model by validation loss saved to logistic_best.pt
Results (Final Epoch 200)
| Metric | Value |
|---|---|
| Train Loss | 0.3025 |
| Val Loss | 0.2731 |
| Accuracy | 93.33% |
| Precision | 89.66% |
| Recall | 96.30% |
| F1 | 0.9286 |
Peak accuracy (epoch 102): 95.00% (F1: 0.9412)
- Validation loss continued decreasing after epoch 102 (0.3485 β 0.2731)
- Accuracy/F1 plateaued β model gained confidence without changing decisions
Files
| File | Description |
|---|---|
logistic_best.pt |
Best model weights + metadata (feature_cols, target_col, X_mean, X_std) |
logistic_latest.pt |
Final epoch weights |
logistic_regression.py |
Training script |
generate_dataset.py |
Data generator (linear boundary) |
figures/logistic_learning_curve.png |
Loss/accuracy curves |
figures/logistic_decision_boundary.png |
Decision boundary visualization |
Usage
import torch
from pathlib import Path
import sys
sys.path.insert(0, str(Path(__file__).parent.parent))
from _july_2.logistic_regression import LogisticRegressionModel
# Load checkpoint
ckpt = torch.load("logistic_best.pt", map_location="cpu")
# Recreate model
model = LogisticRegressionModel(len(ckpt["feature_cols"]))
model.load_state_dict(ckpt["model_state_dict"])
model.eval()
# Prepare input (2 features: battery_percent, electricity_price)
X_raw = torch.tensor([[20.0, 0.15], [80.0, 0.40]], dtype=torch.float32)
# Normalize using training stats
X_norm = (X_raw - ckpt["X_mean"]) / ckpt["X_std"]
# Predict
with torch.no_grad():
logits = model(X_norm)
probs = torch.sigmoid(logits)
preds = (probs >= 0.5).int()
print(f"Probabilities: {probs.squeeze().tolist()}")
print(f"Predictions: {preds.squeeze().tolist()}")
# [low battery, low price] -> prob] -> ~0.9 (charge)
# [high battery, high price] -> ~0.1 (don't charge)
Inference API (for HF Spaces)
The checkpoint contains everything needed:
ckpt = torch.load("logistic_best.pt")
feature_cols = ckpt["feature_cols"] # ["battery_percent", "electricity_price"]
target_col = ckpt["target_col"] # "charge_now"
X_mean, X_std = ckpt["X_mean"], ckpt["X_std"]
Limitations
- Synthetic data β not trained on real EV charging behavior
- Linear boundary β cannot capture price sensitivity that varies with battery level
- Small dataset β 300 samples, 240 train / 60 test
- No temporal/contextual features β time of day, trip distance, user preference ignored
Citation
@misc{ev-charging-logreg-2026,
title={EV Charging Logistic Regression},
author={marmossburg},
year={2026},
url={https://huggingface.co/...}
}
Inference Providers NEW
This model isn't deployed by any Inference Provider. π Ask for provider support