0.0
No commit activity in last 3 years
No release in over 3 years
A decision tree library which implemented ID3 & C4.5 of algorithms
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies
 Project Readme

decision-tree

It's a library to implemente decision tree within ID3 & C4.5 of algorithms.

Installation

Add this line to your application's Gemfile:

gem 'decision-tree'

And then execute:

$ bundle

Or install it yourself as:

$ gem install decision-tree

Usage

A simple example:

require 'decision-tree.rb'
require 'json'

data = [
    {features: [0,"A",0,0,0.1], label: 'y'},
    {features: [0,"A",0,0,0.1], label: 'n'},
    {features: [0,"A",0,1,0.1], label: 'n'},
    {features: [1,"A",0,0,0.5], label: 'y'},
    {features: [2,"B",0,0,0.2], label: 'y'},
    {features: [2,"C",1,0,0.7], label: 'y'},
    {features: [2,"C",1,1,0.8], label: 'n'},
    {features: [1,"C",1,1,0.1], label: 'y'},

]


# Optional parameters
# algorithm: Which algorithm do you want to use? c45 or id3. Default is c45
algorithm = 'c45'

# Optional parameters
# columns: Specify name & type of your features.
#          Default names are feature1, feature2, feature3 & etc.
#          Default type is string if you don't specify.
#          If you assigned the type to num and specified the algorithm to c45,
#          the decision tree will process the feature as continuous numbers.
columns = ['col1','col2','col3','col4','col5:num']


root = DecisionTree.train(data, algorithm: algorithm, columns: columns)
root.to_pseudo_code.each{|line| puts line}
puts root.predict([0,"A",0,0,0.7])
puts root.predict([2,"C",1,1,0.5],"out of rules")
puts root.predict([3,"C",1,1,0.1],"out of rules")

Output below

if(col5 >= 0.8){
  return n
}
else{
  if(col1 == 0){
    if(col4 == 0){
      if(col2 == A){
        if(col3 == 0){
          return ["y", "n"]
        }
      }
    }
    if(col4 == 1){
      return n
    }
  }
  if(col1 == 1){
    return y
  }
  if(col1 == 2){
    return y
  }
}
{"y"=>0.5, "n"=>0.5}
{"y"=>1.0}
out of rules