Examples¶
Interactive Notebooks¶
For hands-on, fully-executed examples with rendered outputs, see the Notebooks section:
| Notebook | Description |
|---|---|
| Quickstart | Convert a model to C in 5 minutes |
| Classification | DT / RF / SVM / MLP comparison |
| Regression IoT | Fixed-point, depth trade-offs |
| Feature Analysis | Sensor reduction, BOM impact |
| Multi-Format Export | C, C++, Arduino, MicroPython |
| End-to-End IoT (ADL) | Full pipeline on a real gas-sensor dataset |
| Advanced Optimization (v0.2) | Quine-McCluskey, BDD, and 'auto' compared on Iris+RandomForest |
Script Examples¶
All script examples are in the examples/ directory.
iris_example.py¶
Full end-to-end demonstration using the Iris dataset:
- Trains Decision Tree, Random Forest, SVM, and Neural Network
- Converts each to C with different configurations
- Compares fidelity, code size, and complexity
- Shows feature analysis workflow
Code Recipes¶
Advanced rule optimization (v0.2)¶
For classification models, the 'auto' level evaluates Quine-McCluskey, BDD, and the
no-op baseline, and returns the variant with the smallest estimated FLASH footprint:
from blackbox2c import convert, ConversionConfig
config = ConversionConfig(
max_depth=5,
optimize_rules='auto', # try qm, bdd, no-op; pick smallest FLASH
qm_max_literals=12, # cap before QM falls back to identity
bdd_max_literals=20, # cap before BDD falls back to identity
)
c_code = convert(model, X_train, config=config)
Typical savings on Iris+RandomForest: −47 % FLASH vs 'medium'. See
benchmarks/results/v0.2.md
and the Advanced Optimization notebook.
Note
Advanced levels ('qm', 'bdd', 'auto') are classification-only.
On regression tasks they emit a single UserWarning and fall back to 'high'.
Fixed-point for AVR (Arduino Uno)¶
Arduino Uno's ATmega328P has no FPU. Use 16-bit fixed-point:
from blackbox2c import convert, ConversionConfig
config = ConversionConfig(
max_depth=4,
use_fixed_point=True,
precision=16,
optimize_rules='high',
memory_budget_kb=1.0,
)
arduino_code = convert(
model, X_train,
target='arduino',
config=config,
)
Minimal footprint for ATtiny¶
ESP32 / Raspberry Pi Pico (MicroPython)¶
mp_code = convert(model, X_train, target='micropython', max_depth=6)
# Save and flash
with open('predictor.py', 'w') as f:
f.write(mp_code)
On device:
Feature selection for sensor-constrained systems¶
When you have many features but limited sensors:
from blackbox2c.analysis import FeatureSensitivityAnalyzer
from blackbox2c import convert
# Analyze
analyzer = FeatureSensitivityAnalyzer(n_repeats=20)
results = analyzer.analyze(model, X_train, y_train, feature_names=names)
# Get top 4 features
top_indices = [idx for idx, _, _ in results.get_top_features(4)]
X_reduced = X_train[:, top_indices]
reduced_names = [names[i] for i in top_indices]
# Retrain on reduced features
model_reduced = RandomForestClassifier(n_estimators=50, random_state=42)
model_reduced.fit(X_reduced, y_train)
# Convert
code = convert(model_reduced, X_reduced, feature_names=reduced_names)