Decision Tree

1 Classification

Takes as input two array: an array X, sparse or dense,of size [n_sample, n_features] holding the training samples, and an array Y of integer values,size[n_samples], holding the class labels for the training samples.


In [19]:
from sklearn import tree
X=[[0,0],[1,1]]
y=[0,1]
clf=tree.DecisionTreeClassifier()
clf=clf.fit(X,y)

In [18]:
clf.predict([[2.,2.]])


Out[18]:
array([ 2.5])

In [20]:
clf.predict_proba([[2.0,2.0]])


Out[20]:
array([[ 0.,  1.]])

In [21]:
from sklearn.datasets import load_iris
from sklearn import tree
iris=load_iris()
clf=tree.DecisionTreeClassifier()
clf=clf.fit(iris.data, iris.target)

In [22]:
with open('iris.dot','w') as f:
    f=tree.export_graphviz(clf,out_file=f)

In [23]:
import os
os.unlink('iris.dot')

In [25]:
from IPython.display import Image
import pydotplus
dot_data=tree.export_graphviz(clf, out_file=None, 
                         feature_names=iris.feature_names,  
                         class_names=iris.target_names,  
                         filled=True, rounded=True,  
                         special_characters=True)
graph=pydotplus.graph_from_dot_data(dot_data)
Image(graph.create_png())


---------------------------------------------------------------------------
InvocationException                       Traceback (most recent call last)
<ipython-input-25-c43efdc6dfc4> in <module>()
      7                          special_characters=True)
      8 graph=pydotplus.graph_from_dot_data(dot_data)
----> 9 Image(graph.create_png())

/Users/gaufung/anaconda/lib/python3.6/site-packages/pydotplus/graphviz.py in <lambda>(f, prog)
   1795             self.__setattr__(
   1796                 'create_' + frmt,
-> 1797                 lambda f=frmt, prog=self.prog: self.create(format=f, prog=prog)
   1798             )
   1799             f = self.__dict__['create_' + frmt]

/Users/gaufung/anaconda/lib/python3.6/site-packages/pydotplus/graphviz.py in create(self, prog, format)
   1958             if self.progs is None:
   1959                 raise InvocationException(
-> 1960                     'GraphViz\'s executables not found')
   1961 
   1962         if prog not in self.progs:

InvocationException: GraphViz's executables not found

2 Regression


In [15]:
from sklearn import tree
X = [[0,0],[2,2]]
y = [0.5,2.5]
clf=tree.DecisionTreeRegressor()
clf=clf.fit(X,y)
clf.predict([[1,1]])


Out[15]:
array([ 0.5])