Python3 decision tree and python3 Decision Tree
#-*-Coding: UTF-8 -*-
"""
Created on Fri Dec 29 10:18:04 2017
@ Author: markli
"""
From sklearn. feature_extraction import DictVectorizer;
From sklearn import preprocessing;
From sklearn import tree;
From sklearn. externals. six import StringIO;
From sklearn. externals import joblib;
Import csv;
Import sys;
Sys. path. append ('../');
Filepath = 'decisiontree.csv ';
F = open (filepath, 'R ');
Reader = csv. reader (f );
Header = next (reader); # Read the header
Print ("header: % s" % header );
Feature_list = [];
Label_list = [];
For row in reader:
Label_list.append (row [len (row)-1]);
Rowdic = {};
For I in range (1, len (row)-1 ):
Rowdic [header [I] = row [I];
Feature_list.append (rowdic );
Print ("feature value: % s" % feature_list );
Dv = DictVectorizer ();
DummX = dv. fit_transform (feature_list). toarray ();
Print ("feature extraction value matrix: % s" % str (dummX ));
# Target value Characterization
Lb = preprocessing. LabelBinarizer ();
DummY = lb. fit_transform (label_list );
Print ("Target Feature Value % s" % str (dummY ));
Clf = tree. DecisionTreeClassifier (criterion = 'entropy ');
Clf = clf. fit (dummX, dummY );
Print ("tree % s" % str (clf ));
# Save the Model
With open ('dicisiontreemodel. dot ', 'w') as f:
F = tree. export_graphviz (clf, feature_names = dv. get_feature_names (), out_file = f );
Joblib. dump (clf, 'dicisiontree _ entropyModel. dot ');
# Read Model Prediction
'''
X = np. array ([,]); # Test Value
Print (x. reshape (1, 10 )));
# Sys. path. append ('f: \ Python \ ml ');
# F = open ('f: \ Python \ ML \ dicisionTree_entropyModel.dot ');
Decisiontree.csv File Format
Clf = joblib. load ('f: \ Python \ ML \ dicisionTree_entropyModel.dot ');
Y = clf. predict (x. reshape (1, 10); # prediction result
Print (y );
'''