Python3 decision tree and python3 Decision Tree

Source: Internet
Author: User

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 );
'''

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.