<jupyter_start><jupyter_text>0.0 Initial  0.1. Import<jupyter_code>#!pip uninstall pandas
#!pip install --upgrade pandas
#!pip install prettytable
#!pip install termcolor
#!pip install ReliefF
import pickle
import random

import pandas            as pd
import numpy             as np
import seaborn           as sns
import matplotlib.pyplot as plt
import xgboost           as xgb

from sklearn    import decomposition   as d
from matplotlib import pyplot          as plt
from sklearn    import model_selection as ms
from keras      import models          as ml
from keras      import layers          as l
from sklearn    import linear_model    as lm
#from sklearn    import metrics         as m
from scikitplot import metrics         as mt

from sklearn.svm             import SVC
from sklearn.neighbors       import KNeighborsClassifier
from sklearn.metrics         import f1_score
from sklearn.metrics         import cohen_kappa_score
from sklearn.metrics         import balanced_accuracy_score
from sklearn.metrics         import accuracy_score
from sklearn.model_selection import KFold
from sklearn.pipeline        import make_pipeline
from sklearn.tree            import DecisionTreeClassifier
from sklearn.ensemble        import RandomForestClassifier, BaggingClassifier, AdaBoostClassifier
from sklearn.pipeline        import Pipeline
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn                 import datasets
from ReliefF                 import ReliefF
from termcolor               import colored
from prettytable             import PrettyTable 
from boruta                  import BorutaPy
from keras.callbacks         import EarlyStopping
from sklearn.ensemble        import RandomForestRegressor
from sklearn.linear_model    import LinearRegression,Lasso
from numpy                   import asarray
from sklearn.preprocessing   import StandardScaler
from sklearn.preprocessing   import MinMaxScaler
from IPython.display         import Image<jupyter_output><empty_output><jupyter_text>0.2. Helper Functions<jupyter_code>def bmi (height, weight):
    bmi = weight/((height/100)**2)
    return bmi

def mean_absolute_error (y,yhat):
    return np.mean(np.abs(y-yhat))

def naive_forecast (yhat):
    n = yhat.shape[0]
    d = np.abs(np.diff(yhat)).sum()/(n-1)
    return d
    
def mean_absolute_scaled_error (y,yhat):
    return mean_absolute_error(y,yhat)/naive_forecast(yhat)

#def mean_absolute_percentage_error (y,yhat):
#    return np.mean(np.abs(y-yhat)/y)

def mean_squared_error (y,yhat):
    return np.mean(pow((yhat-y),2))

def ml_error (model_name, y, yhat):
    mae = mean_absolute_error(y,yhat)
    mase = mean_absolute_scaled_error(np.expm1(y),np.expm1(yhat))
    rmse = np.sqrt(mean_squared_error(y,yhat))
    accuracy = accuracy_score(y,yhat)
    balanced_acc = balanced_accuracy_score(y,yhat)
    kappa = cohen_kappa_score(y,yhat)

    return pd.DataFrame({'Model Name': model_name,
                        'MAE': mae,
                        'MASE': mase,
                        'RMSE': rmse, 
                        'Accuracy': accuracy,
                        'Balanced': balanced_acc,
                        'Kappa': kappa},
                        index=[0])

#calculate % people who had CVD
def perc (x):
    j = len(x)
    for i in range(len(x)):
        if i < j:
            p = ((x['total'][i])/(x['total'][i+pow(-1,(i+2))]+x['total'][i]))*100
            x['%_cardio_1'][i] = p
        else:
            exit
    return x<jupyter_output><empty_output><jupyter_text>0.3. Loading Data<jupyter_code>df_raw = pd.read_csv('cardio_train.csv', low_memory=False)
df_raw<jupyter_output><empty_output><jupyter_text>1.0. Data Description<jupyter_code>df1 = df_raw.copy()<jupyter_output><empty_output><jupyter_text>1.1. Data Dimensions<jupyter_code>print('Number of columns:', format(df1.shape[1]))
print('Number of rows:', format(df1.shape[0]))<jupyter_output>Number of columns: 13
Number of rows: 70000<jupyter_text>1.2. Data Types<jupyter_code>df1.dtypes<jupyter_output><empty_output><jupyter_text>1.3. Check NA<jupyter_code>df1.isna().sum()<jupyter_output><empty_output><jupyter_text>1.4. Unit change<jupyter_code>#age (int)
df1['age'] = (df1['age']/365).astype(int)

#weight (int)
df1['weight'] = df1['weight'].astype(int)

df1.dtypes<jupyter_output><empty_output><jupyter_text>1.5. Descriptive Statiscal<jupyter_code>#central tendency - mean, median
ct1 = pd.DataFrame(df1.apply(np.mean)).T
ct2 = pd.DataFrame(df1.apply(np.median)).T

#Dispersion - std, min, max, range, skew, kurtosis
d1 = pd.DataFrame(df1.apply(min)).T
d2 = pd.DataFrame(df1.apply(max)).T
d3 = pd.DataFrame(df1.apply(lambda x: x.max() - x.min())).T
d4 = pd.DataFrame(df1.apply(np.std)).T
d5 = pd.DataFrame(df1.apply(lambda x: x.skew())).T
d6 = pd.DataFrame(df1.apply(lambda x: x.kurtosis())).T

#concatenate
m = pd.concat([d1,d2,d3,ct1,ct2,d4,d5,d6]).T.reset_index()
m.columns = ['attributes','min','max','range','mean','median','std','skew','kurtosis']
m<jupyter_output><empty_output><jupyter_text>2.0. Feature Engineering<jupyter_code>df2 = df1.copy()
# Specify the Column Names while initializing the Table 
myTable = PrettyTable(['Type','Name', 'Category', 'Code', 'Unity']) 
  
# Add rows 
myTable.add_row(['Identification','Age','Objective Feature', 'age', 'int (days)']) 
myTable.add_row(['Identification','Height','Objective Feature', 'height', 'int (cm)']) 
myTable.add_row(['Identification','Weight','Objective Feature', 'weight', 'float (kg)'])
myTable.add_row(['Identification','Gender','Objective Feature', 'gender', '1 (women) 2 (men)'])
myTable.add_row(['Review of Systems','Systolic blood pressure','Examination Feature', 'ap_hi', 'int'])
myTable.add_row(['Review of Systems','Diastolic blood pressure','Examination Feature', 'ap_lo', 'int'])
myTable.add_row(['Review of Systems','Cholesterol','Examination Feature', 'cholesterol', '1: normal, 2: above, 3: well above'])
myTable.add_row(['Review of Systems','Glucose','Examination Feature', 'gluc', '1: normal, 2: above, 3: well above'])
myTable.add_row(['Habits','Smoking','Subjective Feature', 'smoke', 'binary'])
myTable.add_row(['Habits','Alcohol intake','Subjective Feature', 'alco', 'binary'])
myTable.add_row(['Habits','Physical activity','Subjective Feature', 'active', 'binary'])
myTable.add_row(['Target Variable','Presence/absence of CVD','Target Variable', 'cardio', 'binary'])

print(myTable)<jupyter_output>+-------------------+--------------------------+---------------------+-------------+------------------------------------+
|        Type       |           Name           |       Category      |     Code    |               Unity                |
+-------------------+--------------------------+---------------------+-------------+------------------------------------+
|   Identification  |           Age            |  Objective Feature  |     age     |             int (days)             |
|   Identification  |          Height          |  Objective Feature  |    height   |              int (cm)              |
|   Identification  |          Weight          |  Objective Feature  |    weight   |             float (kg)             |
|   Identification  |          Gender          |  Objective Feature  |    gender   |         1 (women) 2 (men)          |
| Review of Systems | Systolic blood pressure  | Examination Feature |    ap_hi    |                int                 |
| Review of Systems | Di[...]<jupyter_text>2.1 Mind Map Hypothesis<jupyter_code>Image('CCD.png')<jupyter_output><empty_output><jupyter_text>2.2 Hypothesis Creation  2.1.1 Identification Hypothesis **1.** People over 50 are on average 20% more likely to have CVD than under 50**2.** People shorter than 170 cm (5'7'') have a 5% more chance of CVDs than over 170 cm**3.** Men are 30% more likely than women to develop a CVD**4.** People over 100 kg (220 lb) have 40% more chance of having a CVD than 100 kg**5.** People with a BMI above 30 (obese class I) have 25% more chance of having a CVD than below 30  2.1.2 Habits Hypothesis **1.** People who do not engage in physical activity are 40% more likely to have CVDs than who engage**2.** People who regularly consume alcohol have a 10% chance of having CVD than those who do not consume**3.** People who smoke are 20% more likely to have CVD than who do not smoke  2.1.3 Exams Hypothesis **1.** People with well above normal cholesterol have a 50% chance of having CVD than normal**2.** People with well above normal glucose have a 20% chance of having CVD than normal**3.** People who have hypertension have 40% chance of having CVD than who have normal blood pressure  2.3. Final Hypothesis **1.** People over 50 are on average 20% more likely to have CVD than under 50**2.** People shorter than 170 cm (5'7'') have a 5% more chance of CVDs than over 170 cm**3.** Men are 30% more likely than women to develop a CVD**4.** People over 100 kg (220 lb) have 40% more chance of having a CVD than 100 kg**5.** People with a BMI above 30 (obese class I) have 25% more chance of having a CVD than below 30**6.** People who do not engage in physical activity are 40% more likely to have CVDs than who engage**7.** People who regularly consume alcohol have a 10% chance of having CVD than those who do not consume**8.** People who smoke are 20% more likely to have CVD than who do not smoke**9.** People with well above normal cholesterol have a 50% chance of having CVD than normal**10.** People with well above normal glucose have a 20% chance of having CVD than normal**11.** People who have hypertension have 40% chance of having CVD than who have normal blood pressure  2.4. Feature Engineering<jupyter_code>#BMI
df2['bmi'] = bmi(df2['height'], df2['weight'])
df2['bmi'] = df2['bmi'].astype(int)
#blood pressure classification
df2['blood_pressure'] = ''

