<jupyter_start><jupyter_text>Data Variables:"triage": Triage given upon arrival to ED. Class=factor. Green, yellow, orange, red."age": Age, rounded to lower integer. Class=numeric"sex": Class=factor. female/male"crp": S-C-reactive protein. Unit: nmol/l"k": S-Pottasium. Unit: mmol/L"na": S-Sodium. Unit: mmol/L"hb": S-haemoglobine. Unit: mmol/L"crea": S-creatinine. Unit: umol/L"leu": Blood leucocyte count. Unit: 10E9/L"alb": S-albumine. Unit: g/L"ldh": S-Lactate dehydrogenasis. Unit: U/L"mort30": 30-day survival. Class=factor: 0=Survived >30 days. 1= dead within 30 days."icutime": Number of days on Intensive Care Unit. 99999=not admitted to ICU"icustatus": Class=factor. admitted to ICU yes/no. 0=no, 1=yes"inddage": Number of days admitted to hospital"genindl.1": Class=factor. Readmitted within 30 days yes/no. 1=yes, 0=no      "saturation": Peripheral arterial oxygen saturation. Unit: %.           "respirationsfrekvens": Respiratory rate. Unit: /min"puls": Heart rate. Unit: /min                 "systoliskblodtryk": Systolic blood pressure. Unit: mmHg    "gcs": Glascow coma score.         Source: https://datadryad.org/stash/dataset/doi%253A10.5061%252Fdryad.m2bq5  Import files & Python Packages<jupyter_code>import pandas as pd
import numpy as np
import seaborn as sn
import pyreadr
file_t = pyreadr.read_r('data/PrimaryDataAkutDB_Upload2017.rda')
file_v = pyreadr.read_r('data/ValidationDataTriageDB_Upload2017.rda')
print(file_t.keys())
print(file_v.keys())
train = file_t["data"]
val = file_v["data"]
print(train.shape)
print(val.shape)<jupyter_output>(6249, 21)
(6383, 18)<jupyter_text>Match Training & Validation Data Categories + Names<jupyter_code>val.columns
train.columns
val = val.rename(columns={"ev_intensiv": "admitted_icu", 'natr': 'na'});
train = train.rename(columns = {'icustatus':"admitted_icu", 'inddage': 'LOS', 'saturation': 'sat', 'respirationsfrekvens': 'rf', 'systoliskblodtryk':'bts'})
train.drop(columns = ['gcs', 'icutime','genindl.1'], inplace=True)
train.head(1)
val.head(1)
train.dtypes
# train['icutime']=np.where(train['icutime'] == 999999.0, None, train['icutime'])
# 46 null triage
# 654 missing crp
# 603 missing k
# 591 missing na
train.dropna(inplace = True)
val.dropna(inplace=True)
train.corr()
train[['rf', 'puls', 'bts']] = train[['rf', 'puls', 'bts']].astype('int')
val[['rf', 'puls', 'bts', 'admitted_icu', 'sat']] = val[['rf', 'puls', 'bts', 'admitted_icu', 'sat']].astype('int')
val['sex'] = val['sex'].astype('str')
val = val.reset_index()
val.drop(columns = ['rownames'], inplace=True)
val.columns
val= val[['triage', 'age', 'sex', 'crp', 'k', 'na', 'hb', 'crea', 'leu', 'alb',
       'ldh', 'mort30', 'admitted_icu', 'LOS', 'sat', 'rf', 'puls', 'bts']]
# Val and Train Data now match!
val.head(1)
train.head(5)
train['sex_f']=np.where(train['sex']=='female', 1, 0)
train['sex_m']=np.where(train['sex']=='male', 1, 0)
train.drop(columns = ['sex'], inplace=True)

val['sex_f']=np.where(val['sex']=='female', 1, 0)
val['sex_m']=np.where(val['sex']=='male', 1, 0)
val.drop(columns = ['sex'], inplace=True)<jupyter_output><empty_output><jupyter_text>Split Data<jupyter_code># Remove all but blood test
X_train = train.drop(columns = ['mort30','LOS', 'admitted_icu', 'triage'])
# Remove all but blood test
X_val = val.drop(columns = ['mort30','LOS', 'admitted_icu', 'triage'])
y_train = train['mort30']
y_val = val['mort30']
# Confirm shape
print(X_train.shape)
print(y_train.shape)<jupyter_output>(4717, 15)
(4717,)<jupyter_text>Set Baseline<jupyter_code>y_train.value_counts(normalize = True)
# There's already an extraordinarily high skew towards survival (which is a good thing -- bad for modeling problems)
X_train.head(1)<jupyter_output><empty_output><jupyter_text>Create Model<jupyter_code>from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import r2_score
from sklearn.ensemble import RandomForestClassifier
from sklearn.naive_bayes import GaussianNB
# base model using logistic regression
model_lr = make_pipeline(
    StandardScaler(),
    LogisticRegression()
)

model_lr.fit(X_train, y_train)
#Logistic Regression Model
lr_train_acc = model_lr.score(X_train, y_train)
lr_val_acc = model_lr.score(X_val, y_val)

print('Training Accuracy Score:', train_acc_lr)
print('Validation Accuracy Score:', val_acc_lr)
# Precission & Recall
import matplotlib.pyplot as plt
from sklearn.metrics import accuracy_score, plot_confusion_matrix, classification_report

plot_confusion_matrix(
    model_lr,
    X_val,
    y_val,
    values_format='.0f',
    cmap=plt.cm.Blues_r
);
plt.show()
# model using random forrest classifier

model_rf = make_pipeline(
    StandardScaler(),
    RandomForestClassifier(n_estimators=300, 
                           max_depth=30)
)

model_rf.fit(X_train, y_train);
#Logistic Regression Model
rf_train_acc = model_rf.score(X_train, y_train)
rf_val_acc = model_rf.score(X_val, y_val)

print('Training Accuracy Score:', rf_train_acc)
print('Validation Accuracy Score:', rf_val_acc)
# Precission & Recall
plot_confusion_matrix(
    model_rf,
    X_val,
    y_val,
    values_format='.0f',
    cmap=plt.cm.Blues_r
);
plt.show()
# Naive Bayes
model_b = make_pipeline(
    StandardScaler(),
    GaussianNB()
)

model_b.fit(X_train, y_train)
#Logistic Regression Model
b_train_acc = model_b.score(X_train, y_train)
b_val_acc = model_b.score(X_val, y_val)

print('Training Accuracy Score:', b_train_acc)
print('Validation Accuracy Score:', b_val_acc)
# Precission & Recall
plot_confusion_matrix(
    model_b,
    X_val,
    y_val,
    values_format='.0f',
    cmap=plt.cm.Blues_r
);
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15377886634009796, 'fraction_numerical': 0.02099370188943317, 'mean_word_length': 5.904589371980676, 'pattern_counts': {'":': 22, '<': 19, '<?xml version=': 0, '>': 20, 'https://': 1, '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': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '169081', 'license': 'MIT', 'n_tokens_mistral': 2289, 'n_tokens_neox': 2134, 'n_words': 462, 'path': 'analysis/Analysis.ipynb', 'repo_name': 'JenFaith/hospital_records', 'size': '5716'}