{"id":15293695,"url":"https://github.com/rockfordwei/decisiontree","last_synced_at":"2025-06-23T20:05:23.048Z","repository":{"id":63920412,"uuid":"106428162","full_name":"RockfordWei/DecisionTree","owner":"RockfordWei","description":"A Perfect based solution for Decision Tree in Server Side Swift","archived":false,"fork":false,"pushed_at":"2017-10-13T02:59:49.000Z","size":36,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-05-20T06:46:57.052Z","etag":null,"topics":["algorithm","decision-trees","id3","id3-algorithm","machine-learning","perfect","swift4"],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/RockfordWei.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-10-10T14:27:20.000Z","updated_at":"2020-02-06T00:05:13.000Z","dependencies_parsed_at":"2023-01-14T14:00:49.287Z","dependency_job_id":null,"html_url":"https://github.com/RockfordWei/DecisionTree","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/RockfordWei/DecisionTree","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RockfordWei%2FDecisionTree","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RockfordWei%2FDecisionTree/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RockfordWei%2FDecisionTree/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RockfordWei%2FDecisionTree/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RockfordWei","download_url":"https://codeload.github.com/RockfordWei/DecisionTree/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RockfordWei%2FDecisionTree/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":261214477,"owners_count":23125804,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["algorithm","decision-trees","id3","id3-algorithm","machine-learning","perfect","swift4"],"created_at":"2024-09-30T16:50:38.617Z","updated_at":"2025-06-23T20:05:23.028Z","avatar_url":"https://github.com/RockfordWei.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Decision Tree in Server Side Swift\n\n\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"http://perfect.org/get-involved.html\" target=\"_blank\"\u003e\n        \u003cimg src=\"http://perfect.org/assets/github/perfect_github_2_0_0.jpg\" alt=\"Get Involved with Perfect!\" width=\"854\" /\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://github.com/PerfectlySoft/Perfect\" target=\"_blank\"\u003e\n        \u003cimg src=\"http://www.perfect.org/github/Perfect_GH_button_1_Star.jpg\" alt=\"Star Perfect On Github\" /\u003e\n    \u003c/a\u003e  \n    \u003ca href=\"http://stackoverflow.com/questions/tagged/perfect\" target=\"_blank\"\u003e\n        \u003cimg src=\"http://www.perfect.org/github/perfect_gh_button_2_SO.jpg\" alt=\"Stack Overflow\" /\u003e\n    \u003c/a\u003e  \n    \u003ca href=\"https://twitter.com/perfectlysoft\" target=\"_blank\"\u003e\n        \u003cimg src=\"http://www.perfect.org/github/Perfect_GH_button_3_twit.jpg\" alt=\"Follow Perfect on Twitter\" /\u003e\n    \u003c/a\u003e  \n    \u003ca href=\"http://perfect.ly\" target=\"_blank\"\u003e\n        \u003cimg src=\"http://www.perfect.org/github/Perfect_GH_button_4_slack.jpg\" alt=\"Join the Perfect Slack\" /\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://developer.apple.com/swift/\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/Swift-4.0-orange.svg?style=flat\" alt=\"Swift 4.0\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://developer.apple.com/swift/\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/Platforms-OS%20X%20%7C%20Linux%20-lightgray.svg?style=flat\" alt=\"Platforms OS X | Linux\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"http://perfect.org/licensing.html\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/License-Apache-lightgrey.svg?style=flat\" alt=\"License Apache\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"http://twitter.com/PerfectlySoft\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://img.shields.io/badge/Twitter-@PerfectlySoft-blue.svg?style=flat\" alt=\"PerfectlySoft Twitter\"\u003e\n    \u003c/a\u003e\n    \u003ca href=\"http://perfect.ly\" target=\"_blank\"\u003e\n        \u003cimg src=\"http://perfect.ly/badge.svg\" alt=\"Slack Status\"\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n\nThis is a Swift 4.0 version of Decision Tree data structure automation library according to the [wikipedia](https://zh.wikipedia.org/wiki/决策树)\n\n\nThe tree node has been abstracted into such an interface:\n\n``` swift\nclass DecisionTree {\n  public init(_ id: String, branches: [String: Any])\n  public func search(_ data:[String: String]) throws -\u003e String\n}\n```\n\nAll values in the objective data source must be discrete and converted into String.\n\n## Quick Start\n\nPackage.swift:\n\n``` swift\n.package(url: \"https://github.com/RockfordWei/DecisionTree.git\", from: \"0.3.0\")\n```\n\nPlease also **note** that it is necessary to modify the `Package.swift` file with explicit dependency declaration:\n\n```\ndependencies: [\"DecisionTree\"]\n```\n\nThen you can import the library:\n\n```\nimport DecisionTree\n```\n\n## Machine Learning\n\nCurrently there are two ways of tree building by scanning the data tables.\n\nAssuming that we expected to build a tree like:\n\n``` swift\nlet windy = DecisionTree(\"windy\", \n\tbranches: [\"true\": \"false\", \"false\": \"true\"])\n      \nlet humid = DecisionTree(\"humid\", \n\tbranches: [\"false\": \"true\", \"true\": \"false\"])\n      \nlet outlook = DecisionTree(\"outlook\", \n\tbranches: [\"sunny\":humid, \"overcast\": \"true\", \"rain\": windy])\n```\n\nWhich is coming from a data table as below, by ID3 entropy algorithm:\n\n``` swift\n  let discreteRecords: [[String: String]] = [\n    [\"outlook\": \"sunny\",    \"humid\": \"true\", \"windy\": \"false\", \"play\": \"false\"],\n    [\"outlook\": \"sunny\",    \"humid\": \"true\", \"windy\": \"true\",  \"play\": \"false\"],\n    [\"outlook\": \"overcast\", \"humid\": \"true\", \"windy\": \"false\", \"play\": \"true\" ],\n...\n    [\"outlook\": \"rain\",     \"humid\": \"true\", \"windy\": \"true\",  \"play\": \"false\"],\n  ]\n\n```\n\nBy applying such a tree, it is possible to make a prediction based on the history pattern:\n\n``` swift\n// if input a new record in form of [String:String]\nlet prediction = try tree.search(newRecord)\n\n// prediction is the result of the outcome,\n// for example, if the new record outlook is \"overcast\", \n// then the outcome prediction will be \"true\"\n```\nPerfect DecisionTree module provides two different solutions depending on type of the data source - in memory Array/Dictionary or a database connection.\n\n### In-Memory Toy\n\nYou can use `DTBuilderID3Memory` to create such a tree by a Swift Dictionary - Array:\n\n``` swift\nlet tree = try DTBuilderID3Memory.Build(\n\t\"play\", from: discreteRecords)\n```\n\nThis method is single threaded function which is aiming on educational purposes to help developers understand the textbook algorithm.\n\nPlease check the testing script for sample data.\n\n### Production Builder with MySQL\n\nThis 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.\n\n``` swift\nlet tree = try DTBuilderID3MySQL.Build(\n\t\"play\", from: mysqlConnection, tag: \"golf\")\n```\n\nIt 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.\n\nPlease check the testing script to understand how it works.\n\n## Further Information\nFor more information on the Perfect project, please visit [perfect.org](http://perfect.org).\n\n\n## Now WeChat Subscription is Available (Chinese)\n\u003cp align=center\u003e\u003cimg src=\"https://raw.githubusercontent.com/PerfectExamples/Perfect-Cloudinary-ImageUploader-Demo/master/qr.png\"\u003e\u003c/p\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frockfordwei%2Fdecisiontree","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frockfordwei%2Fdecisiontree","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frockfordwei%2Fdecisiontree/lists"}