<jupyter_start><jupyter_text>Gradient boosting using CatBoost on the eICU dataset to predict time of patient mortaility based on APACHE score<jupyter_code>import tensorflow as tf
import pandas as pd
import kmapper
from catboost import CatBoostClassifier
from sklearn.preprocessing import LabelEncoder, OneHotEncoder, KBinsDiscretizer
df = pd.read_csv('ICU dataset.csv')
X = df[['uniquepid', 'apachescore', 'actualiculos', 'hospitaldischargelocation', 'age', 'ethnicity', 'apacheadmissiondx', 'admissionheight', 'admissionweight', 'unitadmitsource', 'intubated', 'vent', 'dialysis', 'eyes', 'motor', 'meds', 'urine', 'wbc', 'temperature', 'respiratoryrate', 'sodium', 'heartrate', 'meanbp', 'ph', 'hematocrit', 'albumin', 'pao2', 'pco2', 'bun', 'glucose', 'bilirubin', 'fio2']]
filter_1 = (y_1['actualiculos'] < 2) & (y_1['hospitaldischargelocation'] == 'Death')
filter_2 = (y_1['actualiculos'] >= 2) & (y_1['hospitaldischargelocation'] == 'Death')

# Drop and replace NaNs
X['unitadmitsource'].fillna('None', inplace=True)
X.dropna(inplace=True)
 
### PREPROCESSING
X['age'].replace(to_replace='> 89', value=89, inplace=True)
X['age'] = X['age'].apply(lambda x: int(x))
#X['age'] = pd.qcut(X['age'], 5, labels=[0,10,20,40,60])


train = X[['age', 'ethnicity', 'apacheadmissiondx', 'admissionheight', 'admissionweight', 'unitadmitsource', 'intubated', 'vent', 'dialysis', 'eyes', 'motor', 'meds', 'urine', 'wbc', 'temperature', 'respiratoryrate', 'sodium', 'heartrate', 'meanbp', 'ph', 'hematocrit', 'albumin', 'pao2', 'pco2', 'bun', 'glucose', 'bilirubin', 'fio2']]
label = (y_1['hospitaldischargelocation'] == 'Death').apply(lambda x: int(x))
y_0 = X[['apachescore']]
y_1 = X[['actualiculos', 'hospitaldischargelocation']]
avg_apache_all = X[y_1['hospitaldischargelocation'] != 'Death']['apachescore'].mean()
less_than_48 = X['apachescore'][filter_1].mean()
more_than_48 = X['apachescore'][filter_2].mean()

print('Average apache scores of')
print(f'Total population: {round(avg_apache_all, 2)}')
print(f'People that died within 48 hours: {round(less_than_24, 2)}')
print(f'People that died after 48 hours: {round(more_than_24, 2)}')
cat_features = ['ethnicity', 'apacheadmissiondx', 'unitadmitsource']
model = CatBoostClassifier(iterations=1000, 
                           task_type="GPU",
                           devices='0:1')
model.fit(train,
          test,
          cat_features)
print('Feature name:',' '*10,'Feature importance:')
for col, scr in zip(model.feature_names_, model.feature_importances_):
        print(col, ' '*(22-len(col)), round(scr,2))
shap_values = model.get_feature_importance(catboost.Pool(train, label=test,cat_features=cat_features), type="ShapValues")
print('Shap values for Patient uniqueid  002-34851:')
print('-'*40)
for col, scr in zip(model.feature_names_, shap_values[0]):
    print(col, ' '*(22-len(col)), round(scr,2))<jupyter_output>Shap values for Patient uniqueid  002-34851:
----------------------------------------
age                     0.32
ethnicity               0.03
apacheadmissiondx       -0.37
admissionheight         -0.1
admissionweight         -0.06
unitadmitsource         -0.02
intubated               -0.0
vent                    -0.11
dialysis                -0.0
eyes                    -0.11
motor                   -0.11
meds                    0.0
urine                   0.21
wbc                     0.06
temperature             0.07
respiratoryrate         0.11
sodium                  -0.13
heartrate               0.58
meanbp                  -0.15
ph                      0.01
hematocrit              -0.02
albumin                 -0.27
pao2                    -0.01
pco2                    0.0
bun                     0.13
glucose                 -0.11
bilirubin               1.01
fio2                    -0.09
{'fraction_non_alphanumeric': 0.16424610509638235, 'fraction_numerical': 0.04330604700290468, 'mean_word_length': 3.029787234042553, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '194581', 'license': 'MIT', 'n_tokens_mistral': 1381, 'n_tokens_neox': 1243, 'n_words': 278, 'path': 'ICU2NB.ipynb', 'repo_name': 'dronelyfe/ICU2_UI', 'size': '3787'}