Machine Learning with C++ – Polynomial regression with Eigen

Hello, this is my third article about how to use modern C++ for solving machine learning problems. This time I will show how to make a model for polynomial regression problem , with well known linear algebra library called Eigen.

Eigen was chosen because it is widely used and has a long history, it is highly optimized for CPUs, and it is a header only library. One of the famous project using it is TensorFlow. I found Eigen as the most useful library for linear algebra in C++. It has intuitive interfaces and implements modern C++ approaches, for example you can use std::move to eliminate matrix coping in some cases. Also it has great documentation with examples and search engine. It supports Intel® Math Kernel Library (MKL), which provides highly optimized multi-threaded mathematical routines for x86-compatible architectures. And it can be used in CUDA kernels, but this is still an experimental feature.



  1. Preparations
  2. Loading data to Eigen data-structures
  3. Standardization
  4. Generating additional polynomial components
  5. Batch gradient descent implementation
  6. Generating new data for testing model predictions
  7. Making predictions
  8. Plotting

It is the last article about linear algebra libraries for C++, I’m going to continue with articles about full featured ML frameworks for C++.

Continue reading the article and source code here