No1:python+机器学习(决策树)

来源:互联网 发布:java 队列 线程安全 编辑:程序博客网 时间:2024/05/16 15:18

1.YouTube相关视频介绍

微软的即使翻译

谷歌的无人驾驶等

2.应用领域

无人驾驶

Google Now中语音识别

百度识图

针对图片,自动生成文字的描述等

3.两个目标

分类 回归

4.步骤

有监督学习:训练集有类别标记

无监督学习:无类别标记

半监督学习:有类别标记的训练集+无标记的训练集

机器学习中分类和预测算法的评估

准确性 速度 强壮性 可规模性 可解释性

一.把数据拆分为训练集和测试集

二.用训练集和训练集的特征向量来训练算法

三.用学习的算法运用在测试集上来评估算法

5. 决策树(decision tree)

   决策树是一个类似于流程图的树结构:其中,每个内部节点表示在一个属性上的测试,每个分支代表一个属性输出,二每个树叶节点代表类或类分布.树的最顶层是数的根节点

 信息是抽象的如何度量

我们一无所知的事情,需要大量信息 ==> 信息的度量就等于不确定的多少

通常以比特来衡量信息的多少-(p1*log p1+p2*log p2+...+pn*log pn) 变量的不确定性越大熵(  信息熵) 也就越大

RID

Age

Income

Student

Credit_rating

Buy_computer

1

youth

high

No

Fair

No

2

youth

high

No

Excellent

No

3

Middle_age

high

No

fair

yes

4

senior

medium

No

fair

yes

5

senior

Low

Yes

fair

yes

6

senior

Low

Yes

Excellent

no

7

Middle_age

low

yes

Excellent

yes

8

youth

medium

no

fair

no

9

youth

low

yes

fair

yes

10

senior

medium

yes

fair

yes

11

youth

medium

yes

Excellent

yes

12

Middle_age

medium

no

Excellent

yes

13

Middle_age

high

yes

fair

yes

14

senior

medium

no

Excellent

no


根节点的选择

于结果而言买电脑的人数为9人,选择不买的为5人

info(D)=-9/14log₂(9/14)--5/14log₂(9514)=0.940bit

于年龄对其影响,分类中年轻的5人(不买电脑3人,买电脑2人),中年4人(买电脑的4人,不买电脑的0人),老年5人(买电脑3人,不买电脑2人)

 info(D)age=5/14*(-2/5*log₂-3/5*log₂*3/5)+4/14*(-4/4*log₂4/4 - 0/4log₂0/4 )+5/14( -3/5log₂3/5 - 2/5log₂2/5)=0.694.bit

以年级区分的获取量 Gain(age) =Info(D) -Info(D)age =0.940 -0.694 =0.246 bit

类似的  Gain(income) =0.029 ,  Gain(student) =0.151,   Gain(credit_rating) =0.048

所以选择age作为第一个根节点

同样的,根据上面的方法获取下一个节点

其他算法:C4.5 ,CART,ID

共同点:都是贪心算法,自上而下

区别:属性选择度量方法不同 C4.5(gain ratio) CART(gini index),ID3(Information Cain)

决策树优点: 直观,便于理解,小规模有效

决策树缺点: 处理连续变量不好,类别较多,错误增加比较快,可规模性一般

6. 在python实现

eclipse下安装pyDev 

下载Anaconda 

https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/

https://www.anaconda.com/download/

数据预处理

需要将上述转化为类似于

youth middle_age senor high medium low yes no fair excellent buy

1         0 0    10     0    0    1   1   00

#encoding=utf-8'''Created on 2017/12/4@author: wangpx'''import csvfrom sklearn import treefrom sklearn import preprocessingfrom builtins import range, strfrom sklearn.feature_extraction.dict_vectorizer import DictVectorizerfrom _nsis import outallData = open(r'D:\pyDev\demo\DecisionTree.csv','rt',encoding= 'utf-8')reader = csv.reader(allData)headers = next(reader)#print(headers)featureList=[]  #特征集labelList=[]  #结果集 for row in reader:    labelList.append(row[len(row) - 1])    rowDic={}    for i in range(1,len(row) -2):        rowDic[headers[i]] =  row[i]    featureList.append(rowDic)#print(featureList)'''    将特征值进行预处理'''vec=DictVectorizer()dummyX = vec.fit_transform(featureList).toarray()#print(str(dummyX))''' 将结果集进行预处理'''lb = preprocessing.LabelBinarizer()dummyY=lb.fit_transform(labelList)#print(str(dummyY))'''    指定决策树算法Id3'''clf=tree.DecisionTreeClassifier(criterion='entropy')clf=clf.fit(dummyX, dummyY)#print(str(clf))'''生成文件'''with open('D:\pyDev\demo\DiscisionTree.dot','w') as f:    f=tree.export_graphviz(clf,feature_names=vec.get_feature_names(),out_file = f)oneRow =dummyX[0]#print(oneRow)newRow=oneRownewRow[0]=1newRow[2]=0predictedY=clf.predict(newRow)print(predictedY)

一个工具graphviz可以将生成dot文件转化为pdf文件,配置环境变量


 cmd执行   dot -Tpdf  初始化文件 生成文件