File size: 3,117 Bytes
56d9941
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66f7821
 
 
 
 
 
 
 
 
 
 
 
56d9941
 
 
 
 
c4c99da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56d9941
 
 
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
import streamlit as st 
import pandas as pd
import numpy as np 
import pickle 
import json

# Load all files

with open('random_forest_model.pkl', 'rb') as file_2:
    loaded_model = pickle.load(file_2)

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

# Load the list of numerical columns
with open('list_num_cols.txt', 'r') as file_3:
    list_num_cols = json.load(file_3)

# Load the list of categorical columns
with open('list_cat_cols.txt', 'r') as file_4:
    list_cat_cols = json.load(file_4)

def run():
    
    with st.form(key='from_heart_failure'):
        Age = st.number_input('age', min_value=16, max_value=50, value=25, step=1, help='Usia Prediksi')
        Anaemia = st.selectbox('anaemia', (1, 0), index=1, help='0 for NO 1 for YES')
        Creatinine = st.slider('creatinine_phosphokinase', min_value=0, max_value=1000, value=78)
        Diabetes = st.selectbox('diabetes', (1, 0), index=1, help='0 for NO 1 for YES')
        Ejection = st.slider('ejection_fraction', min_value=0, max_value=100, value=80)
        BloodPleasure = st.selectbox('high_blood_pressure', (1, 0), index=1, help='0 for NO 1 for YES')
        st.markdown('---')
        
        Platelets = st.number_input('platelets', min_value=100000, max_value=500000, value=250000)
        SerumCreatinine = st.number_input('serum_creatinine', min_value=0.1, max_value=5.0, value=1.7)
        Serum_Sodium = st.number_input('serum_sodium', min_value=100, max_value=160, value=137)
        st.markdown('---')
        
        Gender = st.selectbox('sex', (1, 0), index=1, help='0 for Male 1 for Famele')
        Smoking = st.selectbox('smoking', (1, 0), index=1, help='0 for No 1 for Yes')
        Time = st.number_input('time', min_value=10, max_value=300, value=197)
        
        submitted = st.form_submit_button('Prediction')

    data_inf = {
        'age': Age,
        'anaemia': Anaemia,
        'creatinine_phosphokinase': Creatinine,
        'diabetes': Diabetes,
        'ejection_fraction': Ejection,
        'high_blood_pressure': BloodPleasure,
        'platelets': Platelets,
        'serum_creatinine': SerumCreatinine,
        'serum_sodium': Serum_Sodium,
        'sex': Gender,
        'smoking': Smoking,
        'time': Time
    }

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

    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_final = np.concatenate([data_inf_num_scaled], axis=1)
    
        # Prediksi menggunakan random forest
    
        y_pred_inf = loaded_model.predict(data_inf_final)
    
        # Mengubah hasil prediksi menjadi label yang sesuai
        if y_pred_inf == 0:
            hasil_prediksi = "Ada Harapan"
        else:
            hasil_prediksi = "Death"
    
        st.write('# Kemungkinan : ', hasil_prediksi)

if __name__== '__main__':
    run()