当前位置: 移动技术网 > IT编程>脚本编程>Python > Python3 决策树

Python3 决策树

2018年01月17日  | 移动技术网IT编程  | 我要评论

快乐大本营 魏晨,11dxdx,描情网

 1 # -*- coding: utf-8 -*-
 2 """
 3 Created on Fri Dec 29 10:18:04 2017
 4 
 5 @author: markli
 6 """
 7 from sklearn.feature_extraction import DictVectorizer;
 8 from sklearn import preprocessing;
 9 from sklearn import tree;
10 from sklearn.externals.six import StringIO;
11 from sklearn.externals import joblib;
12 import csv;
13 import sys;
14 
15 sys.path.append('../');
16 filepath = 'decisiontree.csv';
17 f = open(filepath,'r');
18 reader = csv.reader(f);
19 header = next(reader); #读取表头
20 print("表头为 %s" % header);
21 
22 feature_list = [];
23 label_list = [];
24 for row in reader:
25     label_list.append(row[len(row)-1]);
26     rowdic = {};
27     for i in range(1,len(row)-1):
28         rowdic[header[i]] = row[i];
29     feature_list.append(rowdic);
30 
31 print("特征值为 %s" % feature_list);
32     
33 dv = DictVectorizer();
34 dummX = dv.fit_transform(feature_list).toarray();
35 print("特征提取值矩阵为 %s" % str(dummX));
36 
37 #目标值特征化
38 lb = preprocessing.LabelBinarizer();
39 dummY = lb.fit_transform(label_list);
40 print("目标特征化值为 %s" % str(dummY));
41 
42 clf = tree.DecisionTreeClassifier(criterion='entropy');
43 clf = clf.fit(dummX,dummY);
44 print("树 %s" % str(clf));
45 
46 #保存模型
47 with open('dicisiontreeModel.dot','w') as f:
48     f = tree.export_graphviz(clf,feature_names=dv.get_feature_names(),out_file=f);
49 joblib.dump(clf,'dicisionTree_entropyModel.dot');
50 
51 
52 #读取模型 预测
53 '''
54 x = np.array([0,1,0,0,0,1,0,1,1,0]); #测试值
55 print(x.reshape((1,10)));
56 #sys.path.append('F:\\Python\\ML');
57 #f = open('F:\\Python\\ML\\dicisionTree_entropyModel.dot');
58 clf = joblib.load('F:\\Python\\ML\\dicisionTree_entropyModel.dot');
59 y = clf.predict(x.reshape((1,10))); #预测结果
60 print(y);
61 '''

 

decisiontree.csv 文件格式

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网