maoruibin / maoruibin/maoruibin.github.com

决策树

Open
#71 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

机器学习 笔记
Dominant language
HTML
Stars
22
Forks
4
PR merge metrics
No merged PRs in 30d

Description

决策树算法是机器学习算法中的一种分类算法,sklearn 包提供了相应的实现,接着用决策树算法来完成一个小练习。

练习目的

  • 使用 sklearn 包
  • 训练自己的模型来进行简单的识别

练习场景

现在需要编写一个模型用来判断一个水果是苹果还是橘子

训练数据

要得到模型,就必须要有训练数据也称作训练样本,然后利用训练数据的特征和标签进行训练,

样本数据集

为了简单,这里只关心水果的两个特征:重量(g)、光滑度,
序号 | 重量 | 光滑度 | 标签
-| :-: | :-: | :-: |-:
1|140g|光滑|苹果
2|130g|光滑|苹果
3|160g|粗糙|橘子
4|155g|粗糙|橘子

以上便是一个样本集,这个集合里有四条训练样本,样本包含两个要素

  • 特征
    就是样本的属性,比如水果有重量、光滑度、味道等等属性,并且每一个属性都有确定的属性值
  • 标签
    不同的特征最终构成了一个确定的结果,这个结果就是这里的标签值,比如上面数据集中第一个样本中重量为 140 g 表面光滑的水果 对应的标签就是 苹果。

其实,这个例子中关于标签的理解有点抽象,英文中特征是 'feature',标签是 'label'
针对于特定的样本,其实这里的样本的标签理解为结果可能更好理解。

在机器学习的世界里,所有的现实世界中的东西都是数据,要被训练的数据集合中每一个数据样本会有很多特征值,但是光有特征不行,在监督学习的范畴内,特征需要跟一个结果确定起来,这个样本才是一个完整的样本,这里的结果,也可以理解为根据样本属性值打标签。当然生活中标签的概念都是一种被动的行为,是后期人们打上去的,而这里是样本本来就有的东西。 Label

具体训练以及预测

有了样本数据集,就可以开始训练了,这里可以自己编写一个分类器,然后使用训练数据进行训练,然后最终得出一个模型,接着用这个模型进行数据预测。

而 sklearn 作为一个成熟的数据处理包,他已经实现了机器学习中大多数算法,其中就有分类算法决策树的实现 - DecisionTreeClassifier,使用时只需要导入对应的包即可。
训练好了模型,预测未知的数据就很简单了,代码如下

from sklearn import tree
# [重量,光滑度(1 光滑 0 粗糙]
# 样本数据  训练数据
features = [[140,1],[130,1],[150,0],[160,0]]
# 样本对应的标签
labels = ['apple','apple','orange','orange']

# 决策树分类
# 创建分类器
clf = tree.DecisionTreeClassifier()
# 使用样本进行训练
clf = clf.fit(features,labels)

# 利用已有模型预测数据
result = clf.predict([[180,1]])
print result

result is

['orange']

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue body is a Chinese tutorial about decision trees using Python and scikit-learn, including a DecisionTreeClassifier example. No file, test, entry point, or concrete change is named; first confirm where this content belongs and what documentation outcome is expected before starting.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, scikit-learn
Domain
documentation, machine-learning
Issue type
Documentation
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.