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

时间:2021-05-22

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

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

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

源代码如下:

from sklearn.feature_extraction import DictVectorizerimport csvfrom sklearn import treefrom sklearn import preprocessingfrom sklearn.externals.six import StringIOfrom xml.sax.handler import feature_external_gesfrom numpy.distutils.fcompiler import dummy_fortran_file# Read in the csv file and put features into list of dict and list of class labelallElectronicsData = 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)-1for 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=>ID3clf = tree.DecisionTreeClassifier(criterion='entropy')clf = clf.fit(dummX, dummY)print("clf:"+str(clf))#可视化treewith 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 = oneRowXnewRowX[0] = 1newRowX[2] = 0predictedY = clf.predict(newRowX)print("predictedY: "+ str(predictedY))

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

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

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章