File size: 4,305 Bytes
6eb2dc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import streamlit as st
import pandas as pd
import numpy as np
import pickle
import json

# Load all files

with open('list_num_cols.txt', 'r') as file_1:
  list_num_cols = json.load(file_1)

with open('model_scaler.pkl', 'rb') as file_2:
  scaler = pickle.load(file_2)

with open('model_rfc.pkl', 'rb') as file_3:
  model_rfc = pickle.load(file_3)
  
with open('model_gbc.pkl', 'rb') as file_4:
  model_gbc = pickle.load(file_4)
  
def run():
    st.write('##### Form Prediction of Death Due To Heart Failure')
    # Making Form
    with st.form(key='Form Form Prediction of Death Due To Heart Failure'):
        age                            = st.number_input('age', min_value=30, max_value=90, value=30)
        anaemia                        = st.selectbox('anaemia', (0,1), index=0, help='0=No, 1=Yes | Decrease of red blood cells or hemogrobin.')
        creatinine_phosphokinase       = st.number_input('creatinine_phosphokinase', min_value=50, max_value=6000, value=50, step=1, help='Level of the CPK enzyme in the blood (mcg/L).')
        diabetes                       = st.selectbox('diabetes', (0,1), index=0, help='0=No, 1=Yes | If the patient has diabetes.')
        ejection_fraction              = st.number_input('ejection_fraction', min_value=1, max_value=100, value=1, help='Percentage of blood leaving the heart at each contraction (percentage).')
        high_blood_pressure            = st.selectbox('high_blood_pressure', (0,1), index=0, help='0=No, 1=Yes | If the patient has hypertention.')
        platelets                      = st.number_input('platelets', min_value=40000, max_value=500000, value=40000, help='Platelets in the blood (kiloplatelets/mL).')
        serum_creatinine               = st.number_input('serum_creatinine', min_value=0, max_value=10, value=0, help='Level of serum creatinine in the blood (mg/dL).')
        serum_sodium                   = st.number_input('serum_sodium', min_value=100, max_value=150, value=100, help='Level of serum sodium in the blood (mEq/L).')
        sex                            = st.selectbox('sex', (0,1), index=0, help='0=Woman, 1=Man')
        smoking                        = st.selectbox('smoking', (0,1), index=0, help='0=No, 1=Yes | If the patient smokes or not.')
        time                           = st.number_input('time', min_value=1, max_value=300, value=1, help='Follow-up period (days).')
        st.markdown('---')
        
        submited_1 = st.form_submit_button('Predict using RFC')
        submited_2 = st.form_submit_button('Predict using GBC')
        
        data_inf = {
            'age'                       : age,
            'anaemia'                   : anaemia,
            'creatinine_phosphokinase'  : creatinine_phosphokinase,
            'diabetes'                  : diabetes,
            'ejection_fraction'         : ejection_fraction,
            'high_blood_pressure'       : high_blood_pressure,
            'platelets'                 : platelets,
            'serum_creatinine'          : serum_creatinine,
            'serum_sodium'              : serum_sodium,
            'sex'                       : sex,
            'smoking'                   : smoking,
            'time'                      : time,
        }

    data_inf = pd.DataFrame([data_inf])
    st.dataframe(data_inf)

    if submited_1:
        # Split between numerical columns and categorical columns
        data_inf_num = data_inf[list_num_cols]
        data_inf_num
        #Feature scaling and feature encoding
        data_inf_num_scaled  = scaler.transform(data_inf_num)
        data_inf_final = data_inf_num_scaled
        #Predict using Random Forest Classification
        y_pred_inf = model_rfc.predict(data_inf_final)
        st.write('# Result Prediction using RFC : ', str(int(y_pred_inf)))
    else:
        # Split between numerical columns and categorical columns
        data_inf_num = data_inf[list_num_cols]
        data_inf_num
        #Feature scaling and feature encoding
        data_inf_num_scaled  = scaler.transform(data_inf_num)
        data_inf_final = data_inf_num_scaled
        #Predict using RFC
        y_pred_inf = model_gbc.predict(data_inf_final)
        st.write('# Result Prediction using GBC : ', str(int(y_pred_inf)))
        
if __name__ == '__main__':
    run()