A decision tree is a classifier which uses a sequence of verbose rules (like a>7) which can be easily understood.
The example below trains a decision tree classifier using three feature vectors of length 3, and then predicts the result for a so far unknown fourth feature vector, the so called test vector.
from sklearn.tree import DecisionTreeClassifier # Define training and target set for the classifier train = [[1,2,3],[2,5,1],[2,1,7]] target = [10,20,30] # Initialize Classifier. # Random values are initialized with always the same random seed of value 0 # (allows reproducible results) dectree = DecisionTreeClassifier(random_state=0) dectree.fit(train, target) # Test classifier with other, unknown feature vector test = [2,2,3] predicted = dectree.predict(test) print predicted
Output can be visualized using:
import pydot import StringIO dotfile = StringIO.StringIO() tree.export_graphviz(dectree, out_file=dotfile) (graph,)=pydot.graph_from_dot_data(dotfile.getvalue()) graph.write_png("dtree.png") graph.write_pdf("dtree.pdf")