df2['blood_pressure'] = df2.apply(lambda x: '0' if (x['ap_hi'] > 0 and x['ap_hi'] < 120) and (x['ap_lo'] > 0 and x['ap_lo'] < 80) else '' 
                                  '1' if (x['ap_hi'] >= 120 and x['ap_hi'] <= 129) and (x['ap_lo'] < 80) else ''
                                  '2' if (x['ap_hi'] >= 130 and x['ap_hi'] <= 139) or (x['ap_lo'] >= 80 and x['ap_lo'] <= 89) else ''
                                  '3' if (x['ap_hi'] >= 140 and x['ap_hi'] <= 179) or (x['ap_lo'] >= 90 and x['ap_lo'] <= 119) else ''
                                  '4' if x['ap_hi'] >= 180 or x['ap_lo'] >= 120 else '', axis=1)

#remove na
df2['blood_pressure'] = df2['blood_pressure'].replace('',  )
df2['blood_pressure'] = df2['blood_pressure'].dropna()

#transform to (int)
df2['blood_pressure'] = pd.to_numeric(df2['blood_pressure'],errors = 'coerce')
df2.dtypes<jupyter_output><empty_output><jupyter_text>3.0. Data Filtering<jupyter_code>df3 = df2.copy()
df3_full = df2.copy()
#blood pressure
#df3 = df3[~df3['blood_pressure'].isna()]

#height
#df3 = df3[(df3['height'] >= 140) & (df3['height'] <= 200)]

#weight
#df3 = df3[(df3['weight'] >= 50) & (df3['weight'] <= 150)]

#ap_hi
#df3 = df3[(df3['ap_hi'] >= 0) & (df3['ap_hi'] <= 250)]

#ap_lo
#df3 = df3[(df3['ap_lo'] >= 0) & (df3['ap_lo'] <= 250)]
#central tendency - mean, median
ct1 = pd.DataFrame(df3.apply(np.mean)).T
ct2 = pd.DataFrame(df3.apply(np.median)).T

#Dispersion - std, min, max, range, skew, kurtosis
d1 = pd.DataFrame(df3.apply(min)).T
d2 = pd.DataFrame(df3.apply(max)).T
d3 = pd.DataFrame(df3.apply(lambda x: x.max() - x.min())).T
d4 = pd.DataFrame(df3.apply(np.std)).T
d5 = pd.DataFrame(df3.apply(lambda x: x.skew())).T
d6 = pd.DataFrame(df3.apply(lambda x: x.kurtosis())).T
d7 = pd.DataFrame(df3.apply(lambda x: len(x))).T

#concatenate
m = pd.concat([d1,d2,d3,ct1,ct2,d4,d5,d6,d7]).T.reset_index()
m.columns = ['attributes','min','max','range','mean','median','std','skew','kurtosis','total']
m
#calculate summary statistics
data_mean, data_std = m['mean'][3], m['std'][3]
#identify outliers
cut_off = data_std * 3
lower, upper = data_mean - cut_off, data_mean + cut_off
#identify outliers
df3['outliers'] = ''
df3['outliers'] = df3.apply(lambda x: 'outlier' if (x['height'] < lower or x['height'] > upper) else '', axis=1)

#remove outliers
df3 = df3[(df3['outliers'] != 'outlier')]
#calculate summary statistics
data_mean, data_std = m['mean'][4], m['std'][4]
#identify outliers
cut_off = data_std * 3
lower, upper = data_mean - cut_off, data_mean + cut_off
#identify outliers
df3['outliers'] = ''
df3['outliers'] = df3.apply(lambda x: 'outlier' if (x['weight'] < lower or x['weight'] > upper) else '', axis=1)

#remove outliers
df3 = df3[(df3['outliers'] != 'outlier')]
#calculate summary statistics
data_mean, data_std = m['mean'][5], m['std'][5]
#identify outliers
cut_off = data_std * 3
lower, upper = data_mean - cut_off, data_mean + cut_off
#identify outliers
df3['outliers'] = ''
df3['outliers'] = df3.apply(lambda x: 'outlier' if (x['ap_hi'] < lower or x['ap_hi'] > upper) else '', axis=1)

#remove outliers
df3 = df3[(df3['outliers'] != 'outlier')]
#calculate summary statistics
data_mean, data_std = m['mean'][6], m['std'][6]
#identify outliers
cut_off = data_std * 3
lower, upper = data_mean - cut_off, data_mean + cut_off
#identify outliers
df3['outliers'] = ''
df3['outliers'] = df3.apply(lambda x: 'outlier' if (x['ap_lo'] < lower or x['ap_lo'] > upper) else '', axis=1)

#remove outliers
df3 = df3[(df3['outliers'] != 'outlier')]
#calculate summary statistics
data_mean, data_std = m['mean'][13], m['std'][13]
#identify outliers
cut_off = data_std * 3
lower, upper = data_mean - cut_off, data_mean + cut_off
#identify outliers
df3['outliers'] = ''
df3['outliers'] = df3.apply(lambda x: 'outlier' if (x['bmi'] < lower or x['bmi'] > upper) else '', axis=1)

#remove outliers
df3 = df3[(df3['outliers'] != 'outlier')]

df3 = df3.drop('outliers', 1)
#central tendency - mean, median
ct1 = pd.DataFrame(df3.apply(np.mean)).T
ct2 = pd.DataFrame(df3.apply(np.median)).T

#Dispersion - std, min, max, range, skew, kurtosis
d1 = pd.DataFrame(df3.apply(min)).T
d2 = pd.DataFrame(df3.apply(max)).T
d3 = pd.DataFrame(df3.apply(lambda x: x.max() - x.min())).T
d4 = pd.DataFrame(df3.apply(np.std)).T
d5 = pd.DataFrame(df3.apply(lambda x: x.skew())).T
d6 = pd.DataFrame(df3.apply(lambda x: x.kurtosis())).T
d7 = pd.DataFrame(df3.apply(lambda x: len(x))).T

#concatenate
m = pd.concat([d1,d2,d3,ct1,ct2,d4,d5,d6,d7]).T.reset_index()
m.columns = ['attributes','min','max','range','mean','median','std','skew','kurtosis','total']
m<jupyter_output><empty_output><jupyter_text>4.0. Exploratory Data Analysis<jupyter_code>df4 = df3.copy()<jupyter_output><empty_output><jupyter_text>4.1 Univariate Analysis  4.1.1 Response variable<jupyter_code>#sns.displot(df4['cardio'], kde=True)<jupyter_output><empty_output><jupyter_text>4.1.2 Numeric variable<jupyter_code>df42 = df4.drop('id', axis=1)
df42.hist(figsize=(24,12), bins=50);<jupyter_output><empty_output><jupyter_text>4.2. Bivariate Analysis **1. Analysis between 39 - 64. People over 50 are on average 20% more likely to have CVD than under 50****TRUE** On average people over 50 are 56.6% likely to have CVD and 20.9% more likely than under 50. 63 years old has the highest incidence.<jupyter_code>#cvd per age
aux1 = df4[['age', 'cardio']].groupby('age').sum().reset_index().rename(columns={0:'total'})

#histogram
fig_dims = (15, 7)
fig, ax = plt.subplots(figsize=fig_dims)
sns.barplot(x='age', y='cardio', ax=ax, data=aux1)
plt.title('People who had CVD per age')
plt.show()
#% people with CVD per age
aux1 = df4[(df4['age'] >= 39)]
aux1 = aux1[['age', 'cardio']].groupby(['age','cardio']).size().reset_index().rename(columns={0:'total'})
aux1['%_cardio_1'] = ""
aux1 = perc(aux1)
#divide bewtween two groups under and over 50
aux1 = aux1[(aux1['cardio'] >= 1)]
aux_50 = aux1[(aux1['age'] >= 50)]
aux_50 = pd.DataFrame(aux_50.apply(np.mean)).T

