text_plain_output_1.png image_output_1.png
from imblearn.under_sampling import RandomUnderSampler

from sklearn.discriminant_analysis import LinearDiscriminantAnalysis

from sklearn.model_selection import GridSearchCV

from sklearn.model_selection import cross_validate

from sklearn.naive_bayes import GaussianNB

from sklearn.neighbors import KNeighborsClassifier

from sklearn.svm import SVC

from sklearn.tree import DecisionTreeClassifier

import matplotlib.pyplot as plt

import os

import pandas as pd

import seaborn as sb

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sb
sb.set(style='whitegrid')
import os

df = pd.read_csv('../input/full-filled-brain-stroke-dataset/full_data.csv')

df[['hypertension', 'heart_disease', 'stroke']] = df[['hypertension', 'heart_disease', 'stroke']].astype('object')

def plot_cat_features(df):
  fig, ax = plt.subplots(2, 3, figsize=(18,12))

  sb.countplot(x='stroke', data=df, hue='gender', ax=ax[0,0])
  sb.countplot(x='stroke', data=df, hue='hypertension', ax=ax[0,1])
  sb.countplot(x='stroke', data=df, hue='heart_disease', ax=ax[0,2])
  sb.countplot(x='stroke', data=df, hue='ever_married', ax=ax[1,0])
  sb.countplot(x='stroke', data=df, hue='work_type', ax=ax[1,1])
  sb.countplot(x='stroke', data=df, hue='smoking_status', ax=ax[1,2])

  fig.suptitle('Bar graph showing the number of people who had or not Stroke', y=0.93, fontsize=22)

  plt.show()

def plot_nmr_features(df):
  fig, ax = plt.subplots(3, 2, figsize=(12, 18))

  sb.distplot(df['age'], label='Skewness: %.2f'%(df['age'].skew()), ax=ax[0,0])
  ax[0,0].legend(loc='best')
  sb.boxplot(df['age'], ax=ax[0,1])
  sb.distplot(df['avg_glucose_level'], label='Skewness: %.2f'%df['avg_glucose_level'].skew(), ax=ax[1,0])
  ax[1,0].legend(loc='best')
  sb.boxplot(df['avg_glucose_level'], ax=ax[1,1])
  sb.distplot(df['bmi'], label='Skewness: %.2f'%df['bmi'].skew(), ax=ax[2,0])
  ax[2,0].legend(loc='best')
  sb.boxplot(df['bmi'], ax=ax[2,1])

  fig.suptitle('Distribution of numerical features', y=0.93, fontsize=22)
  plt.show()

df.isnull().sum()

data = df[(df['avg_glucose_level'] <= 160) & (df['bmi'] <= 45)]
data = data.reset_index(drop=True)

features = data.drop('stroke', axis=1).columns
features

cat_features = []
nmr_features = []
for i in range(len(features)):
    if df.iloc[:, i].dtype == 'object':
        cat_features.append(df.columns[i])
    else:
        nmr_features.append(df.columns[i])

data = pd.get_dummies(data, columns=cat_features, drop_first=True)

x = data.drop('stroke', axis=1)
y = data['stroke'].astype('int')

from imblearn.under_sampling import RandomUnderSampler
undersample = RandomUnderSampler(sampling_strategy=0.5)
x_under, y_under = undersample.fit_resample(x, y)

pd.DataFrame(y_under).value_counts()

def model_evaluation(x, y):
    models = []
    names = []
    scoring = ['accuracy', 'precision', 'recall', 'f1']

    models.append(('SVC', SVC()))
    models.append(('DTC', DecisionTreeClassifier()))
    models.append(('KNN', KNeighborsClassifier()))
    models.append(('LDA', LinearDiscriminantAnalysis()))
    models.append(('GNB', GaussianNB()))

    df_results = pd.DataFrame(columns=['Algorithm', 'Acc Mean', 'Acc STD', 'Pre Mean', 'Pre STD', 
                                       'Rec Mean', 'Rec STD', 'F1 Mean', 'F1 STD'])
    results_acc = []
    results_pre = []
    results_rec = []
    results_f1 = []
    for name, model in models:
        names.append(name)
        kfold = StratifiedKFold(n_splits=10, shuffle=True, random_state=101)
        result = cross_validate(model, x, y, cv=kfold, scoring=scoring)
    
        # Accuracy
        acc_mean = result['test_accuracy'].mean()
        acc_std = result['test_accuracy'].std()

        # Precision
        pre_mean = result['test_precision'].mean()
        pre_std = result['test_precision'].std()

        # Recall
        rec_mean = result['test_recall'].mean()
        rec_std = result['test_recall'].std()

        #F1-Score
        f1_mean = result['test_f1'].mean()
        f1_std = result['test_f1'].std()

        df_result_row = {'Algorithm': name, 'Acc Mean': acc_mean, 'Acc STD': acc_std, 'Pre Mean': pre_mean, 
                         'Pre STD': pre_std, 'Rec Mean': rec_mean, 'Rec STD': rec_std, 'F1 Mean': f1_mean, 
                         'F1 STD': f1_std}
        
        df_results = df_results.append(df_result_row, ignore_index=True)
        results_acc.append(result['test_accuracy'])
        results_pre.append(result['test_precision'])
        results_rec.append(result['test_recall'])
        results_f1.append(result['test_f1'])

    df_results = df_results.set_index('Algorithm')
    pd.set_option('display.float_format', lambda x: '%.3f' % x)
    # Display the mean and standard deviation of all metrics for all algorithms
    print(df_results)
    
    # Display the overall results in a boxplot graph
    plot_objects = plt.subplots(nrows=1, ncols=4, figsize=(14, 6))
    fig, (ax1, ax2, ax3, ax4) = plot_objects
    
    ax1.boxplot(results_acc)
    ax1.set_title('Accuracy', fontsize=14)
    ax1.set_xticklabels(names)
    ax2.boxplot(results_pre)
    ax2.set_title('Precision', fontsize=14)
    ax2.set_xticklabels(names)
    ax3.boxplot(results_rec)
    ax3.set_title('Recall', fontsize=14)
    ax3.set_xticklabels(names)
    ax4.boxplot(results_f1)
    ax4.set_title('F1-Score', fontsize=14)
    ax4.set_xticklabels(names)

    plt.show()

h_parameters = {'max_features': ['sqrt', 'log2'], 'ccp_alpha': [0.1, 0.01, 0.001], 'max_depth': [5, 6, 7, 8], 'criterion': ['gini', 'entropy']}
model = DecisionTreeClassifier()
kfold = KFold(n_splits=5, shuffle=True, random_state=101)
grid = GridSearchCV(estimator=model, param_grid=h_parameters, cv=kfold)
grid.fit(x_train, y_train)

model = DecisionTreeClassifier(max_features='log2', max_depth=8, criterion='gini', ccp_alpha=0.001)
model.fit(x_train, y_train)