Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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

Awesome Lists containing this project

README

        

# Decision Tree in Server Side Swift



Get Involved with Perfect!



Star Perfect On Github


Stack Overflow


Follow Perfect on Twitter


Join the Perfect Slack



Swift 4.0


Platforms OS X | Linux


License Apache


PerfectlySoft Twitter


Slack Status

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)