"""
## 1. Install libraries
"""
!python3 -m pip install --upgrade pip
!pip install seaborn==0.11.0
!python3 -m pip install pandas
!python3 -m pip install matplotlib
!python3 -m pip install seaborn
!python3 -m pip install sklearn
"""
## 2. Import libraries
"""
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning) 
"""
## 3. Load Dataset
"""
df = pd.read_csv("https://raw.githubusercontent.com/ojjy/datascience/master/heart_failure_clinic/heart_failure_clinical_records_dataset.csv")
df.head(5)
"""
## 4. Data Explorary Analysis
"""
df['age'] = df['age'].astype(int)
df.tail(5)
df.shape
df.isnull().sum()
df.info()
df['anaemia'].value_counts()
df['diabetes'].value_counts()
df['sex'].value_counts()
df['smoking'].value_counts()
df['DEATH_EVENT'].value_counts()
df[(df['anaemia']==1)&(df['diabetes']==1)&(df['smoking']==1)]
plt.figure(figsize=(15,10))
sns.countplot(x='age', data=df)
fig, axes = plt.subplots(2, 3, figsize=(15, 10))
sns.countplot(x=df['anaemia'], data=df, ax=axes[0,0])
sns.countplot(x=df['diabetes'], data=df, ax=axes[0,1])
sns.countplot(x=df['high_blood_pressure'], data=df, ax=axes[0,2])
sns.countplot(x=df['sex'], data=df, ax=axes[1,0])
sns.countplot(x=df['smoking'], data=df, ax=axes[1,1])
sns.countplot(x=df['DEATH_EVENT'], data=df, ax=axes[1,2])
"""
## 5. Split data
"""
x=df.drop('DEATH_EVENT', axis=1)
y=df['DEATH_EVENT']
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)
print(f"x_train: {x_train.shape}")
print(f"x_test: {x_test.shape}")
print(f"y_train: {y_train.shape}")
print(f"y_test: {y_test.shape}")
accuracy_list=[]
model_list=[]
"""
## 6. Modeling
"""
"""
### 6-1. AdaBoost
"""
from sklearn.ensemble import AdaBoostClassifier
from sklearn.metrics import accuracy_score, confusion_matrix
ada_clf = AdaBoostClassifier(n_estimators=500, learning_rate=0.01, random_state=0)
ada_model = ada_clf.fit(x_train, y_train)
y_pred_ada = ada_model.predict(x_test)
ada_acc = accuracy_score(y_test, y_pred_ada)
accuracy_list.append(round(ada_acc*100, 2))
model_list.append("AdaBoost")
round(ada_acc*100, 2)
ada_clf.score(x_test, y_test)
confusion_matrix(y_test, y_pred_ada)
"""
### 6-2. XGBoost
"""
!pip install xgboost
from xgboost import XGBClassifier
from sklearn.metrics import accuracy_score, confusion_matrix
xgb_clf = XGBClassifier(n_estimators=500, learning_rate=0.01, random_state=0)
xgb_model=xgb_clf.fit(x_train, y_train)
y_pred_xgb = xgb_model.predict(x_test)
xgb_acc = accuracy_score(y_test, y_pred_xgb)
accuracy_list.append(round(xgb_acc*100, 2))
model_list.append("XGBoost")
round(xgb_acc*100, 2)
xgb_clf.score(x_test, y_test)
confusion_matrix(y_test, y_pred_xgb)
"""
### 6-3. GradientBoost
"""
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.metrics import accuracy_score, confusion_matrix
grb_clf = GradientBoostingClassifier(max_depth=2, random_state=1)
grb_model = grb_clf.fit(x_train, y_train)
y_pred_grb = grb_model.predict(x_test)
grb_acc = accuracy_score(y_test, y_pred_grb)
accuracy_list.append(round(grb_acc*100, 2))
model_list.append("GradientBoost")
round(grb_acc*100, 2)
grb_clf.score(x_test, y_test)
confusion_matrix(y_test, y_pred_grb)
"""
### 6-4. Logistic Regression
"""
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix
log_reg = LogisticRegression()
log_reg_model = log_reg.fit(x_train, y_train)
y_pred_log = log_reg_model.predict(x_test)
log_reg_acc = accuracy_score(y_test, y_pred_log)
accuracy_list.append(round(log_reg_acc*100,2))
model_list.append("Logistic Regression")
round(log_reg_acc*100,2)
log_reg.score(x_test, y_test)
confusion_matrix(y_test, y_pred_log)
"""
### 6-5. Support Vector Machine
"""
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score, confusion_matrix
sv_clf = SVC()
sv_clf.fit(x_train, y_train)
y_pred_sv = sv_clf.predict(x_test)
sv_clf_acc = accuracy_score(y_test, y_pred_sv)
accuracy_list.append(round(sv_clf_acc*100, 2))
model_list.append("SVC")
round(sv_clf_acc*100, 2)
sv_clf.score(x_test, y_test)
confusion_matrix(y_test, y_pred_sv)
"""
### 6-6. KNN
"""
from sklearn.neighbors import KNeighborsClassifier
from sklearn.metrics import accuracy_score, confusion_matrix
kn_clf = KNeighborsClassifier(n_neighbors=6)
kn_clf.fit(x_train, y_train)
y_pred_kn = kn_clf.predict(x_test)
kn_clf_acc = accuracy_score(y_test, y_pred_kn)
accuracy_list.append(round(kn_clf_acc*100, 2))
model_list.append('KNN')
round(kn_clf_acc*100, 2)
kn_clf.score(x_test, y_test)
confusion_matrix(y_test, y_pred_kn)
"""
### 6-7. Decision Tree
"""
from sklearn.tree import DecisionTreeClassifier
from sklearn.metrics import accuracy_score, confusion_matrix
dt_clf = DecisionTreeClassifier(max_leaf_nodes=3, random_state=0, criterion='entropy')
dt_clf.fit(x_train, y_train)
y_pred_dt = dt_clf.predict(x_test)
dt_clf_acc = accuracy_score(y_test, y_pred_dt)
accuracy_list.append(round(dt_clf_acc*100, 2))
model_list.append('Decision Tree')
round(dt_clf_acc*100, 2)
dt_clf.score(x_test, y_test)
confusion_matrix(y_test, y_pred_dt)
"""
### 6-8. Random Forest
"""
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, confusion_matrix
r_clf = RandomForestClassifier(max_features=0.5, max_depth=15, random_state=1)
r_clf.fit(x_train, y_train)
y_pred_r = r_clf.predict(x_test)
r_clf_acc = accuracy_score(y_test, y_pred_r)
accuracy_list.append(round(r_clf_acc*100, 2))
model_list.append("Random Forest")
round(r_clf_acc*100, 2)
r_clf.score(x_test, y_test)
confusion_matrix(y_test, y_pred_r)
!pip install lightgbm
"""
### 6-8. lightgbm
"""
from lightgbm import LGBMClassifier
from sklearn.metrics import accuracy_score, confusion_matrix
lgb_clf = LGBMClassifier(max_depth=2, random_state=4)
lgb_clf.fit(x_train,y_train)
y_pred_lgb = lgb_clf.predict(x_test)
lgb_clf_acc = accuracy_score(y_test, y_pred_lgb)
accuracy_list.append(round(lgb_clf_acc*100, 2))
model_list.append('LGBM')
round(lgb_clf_acc*100, 2)
lgb_clf.score(x_test, y_test)
confusion_matrix(y_test, y_pred_lgb)
"""
### 6-9. CatBoost
"""
!pip install catboost
from catboost import CatBoostClassifier
from sklearn.metrics import accuracy_score, confusion_matrix
cat_clf = CatBoostClassifier()
cat_clf.fit(x_train,y_train)
y_pred_cat = cat_clf.predict(x_test)
cat_clf_acc = accuracy_score(y_test, y_pred_cat)
accuracy_list.append(round(cat_clf_acc*100, 2))
model_list.append('CatBoost')
round(cat_clf_acc*100, 2)
cat_clf.score(x_test, y_test)
confusion_matrix(y_test, y_pred_cat)
"""
## 7. Compared with models
"""
accuracy_list
model_list
dataset = pd.DataFrame({'x':model_list, 'y':accuracy_list})
dataset
plt.rcParams['figure.figsize']=20,8
plt.xlabel('Classifier Models', fontsize = 20)
plt.ylabel('% of Accuracy', fontsize = 20)
plt.bar(model_list, accuracy_list)
for i, v in enumerate(model_list):
    plt.text(v, accuracy_list[i], accuracy_list[i],  # 좌표 (x축 = v, y축 = y[0]..y[1], 표시 = y[0]..y[1])
             fontsize='large',
             horizontalalignment='center',  # horizontalalignment (left, center, right)
             verticalalignment='bottom')    # verticalalignment (top, center, bottom)