监督学习算法第三种——决策树decision trees
决策树可以通过核技巧把简单的线性决策面转换为非线性决策面
百度百科:决策树是一个预测模型;他代表的是对象属性与对象值之间的一种映射关系。树中每个节点表示某个对象,而每个分叉路径则代表的某个可能的属性值,而每个叶结点则对应从根节点到该叶节点所经历的路径所表示的对象的值
通过坐标数据进行多次分割,找出分界线,绘制决策树。在机器学习中,决策树学习算法就是根据数据,使用计算机算法自动找出决策边界。
-----------
决策树的python代码(sklearn)
链接:http://scikit-learn.org/stable/modules/tree.html>>> from sklearn import tree
>>> X = [[0, 0], [1, 1]]
>>> Y = [0, 1]
>>> clf = tree.DecisionTreeClassifier()
>>> clf = clf.fit(X, Y)
代码练习:
class_vis.py与prep_terrain_data.py代码与朴素贝叶斯代码相同
studentMain.py
#!/usr/bin/python
""" lecture and example code for decision tree unit """
import sys
from class_vis import prettyPicture, output_image
from prep_terrain_data import makeTerrainData
import matplotlib.pyplot as plt
import numpy as np
import pylab as pl
from classifyDT import classify
features_train, labels_train, features_test, labels_test = makeTerrainData()
### the classify() function in classifyDT is where the magic
### happens--fill in this function in the file 'classifyDT.py'!
clf = classify(features_train, labels_train)
#### grader code, do not modify below this line
prettyPictu