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.
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:
All values in the objective data source must be discrete and converted into String.
Quick Start
Package.swift:
Please also note that it is necessary to modify the
Package.swift
file with explicit dependency declaration:Then you can import the library:
Machine Learning
Currently there are two ways of tree building by scanning the data tables.
Assuming that we expected to build a tree like:
Which is coming from a data table as below, by ID3 entropy algorithm:
By applying such a tree, it is possible to make a prediction based on the history pattern:
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: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.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)