Swift Package Index

A Perfect based solution for Decision Tree in Server Side Swift

  • The latest stable release is 0.4.0. Released 3 years ago.
  • The last commit to master was 3 years ago.

Swift Version Compatibility

  • 0.4.0 and master
Full build results

Platform Compatibility

  • 0.4.0 and master
Full build results

Decision Tree in Server Side Swift

This is a Swift 4.0 version of Decision Tree data structure automation library according to the wikipedia

The tree node has been abstracted into such an interface:

class DecisionTree {
  public init(_ id: String, branches: [String: Any])
  public func search(_ data:[String: String]) throws -> String

All values in the objective data source must be discrete and converted into String.

Quick Start


.package(url: "https://github.com/RockfordWei/DecisionTree.git", from: "0.3.0")

Please also note that it is necessary to modify the Package.swift file with explicit dependency declaration:

dependencies: ["DecisionTree"]

Then you can import the library:

import DecisionTree

Machine Learning

Currently there are two ways of tree building by scanning the data tables.

Assuming that we expected to build a tree like:

let windy = DecisionTree("windy", 
	branches: ["true": "false", "false": "true"])
let humid = DecisionTree("humid", 
	branches: ["false": "true", "true": "false"])
let outlook = DecisionTree("outlook", 
	branches: ["sunny":humid, "overcast": "true", "rain": windy])

Which is coming from a data table as below, by ID3 entropy algorithm:

  let discreteRecords: [[String: String]] = [
    ["outlook": "sunny",    "humid": "true", "windy": "false", "play": "false"],
    ["outlook": "sunny",    "humid": "true", "windy": "true",  "play": "false"],
    ["outlook": "overcast", "humid": "true", "windy": "false", "play": "true" ],
    ["outlook": "rain",     "humid": "true", "windy": "true",  "play": "false"],

By applying such a tree, it is possible to make a prediction based on the history pattern:

// if input a new record in form of [String:String]
let prediction = try tree.search(newRecord)

// prediction is the result of the outcome,
// for example, if the new record outlook is "overcast", 
// then the outcome prediction will be "true"

Perfect DecisionTree module provides two different solutions depending on type of the data source - in memory Array/Dictionary or a database connection.

In-Memory Toy

You can use DTBuilderID3Memory to create such a tree by a Swift Dictionary - Array:

let tree = try DTBuilderID3Memory.Build(
	"play", from: discreteRecords)

This method is single threaded function which is aiming on educational purposes to help developers understand the textbook algorithm.

Please check the testing script for sample data.

Production Builder with MySQL

This library also provides a powerful builder powered by mysql, which can scan the whole table in an amazing speed and get the job done - assuming the above data has been transferred to a golf table stored in the database.

let tree = try DTBuilderID3MySQL.Build(
	"play", from: mysqlConnection, tag: "golf")

It will split the table into views recursively without moving or writing any data, in a threading queue. The major cost is the memory of stacks for deep walking with nothing else.

Please check the testing script to understand how it works.

Further Information

For more information on the Perfect project, please visit perfect.org.

Now WeChat Subscription is Available (Chinese)