python利用sklearn包编写决策树源代码

时间:2022-11-12 15:07:18

本文实例为大家分享了python编写决策树源代码,供大家参考,具体内容如下

因为最近实习的需要,所以用python里的sklearn包重新写了一次决策树。

工具:sklearn,将dot文件转化为pdf格式(是为了将形成的决策树可视化)graphviz-2.38,下载解压之后将其中的bin文件的目录添加进环境变量

源代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
from sklearn.feature_extraction import DictVectorizer
import csv
from sklearn import tree
from sklearn import preprocessing
from sklearn.externals.six import StringIO
from xml.sax.handler import feature_external_ges
from numpy.distutils.fcompiler import dummy_fortran_file
 
# Read in the csv file and put features into list of dict and list of class label
allElectronicsData = open(r'E:/DeepLearning/resources/AllElectronics.csv', 'rt')
reader = csv.reader(allElectronicsData)
headers = next(reader)
featureList = []
lableList = []
for row in reader:
lableList.append(row[len(row)-1])
rowDict = {}
#不包括len(row)-1
for i in range(1,len(row)-1):
rowDict[headers[i]] = row[i]
featureList.append(rowDict)
print(featureList)
 
vec = DictVectorizer()
dummX = vec.fit_transform(featureList).toarray()
print(str(dummX))
lb = preprocessing.LabelBinarizer()
dummY = lb.fit_transform(lableList)
print(str(dummY))
 
#entropy=>ID3
clf = tree.DecisionTreeClassifier(criterion='entropy')
clf = clf.fit(dummX, dummY)
print("clf:"+str(clf))
 
 
#可视化tree
with open("resultTree.dot",'w')as f:
f = tree.export_graphviz(clf, feature_names=vec.get_feature_names(),out_file = f)
 
 
#对于新的数据怎样来查看它的分类
oneRowX = dummX[0,:]
print("oneRowX: "+str(oneRowX))
newRowX = oneRowX
newRowX[0] = 1
newRowX[2] = 0
 
predictedY = clf.predict(newRowX)
print("predictedY: "+ str(predictedY))

这里的AllElectronics.csv,形式如下图所示:

python利用sklearn包编写决策树源代码

今天早上好不容易将jdk、eclipse以及pydev装进linux,但是,但是,但是,想装numpy的时候,总是报错,发现是没有gcc,然后又去装gcc,真是醉了,到现在gcc还是没有装成功,再想想方法

原文链接:https://www.cnblogs.com/yunerlalala/p/6240296.html