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

from sklearn.datasets import load_breast_cancer
cancer = load_breast_cancer()

cancer.keys()

cancer['feature_names']

df_in = pd.DataFrame(cancer['data'],columns=cancer['feature_names'])

df_target = pd.DataFrame(cancer['target'],columns=['Cancer'])

df_in.head()

"""
Explanation: Support Vector Machines
End of explanation
"""


from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(df_in, np.ravel(df_target), test_size=0.30, random_state=101)

"""
Explanation: Train Test Split
End of explanation
"""


from sklearn.svm import SVC

model = SVC(kernel='rbf')

model.fit(X_train,y_train)

"""
Explanation: Train the Support Vector Classifier
End of explanation
"""


predictions = model.predict(X_test)

from sklearn.metrics import classification_report,confusion_matrix

print(confusion_matrix(y_test,predictions))

print(classification_report(y_test,predictions))

"""
Explanation: Predictions and Evaluations
Now let's predict using the trained model.
End of explanation
"""


param_grid = {'C': [0.1,1, 10, 100, 1000], 'gamma': [1,0.1,0.01,0.001,0.0001], 'kernel': ['rbf']} 

from sklearn.model_selection import GridSearchCV

grid = GridSearchCV(SVC(),param_grid,refit=True,verbose=3)

grid.fit(X_train,y_train)

grid.best_params_

grid.best_estimator_

grid_predictions = grid.predict(X_test)

print(confusion_matrix(y_test,grid_predictions))

print(classification_report(y_test,grid_predictions))

"""
Explanation: Woah! Notice that we are classifying everything into a single class! This means our model needs to have it parameters adjusted (it may also help to normalize the data).
We can search for parameters using a GridSearch!
Gridsearch
Finding the right parameters (like what C or gamma values to use) is a tricky task! But luckily, we can be a little lazy and just try a bunch of combinations and see what works best! This idea of creating a 'grid' of parameters and just trying out all the possible combinations is called a Gridsearch, this method is common enough that Scikit-learn has this functionality built in with GridSearchCV! The CV stands for cross-validation which is the
GridSearchCV takes a dictionary that describes the parameters that should be tried and a model to train. The grid of parameters is defined as a dictionary, where the keys are the parameters and the values are the settings to be tested.
End of explanation
"""


from sklearn.tree import DecisionTreeClassifier

dtree = DecisionTreeClassifier()

dtree.fit(X_train,y_train)

treepredictop = dtree.predict(X_test)

print(classification_report(y_test,treepredictop))

features = X_train.columns
features

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

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())  

dtree2 = DecisionTreeClassifier(min_samples_split=50)

dtree2.fit(X_train, y_train)

tree2predictop = dtree2.predict(X_test)

print(classification_report(y_test,treepredictop))

dot_data = StringIO()  
export_graphviz(dtree2, 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: Decision Tree
End of explanation
"""


from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)

"""
Explanation: Ensemble Methods
Ensemble methods use a number of models to learn on data and then average or vote among their predictions.
There are two families of ensemble methods:
- Averaging methods: Bagging methods, Random Forests, ...
- Boosting methods: AdaBoost, Gradient Tree Boosting
End of explanation
"""


from sklearn.ensemble import RandomForestClassifier
model = RandomForestClassifier(n_estimators=50, min_samples_split=2)

model.fit(X_train, y_train);

model.score(X_test, y_test)

"""
Explanation: Random Forest
End of explanation
"""


from sklearn.ensemble import AdaBoostClassifier
model = AdaBoostClassifier(n_estimators=100)

model.fit(X_train, y_train);

model.score(X_test, y_test)

"""
Explanation: AdaBoost
End of explanation
"""


