Skip to content

Some machine learning models implemented from scratch in C

License

Notifications You must be signed in to change notification settings

hazyuun/machine-learning-models

Repository files navigation

Machine Learning Models

Some ML models for classification and regression implemented from scratch in C.

C/C++ CI

Plots

Classification

PLA POCKET ADALINE

Regression

Simple linear regression (Least squares with gradient decent) Evolution of loss

Getting started

Dependencies (Optionnal)

  • gnuplot (optionnal) This is just for the plot.sh script, you can compile and use the library without this
  • rainy-csv (optionnal) This is my library, I used it just for the tests, so you can skip it too, it is used as git submodule so it will be automatically downloaded when you clone with --recurse-submodules

Cloning

If you are planning to run the tests, please clone with

$ git clone --recurse-submodules https://github.com/hazyuun/machine-learning-models

to automatically get rainy-csv.
If you won't run the tests, then clone as usual :

$ git clone https://github.com/hazyuun/machine-learning-models

Compiling and using

Compiling

Just use the Makefile

Compile everthing

$ make USE_RAINY_CSV=1 all

Output will be in lib for the library, and in bin for the tests, note that USE_RAINY_CSV macro needs to be defined to compile the tests.

Only compile the library

if you want to compile the library without rainy-csv support, run :

$ make lib

otherwise, run

$ make USE_RAINY_CSV=1 lib

Output will be in lib directory

Compile the test (supposing you already compiled the library

$ make test

Output will be in bin directory

Usage example

#include <helpers/model.h>
#include <helpers/csv2dataset.h>
#include <utils/export.h>

int main(int argc, char **argv) {
  (void)argc;
  (void)argv;

   /* Load data */
  labeled_dataset_t *data;
  data = csv_to_labeled_dataset("./test-data/noisy01.csv");

  /* Make and train the model */
  single_layer_model_t *model;
  model = make_single_layer_model(LOG_REG_MODEL, 2, &sigmoid);
  history_t *history = model->train(model, data);

  /* Generate the output files for plotting */
  gp_export_labeled_dataset(data, "data.in");
  gp_export_single_layer_model(model, history);
}

Running the tests

First of all, after compiling, run the test of your choice, eg.

$ ./bin/rg_simple_linreg_test

Some output files will be generated, those files are used by the gnuplot scripts to show the result

Run the plot.sh bash script to see the plots

  • Reminder : you will need gnuplot for that

For the classification tests run :

$ ./plot.sh

For the linear regression ones run :

$ ./plot.sh --lin-reg

Notes about the tests

  • Test files prefixed with cl_ are for classification, and ones prefixed with rg_ are for regression.