kodokgodog
commited on
Commit
•
67c2d32
1
Parent(s):
8fc0327
Upload 6 files
Browse files- app.py +10 -0
- eda.py +69 -0
- list_num_cols.txt +1 -0
- model_rf.pkl +3 -0
- model_scaler.pkl +3 -0
- prediction.py +75 -0
app.py
CHANGED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import eda
|
3 |
+
import prediction
|
4 |
+
|
5 |
+
navigation = st.sidebar.selectbox('Pilih Halaman : ', ('EDA','Predict Heart Failure'))
|
6 |
+
|
7 |
+
if navigation == 'EDA':
|
8 |
+
eda.run()
|
9 |
+
else:
|
10 |
+
prediction.run()
|
eda.py
ADDED
@@ -0,0 +1,69 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import seaborn as sns
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
import plotly.express as px
|
6 |
+
from PIL import Image
|
7 |
+
|
8 |
+
st.set_page_config(
|
9 |
+
page_title = 'Heart Failure - EDA',
|
10 |
+
layout = 'wide',
|
11 |
+
initial_sidebar_state = 'expanded'
|
12 |
+
)
|
13 |
+
|
14 |
+
def run() :
|
15 |
+
|
16 |
+
# Membuat Title
|
17 |
+
st.title('Heart Rate Survivor Prediction')
|
18 |
+
|
19 |
+
# Membuat Sub Header
|
20 |
+
st.subheader('EDA untuk Analisa Dataset Heart Failure')
|
21 |
+
|
22 |
+
# Menambahkan Gambar
|
23 |
+
image = Image.open('heart.jpg')
|
24 |
+
st.image(image, caption='Love your Heart')
|
25 |
+
|
26 |
+
# Menambahkan Deskripsi
|
27 |
+
st.write('Page ini dibuat oleh **Satriya Fauzan Adhim**')
|
28 |
+
|
29 |
+
# Membuat Garis Lurus
|
30 |
+
st.markdown('---')
|
31 |
+
|
32 |
+
# Magic Syntax
|
33 |
+
'''
|
34 |
+
Pada page ini, penulis akan melakukan eksplorasi sederhana dari dataset Heart Failure.
|
35 |
+
Dataset yang digunakan adalah dataset yang berisikan tentang data terkait dengan heart failure.
|
36 |
+
Dataset diambil dari Google Bigquery.
|
37 |
+
|
38 |
+
'''
|
39 |
+
|
40 |
+
# Show DataFrame
|
41 |
+
data = pd.read_csv('https://raw.githubusercontent.com/kodokgodog/Latihan_hactiv8/main/h8dsft_P1G3_Satriya_Fauzan_Adhim.csv')
|
42 |
+
st.dataframe(data)
|
43 |
+
|
44 |
+
# Membuat Barplot
|
45 |
+
st.write('#### Plot DEATH_EVENT')
|
46 |
+
fig = plt.figure(figsize=(15, 5))
|
47 |
+
sns.countplot(x='DEATH_EVENT', data=data)
|
48 |
+
st.pyplot(fig)
|
49 |
+
|
50 |
+
# Membuat Historgram
|
51 |
+
st.write('#### Histogram of ejection_fraction')
|
52 |
+
fig = plt.figure(figsize=(15, 5))
|
53 |
+
sns.histplot(data['ejection_fraction'], bins=30, kde=True)
|
54 |
+
st.pyplot(fig)
|
55 |
+
|
56 |
+
# Membuat Histogram Berdasarkan Input User
|
57 |
+
st.write('#### Histogram berdasarkan input user')
|
58 |
+
pilihan = st.selectbox('Pilih column : ', ('age', 'ejection_fraction', 'serum_creatinine', 'serum_sodium'))
|
59 |
+
fig = plt.figure(figsize=(15, 5))
|
60 |
+
sns.histplot(data[pilihan], bins=30, kde=True)
|
61 |
+
st.pyplot(fig)
|
62 |
+
|
63 |
+
# Membuat Plotly Plot
|
64 |
+
st.write('#### Plotly Plot - anaemia dengan DEATH_EVENT')
|
65 |
+
fig = px.scatter(data, x='anaemia', y='DEATH_EVENT', hover_data=['age'])
|
66 |
+
st.plotly_chart(fig)
|
67 |
+
|
68 |
+
if __name__== '__main__':
|
69 |
+
run()
|
list_num_cols.txt
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
["age", "ejection_fraction", "serum_creatinine", "serum_sodium", "time"]
|
model_rf.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:78d62464a52df0bf9d4b0a9f91aa35e843bcbd512bb03a196f7a1f7401ef61ce
|
3 |
+
size 486809
|
model_scaler.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:9e9f35bfb5b324f99cb603976c6579c62b40f686098f9371558df3e8f3378c9e
|
3 |
+
size 1247
|
prediction.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
import json
|
5 |
+
import joblib
|
6 |
+
|
7 |
+
# load file joblib dengan .pkl
|
8 |
+
with open('model_rf.pkl', 'rb') as file_1:
|
9 |
+
model_rf= joblib.load(file_1)
|
10 |
+
|
11 |
+
with open('model_scaler.pkl', 'rb') as file_2:
|
12 |
+
model_scaler= joblib.load(file_2)
|
13 |
+
|
14 |
+
#loan file txt dengan json.load
|
15 |
+
with open('list_num_cols.txt', 'r') as file_3:
|
16 |
+
num_columns= json.load(file_3)
|
17 |
+
|
18 |
+
def run() :
|
19 |
+
|
20 |
+
with st.form(key='from_heart_failure'):
|
21 |
+
#name = st.text_input('Full Name', value='')
|
22 |
+
age = st.number_input('Age', min_value=25, max_value=100, value=25, step=1, help='Usia Pasien')
|
23 |
+
sex = st.selectbox('Sex', ('Man', 'Woman'), index=1)
|
24 |
+
smoking = st.selectbox('Smoking', ('No', 'Yes'), index=1)
|
25 |
+
st.markdown('---')
|
26 |
+
|
27 |
+
phosphokinase = st.slider('Creatinine Phosphokinase', min_value=23, max_value=7861, value=70)
|
28 |
+
fraction = st.slider('Ejection Fraction', 10, 90, 50)
|
29 |
+
platelets = st.slider('Platelets', 25000, 900000, 125000)
|
30 |
+
creatinine = st.number_input('Serum Creatinine', min_value=0.0, max_value=10.0, value=0.0, step=0.1)
|
31 |
+
sodium = st.slider('Serum Sodium', 100, 148, 120)
|
32 |
+
time = st.slider('Time', 1, 300, 1)
|
33 |
+
st.markdown('---')
|
34 |
+
|
35 |
+
anaemia = st.selectbox('Have Anaemia', ('No', 'Yes'), index=1)
|
36 |
+
diabetes = st.selectbox('Have Diabetes', ('No', 'Yes'), index=1)
|
37 |
+
high_blood_pressure = st.selectbox('Have High Blood Pressure', ('No', 'Yes'), index=1)
|
38 |
+
DEATH_EVENT = st.selectbox('Death Event', ('Survived', 'Dead'), index=1)
|
39 |
+
|
40 |
+
submitted = st.form_submit_button('Predict')
|
41 |
+
|
42 |
+
data_inf = {
|
43 |
+
'age': age,
|
44 |
+
'anaemia': anaemia,
|
45 |
+
'creatin_phosphokinase: ': phosphokinase,
|
46 |
+
'diabetes': diabetes,
|
47 |
+
'ejection_fraction': fraction,
|
48 |
+
'high_blood_pressure': high_blood_pressure,
|
49 |
+
'platelets': platelets,
|
50 |
+
'serum_creatinine': creatinine,
|
51 |
+
'serum_sodium': sodium,
|
52 |
+
'sex': sex,
|
53 |
+
'smoking': smoking,
|
54 |
+
'time': time,
|
55 |
+
'DEATH_EVENT': DEATH_EVENT,
|
56 |
+
}
|
57 |
+
|
58 |
+
data_inf = pd.DataFrame([data_inf])
|
59 |
+
data_inf
|
60 |
+
|
61 |
+
|
62 |
+
if submitted:
|
63 |
+
#Numerical Columns
|
64 |
+
data_inf_num = data_inf[num_columns]
|
65 |
+
|
66 |
+
# Feature Scaling
|
67 |
+
data_inf_final = model_scaler.transform(data_inf_num)
|
68 |
+
|
69 |
+
# Predict using Random Forest
|
70 |
+
y_pred_inf = model_rf.predict(data_inf_final)
|
71 |
+
prediction_label = "Survived" if y_pred_inf == 0 else "Dead"
|
72 |
+
st.write('# Rating : ', prediction_label)
|
73 |
+
|
74 |
+
if __name__== '__main__':
|
75 |
+
run()
|