<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Decision Tree
CART (Classification  and Regression Tree)
Training a Decision Tree with Scikit-Learn Library
Step1:  DecisionTreeClassifier is capable of both binary (where the labels are [-1, 1]) classification and multiclass (where the labels are [0, …, K-1]) classification.
Applying to Iris Dataset
Step2:  Start Here
Step3:  matplotlib documentation
<ASSISTANT_TASK:>
Python Code:
import pandas as pd
from sklearn import tree
X = [[0, 0], [1, 2]]
y = [0, 1]
clf = tree.DecisionTreeClassifier()
clf = clf.fit(X, y)
clf.predict([[2., 2.]])
clf.predict_proba([[2. , 2.]])
clf.predict([[0.4, 1.2]])
clf.predict_proba([[0.4, 1.2]])
clf.predict_proba([[0, 0.2]])
Explanation: Decision Tree
CART (Classification  and Regression Tree)
Training a Decision Tree with Scikit-Learn Library
End of explanation
from sklearn.datasets import load_iris
from sklearn import tree
iris = load_iris()
iris.data[0:5]
iris.feature_names
X = iris.data[:, 2:]
y = iris.target
y
clf = tree.DecisionTreeClassifier(random_state=42)
clf = clf.fit(X, y)
from sklearn.tree import export_graphviz
export_graphviz(clf,
                out_file="tree.dot",
                feature_names=iris.feature_names[2:],
                class_names=iris.target_names,
                rounded=True,
                filled=True)
import graphviz
dot_data = tree.export_graphviz(clf, out_file=None,
                                feature_names=iris.feature_names[2:],
                                class_names=iris.target_names,
                                rounded=True,
                                filled=True)
graph = graphviz.Source(dot_data)
import numpy as np
import seaborn as sns
sns.set_style('whitegrid')
import matplotlib.pyplot as plt
%matplotlib inline
Explanation: DecisionTreeClassifier is capable of both binary (where the labels are [-1, 1]) classification and multiclass (where the labels are [0, …, K-1]) classification.
Applying to Iris Dataset
End of explanation
df = sns.load_dataset('iris')
df.head()
col = ['petal_length', 'petal_width']
X = df.loc[:, col]
species_to_num = {'setosa': 0,
                  'versicolor': 1,
                  'virginica': 2}
df['tmp'] = df['species'].map(species_to_num)
y = df['tmp']
clf = tree.DecisionTreeClassifier()
clf = clf.fit(X, y)
X[0:5]
X.values
X.values.reshape(-1,1)
Xv = X.values.reshape(-1,1)
Xv
h = 0.02 # set the spacing
Xv.min()
Xv.max() + 1
x_min, x_max = Xv.min(), Xv.max() + 1
y.min()
y.max() + 1
y_min, y_max = y.min(), y.max() + 1
y_min
y_max
np.arange(x_min, x_max, h)
np.arange(y_min, y_max, h)
np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
                     np.arange(y_min, y_max, h))
xx
yy
xx.ravel()
xx.ravel?
yy.ravel()
np.c_[xx.ravel(), yy.ravel()]
np.c_?
pd.DataFrame(np.c_[xx.ravel(), yy.ravel()])
z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
z
xx.shape
z.shape
z = z.reshape(xx.shape)
z.shape
plt.contourf?
Explanation: Start Here
End of explanation
fig = plt.figure(figsize=(16,10))
ax = plt.contourf(xx, yy, z, cmap = 'afmhot', alpha=0.3);
fig = plt.figure(figsize=(16,10))
plt.scatter(X.values[:, 0], X.values[:, 1], c=y, s=80, 
            alpha=0.9, edgecolors='g');
fig = plt.figure(figsize=(16,10))
ax = plt.contourf(xx, yy, z, cmap = 'afmhot', alpha=0.3);
plt.scatter(X.values[:, 0], X.values[:, 1], c=y, s=80, 
            alpha=0.9, edgecolors='g');
Explanation: matplotlib documentation
End of explanation
<END_TASK>