Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rockfordwei/decisiontree
A Perfect based solution for Decision Tree in Server Side Swift
https://github.com/rockfordwei/decisiontree
algorithm decision-trees id3 id3-algorithm machine-learning perfect swift4
Last synced: about 1 month ago
JSON representation
A Perfect based solution for Decision Tree in Server Side Swift
- Host: GitHub
- URL: https://github.com/rockfordwei/decisiontree
- Owner: RockfordWei
- License: apache-2.0
- Created: 2017-10-10T14:27:20.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2017-10-13T02:59:49.000Z (about 7 years ago)
- Last Synced: 2024-10-01T16:50:47.288Z (about 2 months ago)
- Topics: algorithm, decision-trees, id3, id3-algorithm, machine-learning, perfect, swift4
- Language: Swift
- Homepage:
- Size: 35.2 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# Decision Tree in Server Side Swift
This is a Swift 4.0 version of Decision Tree data structure automation library according to the [wikipedia](https://zh.wikipedia.org/wiki/决策树)
The tree node has been abstracted into such an interface:
``` swift
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.swift:
``` swift
.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:
``` swift
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:
``` swift
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:
``` swift
// 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:
``` swift
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.
``` swift
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](http://perfect.org).## Now WeChat Subscription is Available (Chinese)