import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier, plot_tree
n_classes = 3
plot_colors = 'rgb'
plot_step = 0.02
iris = load_iris()
for pairidx, pair in enumerate([[0,1],[0,2],[0,3],
[1,2],[1,3],[2,3]]):
x=iris.data[:,pair]
y=iris.target
clf=DecisionTreeClassifier().fit(x,y)
plt.subplot(2,3,pairidx+1)
x_min,x_max=x[:,0].min()-1,x[:,0].max()+1
y_min,y_max=x[:,1].min()-1,x[:,1].max()+1
xx,yy=np.meshgrid(np.arange(x_min,x_max,plot_step),
np.arange(y_min,y_max,plot_step))
plt.tight_layout(h_pad=0.5,w_pad=0.5,pad=2.5)
Z=clf.predict(np.c_[xx.ravel(),yy.ravel()])
Z=Z.reshape(xx.shape)
cs=plt.contourf(xx,yy,Z,cmap=plt.cm.rainbow)
plt.xlabel(iris.feature_names[pair[0]])
plt.ylabel(iris.feature_names[pair[1]])
for i,color in zip(range(n_classes),plot_colors):
idx=np.where(y==i)
plt.scatter(x[idx,0],x[idx,1],c=color,label=iris.target_names[i],
cmap=plt.cm.rainbow, edgecolor='black', s=15)
plt.suptitle('Decision surface of a decision tree')
plt.legend(loc='best',borderpad=0.01,handletextpad=0.01,labelspacing=0.1,frameon=True)
plt.axis('tight')
plt.figure()
clf=DecisionTreeClassifier().fit(iris.data,iris.target)
plot_tree(clf,filled=True)
plt.show()