# -*- coding: utf-8 -*-
"""knn_dtc_.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1xd8oJeNQ0RiUtMXUhNaMfrc4eoT8mJkv
"""

import pandas as pd
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report, confusion_matrix,accuracy_score  
import seaborn as sns
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree import DecisionTreeRegressor
from sklearn.cluster import KMeans
from numpy import unique
from numpy import where
from matplotlib import pyplot
import pydotplus
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/heart-disease/processed.cleveland.data"

names = ['age', 'gender', 'cp', 'trestbps', 'chol','fbs','restecg','thalach','exang','oldpeak','slope','ca','thal','diagnosis']
data = pd.read_csv(url, names=names,na_values=["?"])
data.median()

data["ca"].fillna(0,inplace = True)
data["thal"].fillna(3,inplace = True)
data.info()

X = data.iloc[:,0:12]
y = data.iloc[:,13]

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=3)
X_train = X_train.fillna(X_train.mean())
X_test = X_test.fillna(X_test.mean())

"""**KNN Regresyon**"""

clf1 = KNeighborsClassifier(n_neighbors=10)
clf1.fit(X_train, y_train)
y_pred = clf1.predict(X_test) 
cm1 = confusion_matrix(y_test, y_pred)

plt.figure(figsize=(5,4))
sns.heatmap(cm1, annot=True)
plt.title('kNN \nAccuracy:{0:.3f}'.format(accuracy_score(y_test, y_pred)))
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.show()

scaler = StandardScaler()
scaler.fit_transform(X_train)

X_train = scaler.transform(X_train)
X_test = scaler.transform(X_test)
clf2 = KNeighborsClassifier(n_neighbors=10)
clf2.fit(X_train, y_train)
y_pred = clf1.predict(X_test)
cm2 = confusion_matrix(y_test, y_pred)
plt.figure(figsize=(5,4))
sns.heatmap(cm2, annot=True)
plt.title('kNN \nAccuracy:{0:.3f}'.format(accuracy_score(y_test, y_pred)))
plt.ylabel('True label')
plt.xlabel('Predicted label')
plt.show()

from sklearn.metrics import mean_absolute_error, mean_squared_error
mae_e = []
mse_e = []
for i in range(1,35):
    clf = KNeighborsClassifier(n_neighbors=i)
    clf.fit(X_train, y_train)
    y_pred = clf.predict(X_test) 
    cm = confusion_matrix(y_test, y_pred)
    plt.figure(figsize=(5,4))
    sns.heatmap(cm, annot=True)
    plt.title('kNN \nAccuracy:{0:.3f}'.format(accuracy_score(y_test, y_pred)))
    plt.ylabel('True label')
    plt.xlabel('Predicted label')
    error_mae = mean_absolute_error(y_test, y_pred)
    mae_e.append(error_mae)
    error_mse = mean_squared_error(y_test, y_pred, squared=False)
    mse_e.append(error_mse)
    print('MAE  : ', error_mae)
    print('MSE  : ', error_mse)
    plt.show()

curve = pd.DataFrame(mae_e)
curve.plot()

curve2 = pd.DataFrame(mse_e)
curve2.plot()

"""**karar** **ağacı**"""

from sklearn.metrics import mean_absolute_error, mean_squared_error
from sklearn import tree

mae = []
mse = []
X = data.iloc[:,0:12]
y = data.iloc[:,13]

for i in range(1,5):
    for j in range(1,5):
        i=i+1
        print("split=",i,", depth=",j)
        X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1)
        dtc = DecisionTreeClassifier(min_samples_split=i)
        dtc.fit(X_train, y_train)
        dtc_1 = DecisionTreeRegressor(max_depth=j)
        dtc_1.fit(X_train, y_train)
        test_predictions = dtc_1.predict(X_test)
        print(test_predictions)
        err_mae = mean_absolute_error(y_test, test_predictions)
        mae.append(err_mae)
        err_mse = mean_squared_error(y_test, test_predictions, squared=False)
        mse.append(err_mse)
        print('MAE  : ', err_mae)
        print('MSE  : ', err_mse)

dtc_curve = pd.DataFrame(mae)
dtc_curve.plot()

dtc_curve2 = pd.DataFrame(mse)
dtc_curve2.plot()

"""Best 8. dt (split=6 depth=4)"""

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.1)
bdtc = DecisionTreeClassifier(min_samples_split=6)
bdtc.fit(X_train, y_train)
bdtc_1 = DecisionTreeRegressor(max_depth=4)
bdtc_1.fit(X_train, y_train)

from IPython.display import Image

dot_data = tree.export_graphviz(bdtc_1, out_file=None)
graph = pydotplus.graph_from_dot_data(dot_data)
Image(graph.create_png())