A complete starter template for building machine learning projects in pure C++17.
Combines mlcore, dataframe, and matplotlib-cpp into a single ready-to-run example.
mltemplate is a modern C++17 machine learning template that integrates:
- mlcore β ML algorithms (Random Forest, SVM, KNN, etc.)
- dataframe β Pandas-like DataFrame in C++
- matplotlib-cpp β C++ wrapper for Pythonβs Matplotlib
It demonstrates an end-to-end ML pipeline in C++: load a dataset, preprocess features, train a model, visualize results, and export predictions to CSV. The repository includes a working Random Forest example on the Pima Indians Diabetes dataset.
- C++17 or newer
- Python with Matplotlib (required by
matplotlib-cpp)
Dependencies
# Clone repository
git clone --recursive https://github.com/overvac/mltemplate.git
cd mltemplate
# Build and run (MSVC / GCC / Clang)
# Windows (MSVC)
cl /std:c++17 /O2 main.cpp
# Linux / macOS
g++ -std=c++17 -O2 main.cpp -o app
./app#include "mltemplate/mltemplate.h"
int main()
{
c_dataframe df("diabetes.csv");
df.print();
std::vector<std::string> feature_column_names = {
"Pregnancies", "Glucose", "BloodPressure", "SkinThickness",
"Insulin", "BMI", "DiabetesPedigreeFunction", "Age"
};
auto features = n_mlcore::n_preprocessing::c_transform::get().transpose<double>(
df.at<double>(feature_column_names)
);
auto outcomes = df.at<int>("Outcome");
n_mlcore::n_supervised::c_rf forest(500);
forest.fit(features, outcomes);
auto fi = forest.feature_importances();
n_plt::bar(fi);
n_plt::show();
std::vector<int> predicted;
predicted.reserve(features.size());
for (const auto& f : features)
{
int p = forest.predict({ f })[0];
predicted.push_back(p);
}
df.add_column<int>("Predicted", predicted);
df.to_csv("solved_diabetes.csv");
return 0;
}- Prints dataset summary from diabetes.csv
- Trains a Random Forest classifier (mlcore)
- Plots feature importances (matplotlib-cpp)
- Saves predictions to solved_diabetes.csv
Released under the MIT License.