import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import plotly.express as px
import plotly.graph_objects as go
import plotly.offline as pyo
import plotly.figure_factory as ff
from plotly import tools
from plotly.subplots import make_subplots
from plotly.offline import iplot
from sklearn.ensemble import RandomForestClassifier, ExtraTreesClassifier
from sklearn.svm import SVC
from xgboost.sklearn import XGBClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
from sklearn import metrics
from sklearn.metrics import accuracy_score
from sklearn.metrics import classification_report,confusion_matrix
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
from sklearn.ensemble import RandomForestClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.naive_bayes import GaussianNB
from sklearn.metrics import RocCurveDisplay
df=pd.read_csv('/kaggle/input/health-care-data-set-on-heart-attack-possibility/heart.csv')
df.head()
df2=df.copy()
df.info()
train_dict=pd.DataFrame(df.dtypes,columns=['Data types'])
train_dict['null']=df.isnull().sum()
train_dict['unique vals']=df.nunique()
train_dict['count']=df.count()
train_dict
df.columns
df.describe()
"""
feature distribution
"""
df.hist(bins=50,figsize=((20,15)))
"""
EDA
"""
fig = px.histogram(data_frame = df,
             x = "sex",
             color="target", title="<b>age vs target</b>",   
)
fig.show()
fig = px.histogram(data_frame = df,
             x = "fbs",
             color="target", title="<b>fbs vs target</b>",   
)
fig.show()
fig = px.histogram(data_frame = df,
             x = "chol",
             color="target", title="<b>chol vs target</b>",   
)
fig.show()
fig = px.histogram(data_frame = df,
             x = "ca",
             color="target", title="<b>ca vs target</b>",   
)
fig.show()
fig = px.histogram(data_frame = df,
             x = "age",
             color="sex", title="<b>age vs sex</b>",   
)
fig.show()
train_dict
df.shape
corrMatrix = df.corr()
sns.heatmap(corrMatrix, annot=True)
plt.show()
"""
Preprocessing
"""
df.head()
y=df.target
df.drop('target',1,inplace=True)
from sklearn.model_selection import train_test_split
X_train,X_test,y_train,y_test=train_test_split(df,y,test_size=0.3)
model = [DecisionTreeClassifier(),RandomForestClassifier(), ExtraTreesClassifier() , XGBClassifier(),GaussianNB(),KNeighborsClassifier()]
trainAccuracy = list()
testAccuracy = list()
kfold = KFold(n_splits=10, random_state=7, shuffle=True)

for mdl in model:
    trainResult = cross_val_score(mdl, X_train, y_train, scoring='accuracy', cv=kfold)
    trainAccuracy.append(trainResult.mean())
    mdl.fit(X_train, y_train)
    y_pred = mdl.predict(X_test)
    testResult = metrics.accuracy_score(y_test, y_pred)
    testAccuracy.append(testResult)
print('The comparision\n')
modelScore = pd.DataFrame({'Model' : model, 'Train_Accuracy' : trainAccuracy, 'Test_Accuracy' : testAccuracy})
modelScore
print('ExtraTreeClassifier\n')
model = ExtraTreesClassifier(n_estimators=200,max_depth=150)
model.fit(X_train, y_train)

pred = model.predict(X_test)
print(metrics.classification_report(y_test,pred))
sns.heatmap(confusion_matrix(y_test,pred), annot=True, fmt='d')
plt.show()
