前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Github 项目推荐 | 基于 ID3 算法的 ML 决策树的实现

Github 项目推荐 | 基于 ID3 算法的 ML 决策树的实现

作者头像
AI研习社
发布2018-07-26 14:04:33
7630
发布2018-07-26 14:04:33
举报
文章被收录于专栏:AI研习社AI研习社

本库是实现用于决策树学习的 ID3 算法的 Ruby 库,目前能够学习连续和离散的数据集。

Github 链接:

https://github.com/igrigorik/decisiontree

特点

  • 用于连续和离散情况的 ID3 算法,支持不一致的数据集。
  • Graphviz 组件可视化学习树
  • 支持多个符号输出和连续树形图。
  • 当没有分支适合输入时返回默认值

实现

Ruleset 是一个用 2/3 训练数据训练 ID3Tree 的类,并将其转换为一组规则,然后用剩下的 1/3 数据(以 C4.5 的方式,https://en.wikipedia.org/wiki/C4.5_algorithm)修剪规则。

Bagging 是一个基于 Bagging 的训练器,它可以训练 10 个 Ruleset 训练器,并通过投票预测最佳的输出结果。

详细信息请访问以下链接:

https://www.igvita.com/2007/04/16/decision-tree-learning-in-ruby/

示例

代码语言:javascript
复制
require 'decisiontree'

attributes = ['Temperature']
training = [
[36.6, 'healthy'],
[37, 'sick'],
[38, 'sick'],
[36.7, 'healthy'],
[40, 'sick'],
[50, 'really sick'],
]

# Instantiate the tree, and train it based on the data (set default to '1')
dec_tree = DecisionTree::ID3Tree.new(attributes, training, 'sick', :continuous)
dec_tree.train

test = [37, 'sick']
decision = dec_tree.predict(test)
puts "Predicted: #{decision} ... True decision: #{test.last}"

# => Predicted: sick ... True decision: sick

# Specify type ("discrete" or "continuous") in the training data
labels = ["hunger", "color"]
training = [
      [8, "red", "angry"],
      [6, "red", "angry"],
      [7, "red", "angry"],
      [7, "blue", "not angry"],
      [2, "red", "not angry"],
      [3, "blue", "not angry"],
      [2, "blue", "not angry"],
      [1, "red", "not angry"]
]

dec_tree = DecisionTree::ID3Tree.new(labels, training, "not angry", color: :discrete, hunger: :continuous)
dec_tree.train

test = [7, "red", "angry"]
decision = dec_tree.predict(test)
puts "Predicted: #{decision} ... True decision: #{test.last}"

# => Predicted: angry ... True decision: angry
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-05-03,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 AI研习社 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 特点
  • 实现
  • 示例
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档