python利用sklearn包编写决策树源代码
本文实例为大家分享了python编写决策树源代码,供大家参考,具体内容如下
因为最近实习的需要,所以用python里的sklearn包重新写了一次决策树。
工具:sklearn,将dot文件转化为pdf格式(是为了将形成的决策树可视化)graphviz-2.38,下载解压之后将其中的bin文件的目录添加进环境变量
源代码如下:
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,形式如下图所示:
今天早上好不容易将jdk、eclipse以及pydev装进linux,但是,但是,但是,想装numpy的时候,总是报错,发现是没有gcc,然后又去装gcc,真是醉了,到现在gcc还是没有装成功,再想想方法
相关推荐
YENCSDN 2020-11-17
lsjweiyi 2020-11-17
houmenghu 2020-11-17
Erick 2020-11-17
HeyShHeyou 2020-11-17
以梦为马不负韶华 2020-10-20
lhtzbj 2020-11-17
夜斗不是神 2020-11-17
pythonjw 2020-11-17
dingwun 2020-11-16
lhxxhl 2020-11-16
坚持是一种品质 2020-11-16
染血白衣 2020-11-16
huavhuahua 2020-11-20
meylovezn 2020-11-20
逍遥友 2020-11-20
weiiron 2020-11-16