Skip to main content

Tree-based machine learning for embedded system

Project description


Tree-based machine learning classifiers for microcontroller and embedded systems. Train in Python, then do inference on any device with support for C.

Want Naive Bayes instead? Go to embayes

Key features

Embedded-friendly Inference

  • Portable C99 code
  • No libc required
  • No dynamic allocations
  • Integer/fixed-point math only
  • Single header file include
  • Fast, sub-millisecond classification
  • Memory efficient. Can run with <100 bytes RAM

Convenient Training

  • API-compatible with scikit-learn
  • Implemented in Python 3
  • C classifier accessible in Python using pybind11

MIT licensed

Can be used as an open source alternative to MATLAB Classification Trees, Decision Trees using MATLAB Coder for C/C++ code generation. fitctree, fitcensemble, TreeBagger, ClassificationEnsemble, CompactTreeBagger


Minimally useful

  • Random Forests and ExtraTrees classifiers implemented
  • Tested running on AVR Atmega, ESP8266 and Linux.
  • On ESP8266, 8x8 digits classify in under 0.3ms with 95%+ accuracy
  • On Linux, is approx 2x faster than sklearn


Install from PyPI

pip install emtrees --user


  1. Train your model in Python
import emtrees
estimator = emtrees.RandomForest(n_estimators=10, max_depth=10), Y_train)
  1. Generate C code
code = estimator.output_c('sonar')
with open('sonar.h', 'w') as f:
  1. Use the C code
#include <emtrees.h>
#include "sonar.h"

const int32_t length = 60;
int32_t values[length] = { ... };
const int32_t predicted_class = sonar_predict(values, length):

For full example code, see examples/ and emtrees.ino



  • Standalone example application on microcontroller
  • Include emtrees.h inline in generated code


  • Support returning probabilities
  • Support serializing/deserializing trees


  • Support sklearn GradientBoostingClassifier
  • Support regression trees
  • Support weighted voting
  • Implement Isolation Forests (requires path/depths)


  • Support XGBoost learning of trees
  • Support LightGBM learning of trees
  • Support CatBoost learning of trees
  • Support/Implement a Very Fast Decision Tree (VFDT) learning algorithm
  • Implement multithreading when used in Python bindings, using OpenMP

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

emtrees-0.2.5.tar.gz (13.6 kB view hashes)

Uploaded source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page