import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline

"""
Explanation: <a href='http://www.pieriandata.com'> <img src='../Pierian_Data_Logo.png' /></a>

Decision Trees and Random Forests in Python
This is the code for the lecture video which goes over tree methods in Python. Reference the video lecture for the full explanation of the code!
I also wrote a blog post explaining the general logic of decision trees and random forests which you can check out. 
Import Libraries
End of explanation
"""


df = pd.read_csv('kyphosis.csv')

df.head()

"""
Explanation: Get the Data
End of explanation
"""


sns.pairplot(df,hue='Kyphosis',palette='Set1')

"""
Explanation: EDA
We'll just check out a simple pairplot for this small dataset.
End of explanation
"""


from sklearn.model_selection import train_test_split

X = df.drop('Kyphosis',axis=1)
y = df['Kyphosis']

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30)

"""
Explanation: Train Test Split
Let's split up the data into a training set and a test set!
End of explanation
"""


from sklearn.tree import DecisionTreeClassifier

dtree = DecisionTreeClassifier()

dtree.fit(X_train,y_train)

"""
Explanation: Decision Trees
We'll start just by training a single decision tree.
End of explanation
"""


predictions = dtree.predict(X_test)

from sklearn.metrics import classification_report,confusion_matrix

print(classification_report(y_test,predictions))

print(confusion_matrix(y_test,predictions))

"""
Explanation: Prediction and Evaluation
Let's evaluate our decision tree.
End of explanation
"""


from IPython.display import Image  
from sklearn.externals.six import StringIO  
from sklearn.tree import export_graphviz
import pydot 

features = list(df.columns[1:])
features

dot_data = StringIO()  
export_graphviz(dtree, out_file=dot_data,feature_names=features,filled=True,rounded=True)

graph = pydot.graph_from_dot_data(dot_data.getvalue())  
Image(graph[0].create_png())  

"""
Explanation: Tree Visualization
Scikit learn actually has some built-in visualization capabilities for decision trees, you won't use this often and it requires you to install the pydot library, but here is an example of what it looks like and the code to execute this:
End of explanation
"""


from sklearn.ensemble import RandomForestClassifier
rfc = RandomForestClassifier(n_estimators=100)
rfc.fit(X_train, y_train)

rfc_pred = rfc.predict(X_test)

print(confusion_matrix(y_test,rfc_pred))

print(classification_report(y_test,rfc_pred))

"""
Explanation: Random Forests
Now let's compare the decision tree model to a random forest.
End of explanation
"""


