emel/ml/decision_tree

Uses a decision tree to go from observations about a point (represented in the branches) to conclusions about the point’s discrete target value (represented in the leaves).

Functions

pub fn classifier(data: List(Map(String, String)), features: List(
    String,
  ), class: String) -> fn(Map(String, String)) -> String

Returns the function that classifies a point by using the ID3 Algorithm.

Data = [
  #{"collateral" => "none", "income" => "low", "debt" => "high", "credit_history" => "bad", "risk" => "high"},
  #{"collateral" => "none", "income" => "moderate", "debt" => "high", "credit_history" => "unknown", "risk" => "high"},
  #{"collateral" => "none", "income" => "moderate", "debt" => "low", "credit_history" => "unknown", "risk" => "moderate"},
  #{"collateral" => "none", "income" => "low", "debt" => "low", "credit_history" => "unknown", "risk" => "high"},
  #{"collateral" => "none", "income" => "high", "debt" => "low", "credit_history" => "unknown", "risk" => "low"},
  #{"collateral" => "adequate", "income" => "high", "debt" => "low", "credit_history" => "unknown", "risk" => "low"},
  #{"collateral" => "none", "income" => "low", "debt" => "low", "credit_history" => "bad", "risk" => "high"},
  #{"collateral" => "adequate", "income" => "high", "debt" => "low", "credit_history" => "bad", "risk" => "moderate"},
  #{"collateral" => "none", "income" => "high", "debt" => "low", "credit_history" => "good", "risk" => "low"},
  #{"collateral" => "adequate", "income" => "high", "debt" => "high", "credit_history" => "good", "risk" => "low"},
  #{"collateral" => "none", "income" => "low", "debt" => "high", "credit_history" => "good", "risk" => "high"},
  #{"collateral" => "none", "income" => "moderate", "debt" => "high", "credit_history" => "good", "risk" => "moderate"},
  #{"collateral" => "none", "income" => "high", "debt" => "high", "credit_history" => "good", "risk" => "low"},
  #{"collateral" => "none", "income" => "moderate", "debt" => "high", "credit_history" => "bad", "risk" => "high"}
],
Features = ["collateral", "income", "debt", "credit_history"],
Class = "risk",
F = emel@ml@decision_tree:classifier(Data, Features, Class),
F(#{"collateral" => "none", "income" => "high", "debt" => "low", "credit_history" => "good"}).
% "low"
pub fn decision_tree(data: List(Map(String, String)), features: List(
    String,
  ), class: String) -> List(Map(String, String))

The expanded decision tree as a list of maps. It gets created with the ID3 Algorithm for the provided data and the specified features.

Data = [
  #{"outlook" => "Sunny", "temperature" => "Hot", "humidity" => "High", "wind" => "Weak", "decision" => "No"},
  #{"outlook" => "Sunny", "temperature" => "Hot", "humidity" => "High", "wind" => "Strong", "decision" => "No"},
  #{"outlook" => "Overcast", "temperature" => "Hot", "humidity" => "High", "wind" => "Weak", "decision" => "Yes"},
  #{"outlook" => "Rain", "temperature" => "Mild", "humidity" => "High", "wind" => "Weak", "decision" => "Yes"},
  #{"outlook" => "Rain", "temperature" => "Cool", "humidity" => "Normal", "wind" => "Weak", "decision" => "Yes"},
  #{"outlook" => "Rain", "temperature" => "Cool", "humidity" => "Normal", "wind" => "Strong", "decision" => "No"},
  #{"outlook" => "Overcast", "temperature" => "Cool", "humidity" => "Normal", "wind" => "Strong", "decision" => "Yes"},
  #{"outlook" => "Sunny", "temperature" => "Mild", "humidity" => "High", "wind" => "Weak", "decision" => "No"},
  #{"outlook" => "Sunny", "temperature" => "Cool", "humidity" => "Normal", "wind" => "Weak", "decision" => "Yes"},
  #{"outlook" => "Rain", "temperature" => "Mild", "humidity" => "Normal", "wind" => "Weak", "decision" => "Yes"},
  #{"outlook" => "Sunny", "temperature" => "Mild", "humidity" => "Normal", "wind" => "Strong", "decision" => "Yes"},
  #{"outlook" => "Overcast", "temperature" => "Mild", "humidity" => "High", "wind" => "Strong", "decision" => "Yes"},
  #{"outlook" => "Overcast", "temperature" => "Hot", "humidity" => "Normal", "wind" => "Weak", "decision" => "Yes"},
  #{"outlook" => "Rain", "temperature" => "Mild", "humidity" => "High", "wind" => "Strong", "decision" => "No"}
],
Features = ["outlook", "temperature", "humidity", "wind"],
Class = "decision",
emel@ml@decision_tree:decision_tree(Data, Features, Class).
% [
%   #{"outlook" => "Overcast", "decision" => "Yes"},
%   #{"outlook" => "Rain", "wind" => "Strong", "decision" => "No"},
%   #{"outlook" => "Rain", "wind" => "Weak", "decision" => "Yes"},
%   #{"humidity" => "High", "outlook" => "Sunny", "decision" => "No"},
%   #{"humidity" => "Normal", "outlook" => "Sunny", "decision" => "Yes"}
% ]