aux_49 = aux1[(aux1['age'] < 50)]
aux_49 = pd.DataFrame(aux_49.apply(np.mean)).T

#result of average
m = pd.concat([aux_50, aux_49]).T.reset_index()
m.columns = ['index','over 50','under 50']
m['index'] = {'age': 0, 'cardio': 1, 'total': 2, 'average': 3}
m
#histogram people who had CVD per age
fig_dims = (18, 10)
fig, ax = plt.subplots(figsize=fig_dims)

graph = sns.barplot(x='age', y='%_cardio_1', ax=ax, data=aux1)
graph.axvline(10.5, ymin=0.0, ymax=1.0, linestyle ="--", color='blue')
graph.axhline(51, xmin=0.0, xmax=1.0, linestyle ="--", color='red')
plt.title('% People who had CVD per age')
plt.show()<jupyter_output><empty_output><jupyter_text>**2. Analysis between 140 - 193 cm. People shorter than 170 cm (5'7'') have a 5% more chance of CVDs than over 170 cm****FALSE** On average people shorter than 170 cm have 4.5% more likely to have CVD than over 170.<jupyter_code>#cvd per height
aux1 = df4[(df4['height'] >= 140) & (df4['height'] <= 193)]
aux1 = aux1[['height', 'cardio']].groupby('height').sum().reset_index()

#histogram
fig_dims = (18, 10)
fig, ax = plt.subplots(figsize=fig_dims)
sns.barplot(x='height', y='cardio', ax=ax, data=aux1)
plt.title('People who had CVD per height (cm)')
plt.show()
#filter height 140 - 190 and divide between two groups
#aux1 = df4[(df4['cardio'] >= 1) & (df4['height'] >= 140) & (df4['height'] <= 190)]
#aux1 = aux1[['height', 'cardio']].groupby('height').size().reset_index().rename(columns={0:'total_1'})

#aux2 = df4[(df4['cardio'] <= 0) & (df4['height'] >= 140) & (df4['height'] <= 190)]
#aux2 = aux2[['height', 'cardio']].groupby('height').size().reset_index().rename(columns={0:'total_0'})

#fig = plt.figure(figsize=(10,10))

#ax = fig.add_subplot(111)

#width = 0.2

#aux1.plot(x='height', y='total_1', kind='bar', color='red', ax=ax, width=width, position=0)
#aux2.plot(x='height', y='total_0', kind='bar', color='blue', ax=ax, width=width, position=1)
#plt.show()
#filtering and calculate % people with CVD per height
aux1 = df4[(df4['height'] >= 135) & (df4['height'] <= 193)]
aux1 = aux1[['height', 'cardio']].groupby(['height','cardio']).size().reset_index().rename(columns={0:'total'})
aux1['%_cardio_1'] = ""
aux1 = perc(aux1)
#divide bewtween two groups below and over 170
aux1 = aux1[(aux1['cardio'] >= 1)]
aux_170 = aux1[(aux1['height'] >= 170)]
aux_170 = pd.DataFrame(aux_170.apply(np.mean)).T

aux_169 = aux1[(aux1['height'] < 170)]
aux_169 = pd.DataFrame(aux_169.apply(np.mean)).T

#result of average
m = pd.concat([aux_170, aux_169]).T.reset_index()
m.columns = ['index','over 170','below 169']
m['index'] = {'age': 0, 'cardio': 1, 'total': 2, 'average': 3}
m
#histogram people who had CVD per height
fig_dims = (18, 10)
fig, ax = plt.subplots(figsize=fig_dims)

graph = sns.barplot(x='height', y='%_cardio_1', ax=ax, data=aux1)
graph.axvline(29.5, ymin=0.0, ymax=1.0, linestyle ="--", color='blue')
graph.axhline(51, xmin=0.0, xmax=1.0, linestyle ="--", color='red')
plt.title('% People who had CVD per height')
plt.show()<jupyter_output><empty_output><jupyter_text>**3. Men are 30% more likely than women to develop a CVD****FALSE** Although men had more CVD than women, the gap are lesser than 30%. Men had 50,1% of cases of CVD, while women had 49,6%<jupyter_code>#transform to categorical
df42 = df4.copy()
df42['gender'] = df42['gender'].apply({1:'women', 2:'men'}.get)
#cvd per height
aux1 = df42[(df42['cardio'] >= 1)]
aux1 = aux1[['gender', 'cardio']].groupby('gender').size().reset_index().rename(columns={0:'total_1'})

aux2 = df42[(df42['cardio'] <= 0)]
aux2 = aux2[['gender', 'cardio']].groupby('gender').size().reset_index().rename(columns={0:'total_0'})

#histogram
fig = plt.figure(figsize=(10,10))

ax = fig.add_subplot(111)

width = 0.2

aux1.plot(x='gender', y='total_1', kind='bar', color='red', ax=ax, width=width, position=0)
aux2.plot(x='gender', y='total_0', kind='bar', color='blue', ax=ax, width=width, position=1)
plt.show()
#calculate % people with CVD per gender
aux1 = df42[['gender', 'cardio']].groupby(['gender','cardio']).size().reset_index().rename(columns={0:'total'})
aux1['%_cardio_1'] = ""
aux1 = perc(aux1)

#result of %
aux1 = aux1[(aux1['cardio'] >= 1)]
aux1
#histogram people who had CVD per gender (average)
fig_dims = (6, 8)
fig, ax = plt.subplots(figsize=fig_dims)

graph = sns.barplot(x='gender', y='%_cardio_1', ax=ax, data=aux1)
graph.axhline(51, xmin=0.0, xmax=1.0, linestyle ="--", color='red')
plt.title('% People who had CVD per active')
plt.show()<jupyter_output><empty_output><jupyter_text>**4. Analysis between 50 - 130. People over 100 kg (220 lb) have 40% more chance of having a CVD than 100 kg****FALSE** On average people with over 100 kg have 19.9% more chance of having a CVD than below 100 kg<jupyter_code>#cvd per weight
aux1 = df4[(df4['weight'] >= 50) & (df4['weight'] <= 130)]
aux1 = aux1[['weight', 'cardio']].groupby('weight').sum().reset_index()

#histogram
fig_dims = (18, 10)
fig, ax = plt.subplots(figsize=fig_dims)
sns.barplot(x='weight', y='cardio', ax=ax, data=aux1)
#filter weight 33 - 100 and divide between two groups
#aux1 = df4[(df4['weight'] >= 33) & (df4['weight'] <= 100) & (df4['cardio'] >= 1)]
#aux1 = aux1[['weight', 'cardio']].groupby('weight').size().reset_index().rename(columns={0:'total_1'})

#aux2 = df4[(df4['weight'] >= 33) & (df4['weight'] <= 100) & (df4['cardio'] <= 0)]
#aux2 = aux2[['weight', 'cardio']].groupby('weight').size().reset_index().rename(columns={0:'total_0'})
#filtering and calculate % people with CVD per weight
aux1 = df4[(df4['weight'] >= 50) & (df4['weight'] <= 130)]
aux1 = aux1[['weight', 'cardio']].groupby(['weight','cardio']).size().reset_index().rename(columns={0:'total'})
aux1['%_cardio_1'] = ""
aux1 = perc(aux1)
#divide bewtween two groups below and over 100 kg
aux1 = aux1[(aux1['cardio'] >=1)]
aux_100 = aux1[(aux1['weight'] >= 100)]
aux_100 = pd.DataFrame(aux_100.apply(np.mean)).T

aux_99 = aux1[(aux1['weight'] < 100)]
aux_99 = pd.DataFrame(aux_99.apply(np.mean)).T

#result of average
m = pd.concat([aux_100, aux_99]).T.reset_index()
m.columns = ['index','over 100','below 100']
m['index'] = {'age': 0, 'cardio': 1, 'total': 2, 'average': 3}
m
#histogram people who had CVD per height
fig_dims = (20,15)
fig, ax = plt.subplots(figsize=fig_dims)

graph = sns.barplot(x='weight', y='%_cardio_1', ax=ax, data=aux1)
graph.axvline(49.5, ymin=0.0, ymax=1.0, linestyle ="--", color='blue')
graph.axhline(51, xmin=0.0, xmax=1.0, linestyle ="--", color='red')
plt.title('% People who had CVD per BMI')
plt.show()<jupyter_output><empty_output><jupyter_text>**5. People with a BMI above 30 (obese class I) have 25% more chance of having a CVD than below 30****TRUE** People with a BMI above than 30 have 26.3% chance of having CVD than BMI below 30<jupyter_code>#convert type bmi (int)
df4['bmi'] = df4['bmi'].astype(int)
#cvd per weight
aux1 = df4[(df4['bmi'] >= 15) & (df4['bmi'] <= 50)]
aux1 = aux1[['bmi', 'cardio']].groupby('bmi').sum().reset_index()

#histogram
fig_dims = (18, 10)
fig, ax = plt.subplots(figsize=fig_dims)
sns.barplot(x='bmi', y='cardio', ax=ax, data=aux1)
#calculate % people with CVD per bmi
aux1 = df4[(df4['bmi'] >= 15) & (df4['bmi'] <= 50)]
aux1 = aux1[['bmi', 'cardio']].groupby(['bmi','cardio']).size().reset_index().rename(columns={0:'total'})
aux1['%_cardio_1'] = ""
aux1 = perc(aux1)
#divide between two groups below and over 30
aux1 = aux1[(aux1['cardio'] >=1)]
aux_30 = aux1[(aux1['bmi'] >= 30)]
aux_30 = pd.DataFrame(aux_30.apply(np.mean)).T

aux_29 = aux1[(aux1['bmi'] < 30)]
aux_29 = pd.DataFrame(aux_29.apply(np.mean)).T

#result of average
m = pd.concat([aux_30, aux_29]).T.reset_index()
m.columns = ['index','over 30','above 30']
m['index'] = {'age': 0, 'cardio': 1, 'total': 2, 'average': 3}
m
#histogram people who had CVD per BMI
fig_dims = (18, 10)
fig, ax = plt.subplots(figsize=fig_dims)

graph = sns.barplot(x='bmi', y='%_cardio_1', ax=ax, data=aux1)
graph.axvline(14.5, ymin=0.0, ymax=1.0, linestyle ="--", color='blue')
graph.axhline(51, xmin=0.0, xmax=1.0, linestyle ="--", color='red')
plt.title('% People who had CVD per BMI')
plt.show()<jupyter_output><empty_output><jupyter_text>**6. People who do not engage in physical activity are 40% more likely to have CVDs than who engage****FALSE** People who engage in physical activity more have 4.6% chance of having CVD<jupyter_code>#transform to categorical
df42 = df4.copy()
df42['active'] = df42['active'].apply({0:'no', 1:'yes'}.get)
#cvd per physical activity
aux1 = df42[(df42['cardio'] >= 1)]
aux1 = aux1[['active', 'cardio']].groupby('active').size().reset_index().rename(columns={0:'total_1'})

aux2 = df42[(df42['cardio'] <= 0)]
aux2 = aux2[['active', 'cardio']].groupby('active').size().reset_index().rename(columns={0:'total_0'})

#histogram
fig = plt.figure(figsize=(20,15))

ax = fig.add_subplot(111)

width = 0.2

aux1.plot(x='active', y='total_1', kind='bar', color='red', ax=ax, width=width, position=0)
aux2.plot(x='active', y='total_0', kind='bar', color='blue', ax=ax, width=width, position=1)
plt.show()
#calculate % people with CVD per activity
aux1 = df4[['active', 'cardio']].groupby(['active','cardio']).size().reset_index().rename(columns={0:'total'})
aux1['%_cardio_1'] = ""
aux1 = perc(aux1)

#result of %
aux1 = aux1[(aux1['cardio'] >= 1)]
aux1['active'] = aux1['active'].apply({0:'no', 1:'yes'}.get)
aux1
#histogram people who had CVD per activity
fig_dims = (6, 8)
fig, ax = plt.subplots(figsize=fig_dims)

graph = sns.barplot(x='active', y='%_cardio_1', ax=ax, data=aux1)
#graph.axvline(29.5, ymin=0.0, ymax=1.0, linestyle ="--", color='blue')
graph.axhline(51, xmin=0.0, xmax=1.0, linestyle ="--", color='red')
plt.title('% People who had CVD per active')
plt.show()<jupyter_output><empty_output><jupyter_text>**7. People who regularly consume alcohol have a 10% chance of having CVD than those who do not consume****FALSE** People who regularly consume alcohol have 1.9% less chance of having CVD than who don't consume<jupyter_code>#transform to categorical
df42 = df4.copy()
df42['alco'] = df42['alco'].apply({0:'no', 1:'yes'}.get)
#cvd per people who consume alcohol
aux1 = df42[(df42['cardio'] >= 1)]
aux1 = aux1[['alco', 'cardio']].groupby('alco').size().reset_index().rename(columns={0:'total_1'})

aux2 = df42[(df42['cardio'] <= 0)]
aux2 = aux2[['alco', 'cardio']].groupby('alco').size().reset_index().rename(columns={0:'total_0'})

#histogram
fig = plt.figure(figsize=(10,10))

ax = fig.add_subplot(111)

width = 0.2

aux1.plot(x='alco', y='total_1', kind='bar', color='red', ax=ax, width=width, position=0)
aux2.plot(x='alco', y='total_0', kind='bar', color='blue', ax=ax, width=width, position=1)
plt.show()
#calculate % people with CVD per people who consume alcohol
aux1 = df42[['alco', 'cardio']].groupby(['alco','cardio']).size().reset_index().reset_index().rename(columns={0:'total'})
aux1['%_cardio_1'] = ""
aux1 = perc(aux1)

#result of %
aux1 = aux1[(aux1['cardio'] >= 1)]
aux1
#histogram people who had CVD per per people who consume alcohol
fig_dims = (6, 8)
fig, ax = plt.subplots(figsize=fig_dims)

graph = sns.barplot(x='alco', y='%_cardio_1', ax=ax, data=aux1)
#graph.axvline(29.5, ymin=0.0, ymax=1.0, linestyle ="--", color='blue')
graph.axhline(51, xmin=0.0, xmax=1.0, linestyle ="--", color='red')
plt.title('% People who had CVD per alco')
plt.show()<jupyter_output><empty_output><jupyter_text>**8. People who smoke are 20% more likely to have CVD than who do not smoke****FALSE** People who smoke have 3% less chance of having CVD than who doesn't<jupyter_code>#transform to categorical
df42 = df4.copy()
df42['smoke'] = df42['smoke'].apply({0:'no', 1:'yes'}.get)
#cvd per people who smoke
aux1 = df42[(df42['cardio'] >= 1)]
aux1 = aux1[['smoke', 'cardio']].groupby('smoke').size().reset_index().rename(columns={0:'total_1'})

aux2 = df42[(df42['cardio'] <= 0)]
aux2 = aux2[['smoke', 'cardio']].groupby('smoke').size().reset_index().rename(columns={0:'total_0'})

#histogram
fig = plt.figure(figsize=(10,10))

ax = fig.add_subplot(111)

width = 0.2

aux1.plot(x='smoke', y='total_1', kind='bar', color='red', ax=ax, width=width, position=0)
aux2.plot(x='smoke', y='total_0', kind='bar', color='blue', ax=ax, width=width, position=1)
plt.show()
#calculate % people with CVD per people who smoke
aux1 = df42[['smoke', 'cardio']].groupby(['smoke','cardio']).size().reset_index().rename(columns={0:'total'})
aux1['%_cardio_1'] = ""
aux1 = perc(aux1)

#result of %
aux1 = aux1[(aux1['cardio'] >= 1)]
aux1
#histogram people who had CVD per people who smoke
fig_dims = (6, 8)
fig, ax = plt.subplots(figsize=fig_dims)

graph = sns.barplot(x='smoke', y='%_cardio_1', ax=ax, data=aux1)
#graph.axvline(29.5, ymin=0.0, ymax=1.0, linestyle ="--", color='blue')
graph.axhline(51, xmin=0.0, xmax=1.0, linestyle ="--", color='red')
plt.title('% People who had CVD per smoke')
plt.show()<jupyter_output><empty_output><jupyter_text>**9. People with well above normal cholesterol have a 50% chance of having CVD than normal****FALSE** People with well above normal cholesterol have 33% likely than people with normal<jupyter_code>#transform to categorical
df42 = df4.copy()
df42['cholesterol'] = df42['cholesterol'].apply({1:'normal', 2:'above normal', 3:'well above normal'}.get)
#cvd per people who have above normal cholesterol
aux1 = df42[(df42['cardio'] >= 1)]
aux1 = aux1[['cholesterol', 'cardio']].groupby('cholesterol').size().reset_index().rename(columns={0:'total_1'})

aux2 = df42[(df42['cardio'] <= 0)]
aux2 = aux2[['cholesterol', 'cardio']].groupby('cholesterol').size().reset_index().rename(columns={0:'total_0'})

#histogram
fig = plt.figure(figsize=(10,10))

ax = fig.add_subplot(111)

width = 0.2

aux1.plot(x='cholesterol', y='total_1', kind='bar', color='red', ax=ax, width=width, position=0)
aux2.plot(x='cholesterol', y='total_0', kind='bar', color='blue', ax=ax, width=width, position=1)
plt.show()
#calculate % people with CVD per people who have above normal cholesterol
aux1 = df42[['cholesterol', 'cardio']].groupby(['cholesterol','cardio']).size().reset_index().rename(columns={0:'total'})
aux1['%_cardio_1'] = ""
aux1 = perc(aux1)

#result of %
aux1 = aux1[(aux1['cardio'] >= 1)]
aux1
#histogram people who had CVD per people who have above normal cholesterol
fig_dims = (6, 8)
fig, ax = plt.subplots(figsize=fig_dims)

graph = sns.barplot(x='cholesterol', y='%_cardio_1', ax=ax, data=aux1)
#graph.axvline(29.5, ymin=0.0, ymax=1.0, linestyle ="--", color='blue')
graph.axhline(51, xmin=0.0, xmax=1.0, linestyle ="--", color='red')
plt.title('% People who had CVD per cholesterol')
plt.show()<jupyter_output><empty_output><jupyter_text>**10. People with well above normal glucose have a 20% chance of having CVD than normal****FALSE** People with well above normal glucose have 14.1% more likely than people with normal glucose<jupyter_code>#transform to categorical
df42 = df4.copy()
df42['gluc'] = df42['gluc'].apply({1:'normal', 2:'above normal', 3:'well above normal'}.get)
#cvd per people who have above normal glucose
aux1 = df42[(df42['cardio'] >= 1)]
aux1 = aux1[['gluc', 'cardio']].groupby('gluc').size().reset_index().rename(columns={0:'total_1'})

aux2 = df42[(df42['cardio'] <= 0)]
aux2 = aux2[['gluc', 'cardio']].groupby('gluc').size().reset_index().rename(columns={0:'total_0'})

#histogram
fig = plt.figure(figsize=(10,10))

ax = fig.add_subplot(111)

width = 0.2

aux1.plot(x='gluc', y='total_1', kind='bar', color='red', ax=ax, width=width, position=0)
aux2.plot(x='gluc', y='total_0', kind='bar', color='blue', ax=ax, width=width, position=1)
plt.show()
#calculate % people with CVD per people who have above normal glucose
aux1 = df42[['gluc', 'cardio']].groupby(['gluc','cardio']).size().reset_index().rename(columns={0:'total'})
aux1['%_cardio_1'] = ""
aux1 = perc(aux1)

#result of %
aux1 = aux1[(aux1['cardio'] >= 1)]
aux1
#histogram people who had CVD per people who have above normal glucose
fig_dims = (6, 8)
fig, ax = plt.subplots(figsize=fig_dims)

graph = sns.barplot(x='gluc', y='%_cardio_1', ax=ax, data=aux1)
#graph.axvline(29.5, ymin=0.0, ymax=1.0, linestyle ="--", color='blue')
graph.axhline(51, xmin=0.0, xmax=1.0, linestyle ="--", color='red')
plt.title('% People who had CVD per gluc')
plt.show()<jupyter_output><empty_output><jupyter_text>**11. People who have hypertension have 40% chance of having CVD than who have normal blood pressure****TRUE** People with hypertension 3 have 54.2% more chance of having a CVD than people with normal blood pressure<jupyter_code>#cvd per people who have hypertension
aux1 = df4[(df4['cardio'] >= 1)]
aux1 = aux1[['blood_pressure', 'cardio']].groupby('blood_pressure').size().reset_index().rename(columns={0:'total_1'})


aux2 = df4[(df4['cardio'] <= 0)]
aux2 = aux2[['blood_pressure', 'cardio']].groupby('blood_pressure').size().reset_index().rename(columns={0:'total_0'})

#histogram
fig = plt.figure(figsize=(10,10))

ax = fig.add_subplot(111)

width = 0.2

aux1.plot(x='blood_pressure', y='total_1', kind='bar', color='red', ax=ax, width=width, position=0)
aux2.plot(x='blood_pressure', y='total_0', kind='bar', color='blue', ax=ax, width=width, position=1)
plt.show()
#calculate % people with CVD per people who have hypertension
aux1 = df4[['blood_pressure', 'cardio']].groupby(['blood_pressure','cardio']).size().reset_index().rename(columns={0:'total'})
aux1['%_cardio_1'] = ""
aux1 = perc(aux1)

#result of %
aux1 = aux1[(aux1['cardio'] >= 1)]
aux1
#histogram people who had CVD per people who have hypertension
fig_dims = (10, 8)
fig, ax = plt.subplots(figsize=fig_dims)

graph = sns.barplot(x='blood_pressure', y='%_cardio_1', ax=ax, data=aux1)
#graph.axvline(29.5, ymin=0.0, ymax=1.0, linestyle ="--", color='blue')
graph.axhline(51, xmin=0.0, xmax=1.0, linestyle ="--", color='red')
plt.title('% People who had CVD per blood_pressure')
plt.show()<jupyter_output><empty_output><jupyter_text>4.3. Multivariate Analysis<jupyter_code>correlations = df4.corr(method='pearson')
mask = np.zeros_like(correlations)
mask[np.triu_indices_from(mask)] = True
#fig_dims = (15, 7)
#fig, ax = plt.subplots(figsize=fig_dims)
#sns.heatmap(correlations,ax=ax, annot=True);

with sns.axes_style("white"):
    f, ax = plt.subplots(figsize=(15, 7))
    ax = sns.heatmap(correlations, mask=mask, vmax=.3, annot=True)<jupyter_output><empty_output><jupyter_text>5.0. Data Preparation<jupyter_code>df5 = df4.copy()<jupyter_output><empty_output><jupyter_text>5.1. Normalization<jupyter_code># define min max scaler
scaler_mm = MinMaxScaler()
scaler_std = StandardScaler()

# transform weight - STD
df5['weight'] = scaler_mm.fit_transform(df5[['weight']].values)

# transform height - STD
df5['height'] = scaler_mm.fit_transform(df5[['height']].values)

# transform age - MM
df5['age'] = scaler_mm.fit_transform(df5[['age']].values)

# transform bmi - MM
df5['bmi'] = scaler_mm.fit_transform(df5[['bmi']].values)

# transform ap_hi - MM
df5['ap_hi'] = scaler_mm.fit_transform(df5[['ap_hi']].values)

# transform ap_lo - MM
df5['ap_lo'] = scaler_mm.fit_transform(df5[['ap_lo']].values)

df5<jupyter_output><empty_output><jupyter_text>6.0. Feature Selection<jupyter_code>df6 = df5.copy()
df6_full = df3_full.copy()
#drop id, ap_hi, ap_lo
df6 = df6.drop(['id'], axis=1)
df6_full = df6_full.drop(['id'], axis=1)
df6_boruta = df6['ap_hi']
df6_relief = df6[['gender', 'ap_hi', 'ap_lo', 'cholesterol', 'gluc',
                        'smoke', 'alco', 'active', 'bmi', 'blood_pressure']]
df6_lasso = df6[['age', 'ap_hi', 'blood_pressure']]

X = df6.drop(['cardio'], axis=1)
Y = df6['cardio'].copy()

X_full = df6_full.drop(['cardio'], axis=1)
Y_full = df6_full['cardio'].copy()

X_boruta = df6_boruta
Y_boruta = df6['cardio'].copy()

X_relief = df6_relief
Y_relief = df6['cardio'].copy()

X_lasso = df6_lasso
Y_lasso = df6['cardio'].copy()
#split dataset into train and test
x_train, x_test, y_train, y_test = ms.train_test_split(X, Y, test_size=0.2, random_state=42)

#split dataset into train and test (with outlier)
x_train_full, x_test_full, y_train_full, y_test_full = ms.train_test_split(X_full, Y_full, test_size=0.2, random_state=42)

#split dataset into train and test (boruta)
x_train_boruta, x_test_boruta, y_train_boruta, y_test_boruta = ms.train_test_split(X_boruta, Y_boruta, test_size=0.2, random_state=42)

#split dataset into train and test (relief)
x_train_relief, x_test_relief, y_train_relief, y_test_relief = ms.train_test_split(X_relief, Y_relief, test_size=0.2, random_state=42)

#split dataset into train and test (lasso)
x_train_lasso, x_test_lasso, y_train_lasso, y_test_lasso = ms.train_test_split(X_lasso, Y_lasso, test_size=0.2, random_state=42)<jupyter_output><empty_output><jupyter_text>6.1. Boruta as feature selector<jupyter_code>#training and test dataset for Boruta
x_train_n = x_train.values
y_train_n = y_train.values.ravel()

#define RandomForestRegressor
rf = RandomForestRegressor(n_jobs=-1)

#define Boruta
boruta = BorutaPy(rf, n_estimators='auto',verbose=2,random_state=42).fit(x_train_n,y_train_n)
cols_selected = boruta.support_.tolist()

#best feature
x_train_fs = x_train
cols_selected_boruta = x_train_fs.iloc[:,cols_selected].columns.to_list()
print('Columns selected: ',cols_selected_boruta)

#not selected from Boruta
cols_not_selected_boruta = list(np.setdiff1d(x_train_fs.columns,cols_selected_boruta))
print('Columns not selected: ',cols_not_selected_boruta)<jupyter_output>Columns selected:  ['ap_hi']
Columns not selected:  ['active', 'age', 'alco', 'ap_lo', 'blood_pressure', 'bmi', 'cholesterol', 'gender', 'gluc', 'height', 'smoke', 'weight']<jupyter_text>6.2. Relieff Selection<jupyter_code>#pipeline = Pipeline([('scaler',ReliefF()),
#                     ('model',RandomForestClassifier())])
#clf = make_pipeline(ReliefF(),
#                    RandomForestClassifier())
#kfold = KFold(n_splits=2, random_state=42,shuffle=True)

#parameters = {'randomforestclassifier__n_estimators': [4, 6, 9], 
#              'randomforestclassifier__max_features': ['log2', 'sqrt','auto'],
 #             'randomforestclassifier__criterion': ['entropy', 'gini'], 
 #             'randomforestclassifier__max_depth': [2, 3, 5, 10], 
  #            'randomforestclassifier__min_samples_split': [2, 3, 5],
   #           'randomforestclassifier__min_samples_leaf': [1,5,8] }

#grid_RF=GridSearchCV(clf,param_grid=parameters,
#        scoring='accuracy',cv=kfold)
#grid_RF = grid_RF.fit(x_train, y_train)
#grid_RF.best_score_
fs = ReliefF(n_neighbors=100, n_features_to_keep=10)
x_train_relieff = fs.fit_transform(X.values, Y.values)
print("(No. of tuples, No. of Columns before ReliefF) : "+str(df6.shape[0])+
      "\n(No. of tuples, No. of Columns after ReliefF) : "+str(x_train_relieff.shape))
x_train_relieff[3]
cols_selected_relief = ['gender', 'ap_hi', 'ap_lo', 'cholesterol', 'gluc',
                        'smoke', 'alco', 'active', 'bmi', 'blood_pressure']<jupyter_output><empty_output><jupyter_text>6.3. Lasso Selection<jupyter_code>df6_ls = df6.drop('cardio', 1)
features = df6_ls.columns
pipeline = Pipeline([('scaler',StandardScaler()),('model',Lasso())])
search = GridSearchCV(pipeline,{'model__alpha':np.arange(0.1,10,0.1)},
                      cv = 5, scoring="neg_mean_squared_error",verbose=0)

search.fit(x_train,y_train)
search.best_params_
coefficients = search.best_estimator_.named_steps['model'].coef_
importance = np.abs(coefficients)
cols_selected_lasso = np.array(features)[importance > 0]
print('Columns selected: ', cols_selected_lasso)

cols_not_selected_lasso = np.array(features)[importance == 0]
print('Columns not selected: ', cols_not_selected_lasso)<jupyter_output>Columns selected:  ['age' 'ap_hi' 'blood_pressure']
Columns not selected:  ['gender' 'height' 'weight' 'ap_lo' 'cholesterol' 'gluc' 'smoke' 'alco'
 'active' 'bmi']<jupyter_text>7.0. Machine Learning Modeling  7.1. Baseline Model<jupyter_code>#df without outlier
response_list = y_test.drop_duplicates().sort_values().tolist()
size = x_test.shape[0]
weights = df6['cardio'].value_counts(normalize=True).sort_index().tolist()

# prediction
yhat_random = random.choices(response_list,k=size,weights=weights)

#performance
random_result = ml_error('Baseline', y_test, yhat_random)
random_result
#confusion matrix
mt.plot_confusion_matrix(y_test, yhat_random,  cmap=plt.cm.viridis, normalize=False, figsize=(12,12))
#df full
response_list = y_test_full.drop_duplicates().sort_values().tolist()
size = x_test_full.shape[0]
weights = df6_full['cardio'].value_counts(normalize=True).sort_index().tolist()

# prediction
yhat_random_full = random.choices(response_list,k=size,weights=weights)

#performance
random_result_full = ml_error('Baseline', y_test_full, yhat_random_full)
random_result_full
#confusion matrix
mt.plot_confusion_matrix(y_test_full, yhat_random_full,  cmap=plt.cm.viridis, normalize=False, figsize=(12,12))
#boruta
response_list = y_test_boruta.drop_duplicates().sort_values().tolist()
size = x_test_boruta.shape[0]
weights = df6['cardio'].value_counts(normalize=True).sort_index().tolist()

# prediction
yhat_random_boruta = random.choices(response_list,k=size,weights=weights)

#performance
random_result_boruta = ml_error('Baseline', y_test_boruta, yhat_random_boruta)
random_result_boruta
#confusion matrix
mt.plot_confusion_matrix(y_test_full, yhat_random_full,  cmap=plt.cm.viridis, normalize=False, figsize=(12,12))
#lasso
response_list = y_test_lasso.drop_duplicates().sort_values().tolist()
size = x_test_lasso.shape[0]
weights = df6['cardio'].value_counts(normalize=True).sort_index().tolist()

# prediction
yhat_random_lasso = random.choices(response_list,k=size,weights=weights)

#performance
random_result_lasso = ml_error('Baseline', y_test_lasso, yhat_random_lasso)
random_result_lasso
#confusion matrix
mt.plot_confusion_matrix(y_test_lasso, yhat_random_lasso,  cmap=plt.cm.viridis, normalize=False, figsize=(12,12))
#relief
response_list = y_test_relief.drop_duplicates().sort_values().tolist()
size = x_test_relief.shape[0]
weights = df6['cardio'].value_counts(normalize=True).sort_index().tolist()

# prediction
yhat_random_relief = random.choices(response_list,k=size,weights=weights)

#performance
random_result_relief = ml_error('Baseline', y_test_relief, yhat_random_relief)
random_result_relief
#confusion matrix
mt.plot_confusion_matrix(y_test_relief, yhat_random_relief,  cmap=plt.cm.viridis, normalize=False, figsize=(12,12))<jupyter_output><empty_output><jupyter_text>7.2. Logistic Regression Model<jupyter_code>#model
lg = lm.LogisticRegression(n_jobs=-1, random_state=42).fit(x_train, y_train)

#prediction
yhat_lg = lg.predict(x_test)

#performance
lg_result = ml_error('Logistic Regression', y_test, yhat_lg)
lg_result
#confusion matrix
mt.plot_confusion_matrix(y_test, yhat_lg,  cmap=plt.cm.viridis, normalize=False, figsize=(12,12))
#model df full
lg = lm.LogisticRegression(n_jobs=-1, random_state=42).fit(x_train_full, y_train_full)

#prediction
yhat_lg_full = lg.predict(x_test_full)

#performance
lg_result_full = ml_error('Logistic Regression', y_test_full, yhat_lg_full)
lg_result_full
#confusion matrix
mt.plot_confusion_matrix(y_test_full, yhat_lg_full,  cmap=plt.cm.viridis, normalize=False, figsize=(12,12))
#boruta
#model
#lg = lm.LogisticRegression(n_jobs=-1, random_state=42).fit(x_train_boruta, y_train_boruta)

#prediction
#yhat_lg_full = lg.predict(x_test_boruta)

#performance
#lg_result_boruta = ml_error('Logistic Regression', y_test_boruta, yhat_lg_boruta)
#lg_result_boruta
#confusion matrix
#mt.plot_confusion_matrix(y_test_relief, yhat_lg_boruta,  cmap=plt.cm.viridis, normalize=False, figsize=(12,12))
#relief
#model
lg = lm.LogisticRegression(n_jobs=-1, random_state=42).fit(x_train_relief, y_train_relief)

#prediction
yhat_lg_relief = lg.predict(x_test_relief)

#performance
lg_result_relief = ml_error('Logistic Regression', y_test_relief, yhat_lg_relief)
lg_result_relief
#confusion matrix
mt.plot_confusion_matrix(y_test_relief, yhat_lg_relief,  cmap=plt.cm.viridis, normalize=False, figsize=(12,12))
#lasso
#model
lg = lm.LogisticRegression(n_jobs=-1, random_state=42).fit(x_train_lasso, y_train_lasso)

#prediction
yhat_lg_lasso = lg.predict(x_test_lasso)

#performance
lg_result_lasso = ml_error('Logistic Regression', y_test_lasso, yhat_lg_lasso)
lg_result_lasso
#confusion matrix
mt.plot_confusion_matrix(y_test_lasso, yhat_lg_lasso,  cmap=plt.cm.viridis, normalize=False, figsize=(12,12))<jupyter_output><empty_output><jupyter_text>7.2.1. Logistic Regression Performance - Cross Validation<jupyter_code>#geneate K-Fold
num_fold = 5
kfold = ms.StratifiedKFold(n_splits=num_fold, shuffle=True, random_state=42)

balanced_acc_list = []
kappa_list = []
weighted_f1_score_list = []
i = 1

for train_ix, val_ix in kfold.split(x_train, y_train):
    print('Fold Number: {}/{}'.format(i, num_fold))
    
    #get fold data -
    x_train_fold = x_train.iloc[train_ix]
    x_val_fold = x_train.iloc[val_ix]
    
    y_train_fold = y_train.iloc[train_ix]
    y_val_fold = y_train.iloc[val_ix]
    
    #model definition
    model = lm.LogisticRegression(n_jobs=-1, random_state=32)

    #train model
    model.fit(x_train_fold, y_train_fold)

    #prediction
    yhat_fold = model.predict(x_val_fold )

    #Balanced Accuracy
    balanced_acc = balanced_accuracy_score(y_val_fold, yhat_fold)
    balanced_acc_list.append(balanced_acc)

    #Kappa Metrics
    kappa = cohen_kappa_score(y_val_fold, yhat_fold)
    kappa_list.append(kappa)
    
    #Weighted F1-Score
    weighted_f1_score = f1_score(y_val_fold, yhat_fold, average='weighted')
    weighted_f1_score_list.append(weighted_f1_score)
    
    i += 1
    
print( 'Avg Balanced Accuracy: {}'.format(np.mean(balanced_acc_list)))
print( 'Avg Weighted F1-Score: {}'.format(np.mean(weighted_f1_score_list)))
print( 'Avg Kappa: {}'.format(np.mean(kappa_list)))<jupyter_output>Fold Number: 1/5
Fold Number: 2/5
Fold Number: 3/5
Fold Number: 4/5
Fold Number: 5/5
Avg Balanced Accuracy: 0.7239645968499455
Avg Weighted F1-Score: 0.7240468310984319
Avg Kappa: 0.44839106880898616<jupyter_text>7.3. Random Forest<jupyter_code>#model
rf = RandomForestClassifier(n_estimators=100, n_jobs=-1, random_state=42).fit(x_train, y_train)

#prediction
yhat_rf = rf.predict(x_test)

#performance
rf_result = ml_error('Random Forest', y_test, yhat_rf)
rf_result
#model df full
rf = RandomForestClassifier(n_estimators=100, n_jobs=-1, random_state=42).fit(x_train_full,y_train_full)

#prediction
yhat_rf_full = rf.predict(x_test_full)

#performance
rf_result = ml_error('Random Forest', y_test_full, yhat_rf_full)
rf_result
#model boruta
#rf = RandomForestClassifier(n_estimators=100, n_jobs=-1, random_state=42).fit(x_train_boruta,y_train_boruta)

#prediction
#yhat_rf_boruta = rf.predict(x_test_boruta)

#performance
#rf_result = ml_error('Random Forest', y_test_boruta, yhat_rf_boruta)
#rf_result
#model lasso
rf = RandomForestClassifier(n_estimators=100, n_jobs=-1, random_state=42).fit(x_train_lasso,y_train_lasso)

#prediction
yhat_rf_lasso = rf.predict(x_test_lasso)

#performance
rf_result = ml_error('Random Forest', y_test_lasso, yhat_rf_lasso)
rf_result
#model relief
rf = RandomForestClassifier(n_estimators=100, n_jobs=-1, random_state=42).fit(x_train_relief,y_train_relief)

#prediction
yhat_rf_relief = rf.predict(x_test_relief)

#performance
rf_result = ml_error('Random Forest', y_test_relief, yhat_rf_relief)
rf_result
#rf_bg = BaggingClassifier(RandomForestClassifier(), max_samples=0.5, max_features=1.0, n_estimators=100).fit(x_train,y_train)

#prediction
#yhat_bg = rf_bg.predict(x_test)

#performance
#bg_result = ml_error('Random Forest Bagging', y_test, yhat_bg)
#bg_result
#pickle.dump(bg_result, open('bg_result.pkl','+wb'))
bg_result_pickle = pickle.load(open('C:/Users/Henrique/repos/Cardio Catch Diseases/bg_result.pkl','rb'))
bg_result_pickle
rf_bg_relief = BaggingClassifier(RandomForestClassifier(), max_samples=0.5, max_features=1.0, n_estimators=100).fit(x_train_relief,y_train_relief)

#prediction
yhat_rf_bg_relief = rf_bg_relief.predict(x_test_relief)

#performance
rf_bg_relief_result = ml_error('Random Forest Bagging', y_test_relief, yhat_rf_bg_relief)
rf_bg_relief_result
pickle.dump(rf_bg_relief_result, open('rf_bg_relief_result.pkl','+wb'))
rf_bg_relief_result = pickle.load(open('C:/Users/Henrique/repos/Cardio Catch Diseases/rf_bg_relief_result.pkl','rb'))
rf_bg_relief_result<jupyter_output><empty_output><jupyter_text>7.4. KNN<jupyter_code>#model definition
model_knn = KNeighborsClassifier(n_neighbors=2, n_jobs=-1).fit(x_train, y_train)

#prediction
yhat_knn = model_knn.predict(x_test)

#performance
knn_result = ml_error('K-NN', y_test, yhat_knn)
knn_result
#model df full
model_knn = KNeighborsClassifier(n_neighbors=2, n_jobs=-1).fit(x_train_full, y_train_full)

#prediction
yhat_knn_full = model_knn.predict(x_test_full)

#performance
knn_result = ml_error('K-NN', y_test_full, yhat_knn_full)
knn_result
# model boruta
#model_knn = KNeighborsClassifier(n_neighbors=2, n_jobs=-1).fit(x_train_boruta, y_train_boruta)

#prediction
#yhat_knn_boruta = model_knn.predict(x_test_boruta)

#performance
#knn_result = ml_error('K-NN', y_test_boruta, yhat_knn_boruta)
#knn_result
#model lasso
model_knn = KNeighborsClassifier(n_neighbors=2, n_jobs=-1).fit(x_train_lasso, y_train_lasso)

#prediction
yhat_knn_lasso = model_knn.predict(x_test_lasso)

#performance
knn_result = ml_error('K-NN', y_test_lasso, yhat_knn_lasso)
knn_result
#model relief
model_knn = KNeighborsClassifier(n_neighbors=2, n_jobs=-1).fit(x_train_relief, y_train_relief)

#prediction
yhat_knn_relief = model_knn.predict(x_test_relief)

#performance
knn_result = ml_error('K-NN', y_test_relief, yhat_knn_relief)
knn_result
balanced_acc_list = []
for i in range(1, 6):
    print( 'K-value: {}'.format(i))
    #model definition
    model_knn = KNeighborsClassifier(n_neighbors=i, n_jobs=-1)

    #train model
    model_knn.fit(x_train, y_train)
    
    #prediction
    yhat_ft = model_knn.predict(x_test)
    
    #Balanced Accuracy Score
    balanced_acc_list.append(balanced_accuracy_score(y_test, yhat_ft))
#performance
knn_result_ft = ml_error('K-NN', y_test, yhat_ft)
knn_result_ft
knn_bg = BaggingClassifier(KNeighborsClassifier(), max_samples=0.5, max_features=1.0, n_estimators=100).fit(x_train,y_train)

#prediction
yhat_knn_bg = knn_bg.predict(x_test)

#performance
knn_bg_result = ml_error('KNN Bagging', y_test, yhat_knn_bg)
#bg_result
pickle.dump(knn_bg_result, open('knn_bg_result.pkl','+wb'))
knn_bg_result_pickle = pickle.load(open('C:/Users/Henrique/repos/Cardio Catch Diseases/knn_bg_result.pkl','rb'))
knn_bg_result_pickle<jupyter_output><empty_output><jupyter_text>7.5. Support Vector Machine<jupyter_code>#model definition
model_svm = SVC(random_state=42).fit(x_train, y_train)

#prediction
yhat_svm = model_svm.predict(x_test)

#performance
svm_result = ml_error('SVM', y_test, yhat_svm)
svm_result

pickle.dump(svm_result, open('svm_result.pkl','+wb'))
svm_result_pickle = pickle.load(open('C:/Users/Henrique/repos/Cardio Catch Diseases/svm_result.pkl','rb'))
svm_result_pickle
#model df full
model_svm = SVC(random_state=42).fit(x_train_full, y_train_full)

#prediction
yhat_svm_full = model_svm.predict(x_test_full)

#performance
svm_result_full = ml_error('SVM', y_test_full, yhat_svm_full)
svm_result_full

pickle.dump(svm_result_full, open('svm_result_full.pkl','+wb'))
svm_result_full_pickle = pickle.load(open('C:/Users/Henrique/repos/Cardio Catch Diseases/svm_result_full.pkl','rb'))
svm_result_full_pickle
#model boruta
#model_svm = SVC(random_state=42).fit(x_train_boruta, y_train_boruta)

#prediction
#yhat_svm_boruta = model_svm.predict(x_test_boruta)

#performance
#svm_result = ml_error('SVM', y_test_boruta, yhat_svm_boruta)
#svm_result
#model lasso
model_svm = SVC(random_state=42).fit(x_train_lasso, y_train_lasso)

#prediction
yhat_svm_lasso = model_svm.predict(x_test_lasso)

#performance
svm_result_lasso = ml_error('SVM', y_test_lasso, yhat_svm_lasso)
svm_result_lasso

pickle.dump(svm_result_lasso, open('svm_result_lasso.pkl','+wb'))
svm_result_lasso_pickle = pickle.load(open('C:/Users/Henrique/repos/Cardio Catch Diseases/svm_result_lasso.pkl','rb'))
svm_result_lasso_pickle
#model relief
model_svm = SVC(random_state=42).fit(x_train_relief, y_train_relief)

#prediction
yhat_svm_relief = model_svm.predict(x_test_relief)

#performance
svm_result_relief = ml_error('SVM', y_test_relief, yhat_svm_relief)
svm_result_relief

pickle.dump(svm_result_relief, open('svm_result_relief.pkl','+wb'))
svm_result_relief_pickle = pickle.load(open('C:/Users/Henrique/repos/Cardio Catch Diseases/svm_result_relief.pkl','rb'))
svm_result_relief_pickle<jupyter_output><empty_output><jupyter_text>7.5.2 SVM Performance - Cross Validation<jupyter_code>#geneate K-Fold
num_fold = 5
kfold = ms.StratifiedKFold(n_splits=num_fold, shuffle=True, random_state=42)

balanced_acc_list = []
kappa_list = []
weighted_f1_score_list = []
i = 1

for train_ix, val_ix in kfold.split(x_train, y_train):
    print( 'Fold Number: {}/{}'.format(i, num_fold))
    
    #get fold data -
    x_train_fold = x_train.iloc[train_ix]
    x_val_fold = x_train.iloc[val_ix]
    
    y_train_fold = y_train.iloc[train_ix]
    y_val_fold = y_train.iloc[val_ix]
    
    #model definition
    model = SVC(random_state=32)

    # train model
    model.fit(x_train_fold, y_train_fold)

    #prediction
    yhat_fold = model.predict(x_val_fold)

    #Balanced Accuracy
    balanced_acc = balanced_accuracy_score(y_val_fold, yhat_fold)
    balanced_acc_list.append(balanced_acc)

    #Kappa Metrics
    kappa = cohen_kappa_score(y_val_fold, yhat_fold)
    kappa_list.append(kappa)
    
    #Weighted F1-Score
    weighted_f1_score = f1_score(y_val_fold, yhat_fold, average='weighted')
    weighted_f1_score_list.append(weighted_f1_score)
    
    i += 1
    
print('Avg Balanced Accuracy: {}'.format(np.mean(balanced_acc_list)))
print('Avg Weighted F1-Score: {}'.format(np.mean(weighted_f1_score_list)))
print('Avg Kappa: {}'.format( np.mean(kappa_list)))
pickle.dump(balanced_acc_list, open('balanced_acc_list_svm.pkl','+wb'))
balanced_acc_list_svm = pickle.load(open('C:/Users/Henrique/repos/Cardio Catch Diseases/balanced_acc_list_svm.pkl','rb'))

pickle.dump(weighted_f1_score_list, open('weighted_f1_score_list_svm.pkl','+wb'))
weighted_f1_score_list_svm = pickle.load(open('C:/Users/Henrique/repos/Cardio Catch Diseases/weighted_f1_score_list_svm.pkl','rb'))

pickle.dump(kappa_list, open('kappa_list_svm.pkl','+wb'))
kappa_list_svm = pickle.load(open('C:/Users/Henrique/repos/Cardio Catch Diseases/kappa_list_svm.pkl','rb'))<jupyter_output><empty_output><jupyter_text>7.6. AdaBoost<jupyter_code>ab = AdaBoostClassifier(AdaBoostClassifier(), n_estimators=100, learning_rate = 1).fit(x_train,y_train)

#prediction
yhat_ab = ab.predict(x_test)

#performance
ab_result = ml_error('Ada Boost', y_test, yhat_ab)
ab_result
pickle.dump(ab_result, open('ab_result.pkl','+wb'))
ab_result_pickle = pickle.load(open('C:/Users/Henrique/repos/Cardio Catch Diseases/ab_result.pkl','rb'))
ab_result_pickle<jupyter_output><empty_output><jupyter_text>7.7. Decision Tree<jupyter_code>dt_bg = BaggingClassifier(DecisionTreeClassifier(), max_samples=0.5, max_features=1.0, n_estimators=100).fit(x_train,y_train)

#prediction
yhat_dt_bg = dt_bg.predict(x_test)

#performance
dt_bg_result = ml_error('DT Bagging', y_test, yhat_dt_bg)
#bg_result
pickle.dump(dt_bg_result, open('dt_bg_result.pkl','+wb'))
dt_bg_result_pickle = pickle.load(open('C:/Users/Henrique/repos/Cardio Catch Diseases/dt_bg_result.pkl','rb'))
dt_bg_result_pickle
dt_bg_relief = BaggingClassifier(DecisionTreeClassifier(), max_samples=0.5, max_features=1.0, n_estimators=100).fit(x_train_relief,y_train_relief)

#prediction
yhat_dt_bg_relief = dt_bg_relief.predict(x_test_relief)

#performance
dt_bg_relief_result = ml_error('DT Bagging', y_test_relief, yhat_dt_bg_relief)
#bg_result
pickle.dump(dt_bg_relief_result, open('dt_bg_relief_result.pkl','+wb'))
dt_bg_relief_result = pickle.load(open('C:/Users/Henrique/repos/Cardio Catch Diseases/dt_bg_relief_result.pkl','rb'))
dt_bg_relief_result<jupyter_output><empty_output><jupyter_text>6.4. XGBoost<jupyter_code>#model
model_xgb = xgb.XGBRegressor(objective = 'reg:squarederror',
                            n_estimators=100,
                            eta=0.01,
                            max_depth=10,
                            subsample=0.7,
                            colsample_bytee=0.9).fit(x_train,y_train)

#prediction
yhat_xgb = model_xgb.predict(x_test)

#performance
xgb_result = ml_error('XGBoost Regressor', np.expm1(y_test), np.expm1(yhat_xgb))
xgb_result
x_val.shape
#model definition
model = ml.Sequential()
model.add(l.Dense(11, input_dim=x_train.shape[1], activation='relu'))
model.add(l.Dense(1, activation='softmax'))

#model compile
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

#train model
es = EarlyStopping(monitor='val_loss', patience=10)
history = model.fit(x_train, y_train, validation_data=(x_val, y_val), epochs=10000, callbacks=[es], verbose=1)
##model evaluate
_, train_acc = model.evaluate(x_train, y_train, verbose=0)
_, val_acc = model.evaluate(x_val, y_val, verbose=0)

print('Train ACC:{} - Val ACC:{}'.format(train_acc, val_acc ))
#define the number of eigenvectors (autovetor) + eigenvalues (autovalores)
pca = d.PCA().fit(x_train)
k = np.argmax(np.cumsum(pca.explained_variance_ratio_) > 0.99)

#change data to new space
pca = d.PCA(k).fit(x_train)

x_train = pca.transform(x_train)
x_val = pca.transform(x_val)
#ohe = pp.OneHotEncoder()
#y_train = ohe.fit_transform(y_train.values.reshape(-1, 1)).toarray()
#y_val = ohe.transform(y_val.values.reshape(-1, 1)).toarray()
#model definition
model = ml.Sequential()
model.add(l.Dense(11, input_dim=x_train.shape[1], activation='relu'))
model.add(l.Dense(1, activation='softmax'))

#model compile
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

#train model
es = EarlyStopping(monitor='val_loss', patience=10)
history = model.fit(x_train, y_train, validation_data=(x_val, y_val), epochs=100, callbacks=[es], verbose=0)
##model evaluate
_, train_acc = model.evaluate(x_train, y_train, verbose=0)
_, val_acc = model.evaluate(x_val, y_val, verbose=0)

print('Train ACC:{} - Val ACC:{}'.format(train_acc, val_acc ))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16569962638384325, 'fraction_numerical': 0.03476179817840602, 'mean_word_length': 5.70151148032768, 'pattern_counts': {'":': 0, '<': 213, '<?xml version=': 0, '>': 227, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '44', 'dir': 'jupyter-structured-clean-dedup', 'id': '393514', 'license': 'MIT', 'n_tokens_mistral': 23473, 'n_tokens_neox': 22021, 'n_words': 4386, 'path': 'm05_cardio_catch_diseases.ipynb', 'repo_name': 'HenriqueCR1291/cardio_catch_diseases', 'size': '58081'}