# Chapter 3 Decision tree learning

# Maximizing information gain - getting the most bang for the buck
''' Plot the impurity indices for the probability range [0,1] for class 1.
Note that we will also add in a scaled version of the entropy (entropy/2)
to observe that the Gini impurity is an intermediate measure  between
entropy and the classification error. '''

import matplotlib.pyplot as plt
import numpy as np

def gini(p):
    return (p) * (1 - (p)) + (1 - p) * (1 - (1-p))
    
def entropy(p):
    return - p*np.log2(p) - (1 - p)* np.log2((1 - p))
    
def error(p):
    return 1 - np.max([p, 1 - p])
    
x = np.arange(0.0, 1.0, 0.01)
ent = [entropy(p) if p != 0 else None for p in x]
sc_ent = [e*0.5 if e else None for e in ent]
err = [error(i) for i in x]
fig = plt.figure()
ax = plt.subplot(111)
for i, lab, ls, c, in zip([ent, sc_ent, gini(x), err], ['Entropy', 'Entropy (scaled)', 'Gini Impurity', 'Misclassification Error'],
['-', '-', '--', '-.'], ['black', 'lightgray', 'red', 'green', 'cyan']):
   line = ax.plot(x, i, label=lab, linestyle=ls, lw=2, color=c)
   
ax.legend(loc='upper center', bbox_to_anchor=(0.5, 1.15), ncol=3, fancybox=True, shadow=False)
ax.axhline(y=0.5, linewidth=1, color='k', linestyle='--')
ax.axhline(y=1.0, linewidth=1, color='k', linestyle='--')
plt.ylim([0, 1.1])
plt.xlabel('p(i=1)')
plt.ylabel('Impurity Index')
plt.show()

# Building a decision tree
'''
Decision trees can build complex decision boundaries by dividing the feature
space into rectangles. Using scikit-learn, we will now train a decision tree with
a maximum depth of 3 using entropy as a criterion for impurity.  Although feature
scaling may be desired for visualization purposes, note that feature scaling is
not a requirement for decision tree algorithms.
 '''
# Train a  model to classify the different flowers in our Iris dataset
from sklearn import datasets
import numpy as np

iris = datasets.load_iris()
X = iris.data[:, [2, 3]]
y = iris.target

from sklearn.cross_validation import train_test_split

# random_state : int or RandomState
# Pseudo-random number generator state used for random sampling.
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)

from matplotlib.colors import ListedColormap
import matplotlib.pyplot as plt


def plot_decision_regions(X, y, classifier, test_idx=None, resolution=0.02):
    # setup marker generator and color map
    markers = ('s', 'x', 'o', '^', 'v')
    colors = ('red', 'blue', 'lightgreen', 'gray', 'cyan')
    cmap = ListedColormap(colors[:len(np.unique(y))])
    # plot the decision surface
    x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1
    x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1
    xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution),
                           np.arange(x2_min, x2_max, resolution))
    Z = classifier.predict(np.array([xx1.ravel(), xx2.ravel()]).T)
    Z = Z.reshape(xx1.shape)
    plt.contourf(xx1, xx2, Z, alpha=0.4, cmap=cmap)
    plt.xlim(xx1.min(), xx1.max())
    plt.ylim(xx2.min(), xx2.max())
    # plot all samples
    X_test, y_test = X[test_idx, :], y[test_idx]
    for idx, cl in enumerate(np.unique(y)):
        plt.scatter(x=X[y == cl, 0], y=X[y == cl, 1], alpha=0.8, c=cmap(idx), marker=markers[idx], label=cl)

    # highlight test samples
    if test_idx:
        X_test, y_test = X[test_idx, :], y[test_idx]
        plt.scatter(X_test[:, 0], X_test[:, 1], c='', alpha=1.0, linewidth=1, marker='o', s=55, label='test set')


from sklearn.tree import DecisionTreeClassifier
tree = DecisionTreeClassifier(criterion='entropy',max_depth=3, random_state=0)
tree.fit(X_train, y_train)
X_combined = np.vstack((X_train, X_train))
y_combined = np.hstack((y_train, y_test))
plot_decision_regions(X_combined, y_combined, classifier=tree, test_idx=range(105,150))
plt.xlabe('petal length [cm]')
plt.ylabel('petal width [cm]')
plt.legend(loc='upper left')
plt.show()

'''
A nice feature in scikit-learn is that is allows us to export the decision tree as a 
.dot file after training, which we can visualize using the GraphViz program. 
First, we create the .dot file via scikit-learn using the export_graphviz function
from the tree submodule, as follows:
'''
from sklearn.tree import export_graphviz
export_graphviz(tree, out_file='C:\Users\Wei\Desktop/tree.dot', feature_names=['petal length', 'petal width'])

'''
After installed GraphViz on the computer, we can convert the tree.dot file into a PNG file
by executing the following command from the command line in the location where we 
saved the tree.dot file:

> dot -Tpng tree.dot -o tree.png
'''



















