File size: 3,523 Bytes
dd0c888
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
83
84
85
86
87
88
89
90
91
92
93
94
import streamlit as st
import pandas as pd
import numpy as np
import pickle
import json


# Load All Files

with open('model_rf.pkl', 'rb') as file_1:
  model_rf = pickle.load(file_1)

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

with open('model_encoder.pkl','rb') as file_4:
  model_encoder = pickle.load(file_4)

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

with open('list_cat_cols.txt', 'r') as file_6:
  list_cat_cols = json.load(file_6)

def run():

    st.title('Heart Failure Prediction')


    with st.form(key='form_heart_failure'):
        age = st.number_input('Age', min_value=1, max_value=100, value=25, step=1, help='in Years Old')
        anemia = st.selectbox('Do you have Anemia?', ('Yes', 'No'))
        cpk = st.number_input('Level of CPK Enzyme', min_value=10, max_value=8000, value=120, help='in mcg/L, 10-120 considered normal')
        diabetes = st.selectbox('Do you have Diabetes?', ('Yes','No'))
        ejection_fraction = st.slider('Ejection Fraction', 0,100,50, help='in Percentage (%)')
        hypertension = st.selectbox('Do you have Hypertension?', ('Yes','No'))
        platelets = st.number_input('Platelets / Trombosit per mL of Blood', min_value=1000, max_value=800000, value=150000,help='150k - 450k is considered normal')
        serum_creatinine = st.number_input('Serum Creatinine Level', min_value=.1, max_value=10., value=1.,step=.1, help='in mg/dL, 0.6 - 1.2 is considered normal')
        serum_sodium = st.number_input('Serum Sodium Level', min_value=100, max_value=200, value=150, help='in mEq/L, 135 - 145 considered to be normal')
        sex = st.selectbox('Are you a male or female?', ('Male','Female'))
        smoking = st.selectbox('Do you smoke?', ('Yes','No'))
        time = st.number_input('How many days since receiving medical treatment?', min_value=1, max_value=365, value=1)
        
        submitted = st.form_submit_button('Predict')


    data_inf = {
        'age': age,
        'anemia': anemia,
        'cpk': cpk,
        'diabetes': diabetes,
        'ejection_fraction' : ejection_fraction,
        'hypertension': hypertension,
        '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)

    data_inf['anemia'] = data_inf['anemia'].replace({'Yes': 1, 'No': 0})
    data_inf['diabetes'] = data_inf['diabetes'].replace({'Yes': 1, 'No': 0})
    data_inf['hypertension'] = data_inf['hypertension'].replace({'Yes': 1, 'No': 0})
    data_inf['smoking'] = data_inf['smoking'].replace({'Yes': 1, 'No': 0})
    data_inf['sex'] = data_inf['sex'].replace({'Male': 1, 'Female': 0})

    if submitted:
        # Split between Numerical Columns and Categorical Columns

        data_inf_num = data_inf[list_num_cols]
        data_inf_cat = data_inf[list_cat_cols]

        # Feature Scaling and Feature Encoding

        data_inf_num_scaled = model_scaler.transform(data_inf_num)
        data_inf_cat_encoded = model_encoder.transform(data_inf_cat)
        data_inf_final = np.concatenate([data_inf_num_scaled, data_inf_cat_encoded], axis=1)

        # Predict using Model

        y_pred_inf = model_rf.predict(data_inf_final)
        st.write('Hasil prediksi Model : ', y_pred_inf)


    '''
    Hasil Prediksi 0 = Alive, 1 = Dead
    '''

if __name__ == '__main__':
    run()