GithubHelp home page GithubHelp logo

decisiontree's Introduction

Decision Trees

Template Library for Decision Tree Algorithms

(Examples will be provided at each stage of the implementation of certain algorithms)

Cmake Building

$ mkdir build && cd build
$ cmake ..
$ cmake --build build/

Examples

1. Decision Tree Classifier

1.1. Binary classification

Examples/binary_classification_tree.cpp
#include "../src/tree/DecisionTreeClassifier.h"
#include "../src/_utils.h"
#include <iostream>
#include <string>

// the datatype of your dataset
using datatype = int;

int main(){
    /*
    * Sex: {0, 1}
    * Pclass: {1, 2, 3}
    * Embarked: {0, 1, 2}
    * Survived (binary target): {0, 1}
    */
    std::string file_path{"./titanic.csv"};
    algo::util::DataFrame dataframe{file_path, ',', true};
    auto df = dataframe.df<datatype>();
    algo::DecisionTreeClassifier<datatype> cls{5, 0};
    cls.fit(df);
    cls.PrintTree();
    std::cout << "##### Inference #######" << std::endl;
    std::vector<std::vector<datatype>> new_observations{
        {1, 3, 1}, // 0
        {0, 1, 2}, // 1
        {1, 0, 2}, // 0
        {1, 1, 1}  // 0
    };
    auto pred = cls.predict(new_observations);
    for (auto &c: pred)
        std::cout << c << std::endl;    
    return 0;
 };
Examples/run_binary_classification_tree.sh
#!/bin/sh
g++ -Wall -o binary_class_exec binary_classification_tree.cpp -L../build -Wl,-rpath=../build -lAlgo

1.2. Multiclass classification

Examples/multiclass_classification_tree.cpp
#include "../src/tree/DecisionTreeClassifier.h"
#include "../src/_utils.h"
#include <iostream>
#include <string>

// the datatype of your dataset
using datatype = double;

int main(){
    /*
    * 1. sepal length in cm
    * 2. sepal width in cm
    * 3. petal length in cm
    * 4. petal width in cm
    * 5. species(multiclass target):
    -- Iris Setosa: 0
    -- Iris Versicolor: 1
    -- Iris Virginica: 2
    */
    std::string file_path{"./iris.csv"};
    algo::util::DataFrame dataframe{file_path, ',', true};
    auto df = dataframe.df<datatype>();
    algo::DecisionTreeClassifier<datatype> cls{5, 0};
    cls.fit(df);
    cls.PrintTree();
    std::cout << "##### Inference #######" << std::endl;
    std::vector<std::vector<datatype>> new_observations{
        {5.5,2.4,3.8,1.1},//1
        {4.9,2.5,4.5,1.7},//2
        {5.5,4.2,1.4,0.2},//0
        {6.4,3.2,4.5,1.5} //1
    };
    auto pred = cls.predict(new_observations);
    for (auto &c: pred)
        std::cout << c << std::endl;    
    return 0;
 };
Examples/run_multiclass_classification_tree.sh
#!/bin/sh
g++ -Wall -o multi_class_exec multiclass_classification_tree.cpp -L../build -Wl,-rpath=../build -lAlgo

TODO:

  • Cmake: switch from SHARED library to an INTERFACE library (header-only)
  • Cmake: include installation of the library

decisiontree's People

Contributors

ayoyu avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.