Spaces:
Sleeping
Sleeping
modeldeploy
Browse files- app.py +72 -0
- categorical_columns.txt +1 -0
- eda.py +35 -0
- model_scaler.pkl +3 -0
- numeric_columns.txt +1 -0
- prediction.py +53 -0
- requirements.txt +6 -0
- stacked_es_model.pkl +3 -0
app.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import joblib
|
| 5 |
+
import seaborn as sns
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
import json
|
| 8 |
+
|
| 9 |
+
# Fungsi untuk memuat data
|
| 10 |
+
def load_data():
|
| 11 |
+
df = pd.read_csv('h8dsft_P1G3_Muhammad_Arief_Kurniawan.csv')
|
| 12 |
+
return df
|
| 13 |
+
|
| 14 |
+
# Fungsi untuk memuat komponen model dan preprocessing
|
| 15 |
+
def load_components():
|
| 16 |
+
model = joblib.load('stacked_es_model.pkl')
|
| 17 |
+
scaler = joblib.load('model_scaler.pkl')
|
| 18 |
+
with open('numeric_columns.txt', 'r') as file_1:
|
| 19 |
+
numeric_columns = json.load(file_1)
|
| 20 |
+
with open('categorical_columns.txt', 'r') as file_2:
|
| 21 |
+
categorical_columns = json.load(file_2)
|
| 22 |
+
return model, scaler, numeric_columns, categorical_columns
|
| 23 |
+
|
| 24 |
+
# Fungsi untuk mendapatkan input dari pengguna
|
| 25 |
+
def get_user_input(numeric_columns, categorical_columns):
|
| 26 |
+
inputs = {}
|
| 27 |
+
for col in numeric_columns + categorical_columns:
|
| 28 |
+
if col in categorical_columns:
|
| 29 |
+
inputs[col] = st.selectbox(col, [0, 1])
|
| 30 |
+
else:
|
| 31 |
+
default_val = np.expm1(50.0) if col != 'time' else 50.0
|
| 32 |
+
inputs[col] = np.log1p(st.number_input(col, value=default_val))
|
| 33 |
+
return pd.DataFrame([inputs])
|
| 34 |
+
|
| 35 |
+
def main():
|
| 36 |
+
st.title("Prediksi dan Eksplorasi Gagal Jantung")
|
| 37 |
+
|
| 38 |
+
# Sidebar untuk navigasi
|
| 39 |
+
choice = st.sidebar.selectbox("Pilih Halaman", ["Beranda", "Eksplorasi Data", "Prediksi"])
|
| 40 |
+
|
| 41 |
+
if choice == "Beranda":
|
| 42 |
+
st.write("Selamat datang di aplikasi prediksi dan eksplorasi gagal jantung.")
|
| 43 |
+
st.write("Silakan pilih halaman di sidebar untuk melanjutkan.")
|
| 44 |
+
|
| 45 |
+
elif choice == "Eksplorasi Data":
|
| 46 |
+
data = load_data()
|
| 47 |
+
st.write("### Informasi Dasar Data")
|
| 48 |
+
st.write(data.info())
|
| 49 |
+
st.write("### Deskripsi Statistik")
|
| 50 |
+
st.write(data.describe())
|
| 51 |
+
st.write("### 5 Data Pertama")
|
| 52 |
+
st.write(data.head())
|
| 53 |
+
|
| 54 |
+
st.write("### Visualisasi Distribusi Target: DEATH_EVENT")
|
| 55 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
| 56 |
+
sns.countplot(x=data['DEATH_EVENT'], ax=ax)
|
| 57 |
+
st.pyplot(fig)
|
| 58 |
+
|
| 59 |
+
elif choice == "Prediksi":
|
| 60 |
+
model, scaler, numeric_columns, categorical_columns = load_components()
|
| 61 |
+
user_input = get_user_input(numeric_columns, categorical_columns)
|
| 62 |
+
user_input[numeric_columns] = scaler.transform(user_input[numeric_columns])
|
| 63 |
+
prediction = model.predict(user_input)
|
| 64 |
+
if prediction[0] == 0:
|
| 65 |
+
st.write("Prediksi: Tidak Ada Gagal Jantung")
|
| 66 |
+
else:
|
| 67 |
+
st.write("Prediksi: Gagal Jantung")
|
| 68 |
+
st.subheader("Data Input Pengguna")
|
| 69 |
+
st.write(user_input)
|
| 70 |
+
|
| 71 |
+
if __name__ == "__main__":
|
| 72 |
+
main()
|
categorical_columns.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
["anaemia", "diabetes", "high_blood_pressure", "sex", "smoking"]
|
eda.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import seaborn as sns
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
|
| 6 |
+
# Fungsi untuk memuat data
|
| 7 |
+
def load_data():
|
| 8 |
+
df = pd.read_csv('h8dsft_P1G3_Muhammad_Arief_Kurniawan.csv') # Adjust the path if needed
|
| 9 |
+
return df
|
| 10 |
+
|
| 11 |
+
def main():
|
| 12 |
+
st.title("Eksplorasi Data Analitik")
|
| 13 |
+
|
| 14 |
+
# Load data
|
| 15 |
+
data = load_data()
|
| 16 |
+
|
| 17 |
+
# Display basic data information
|
| 18 |
+
st.write("### Informasi Dasar Data")
|
| 19 |
+
st.write(data.info())
|
| 20 |
+
st.write("### Deskripsi Statistik")
|
| 21 |
+
st.write(data.describe())
|
| 22 |
+
st.write("### 5 Data Pertama")
|
| 23 |
+
st.write(data.head())
|
| 24 |
+
|
| 25 |
+
# Visualization with Streamlit
|
| 26 |
+
st.write("### Visualisasi Distribusi Target: DEATH_EVENT")
|
| 27 |
+
fig, ax = plt.subplots(figsize=(10, 6))
|
| 28 |
+
sns.countplot(x=data['DEATH_EVENT'], ax=ax)
|
| 29 |
+
st.pyplot(fig)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
# You can add more visualizations or features as needed
|
| 33 |
+
|
| 34 |
+
if __name__ == "__main__":
|
| 35 |
+
main()
|
model_scaler.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1ccba0dda6850d57aae324ed74dc8a9b7b65d71a5c478c1ddd1ca682eea6c68b
|
| 3 |
+
size 810
|
numeric_columns.txt
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
["age", "creatinine_phosphokinase", "ejection_fraction", "platelets", "serum_creatinine", "serum_sodium", "time"]
|
prediction.py
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import joblib
|
| 4 |
+
import numpy as np
|
| 5 |
+
import json
|
| 6 |
+
|
| 7 |
+
# Fungsi untuk memuat model dan komponen preprocessing
|
| 8 |
+
def load_components():
|
| 9 |
+
model = joblib.load('stacked_es_model.pkl')
|
| 10 |
+
scaler = joblib.load('model_scaler.pkl')
|
| 11 |
+
with open('numeric_columns.txt', 'r') as file_1:
|
| 12 |
+
numeric_columns = json.load(file_1)
|
| 13 |
+
with open('categorical_columns.txt', 'r') as file_2:
|
| 14 |
+
categorical_columns = json.load(file_2)
|
| 15 |
+
return model, scaler, numeric_columns, categorical_columns
|
| 16 |
+
|
| 17 |
+
# Fungsi untuk mendapatkan input dari pengguna
|
| 18 |
+
def get_user_input(numeric_columns, categorical_columns):
|
| 19 |
+
inputs = {}
|
| 20 |
+
for col in numeric_columns + categorical_columns:
|
| 21 |
+
if col in categorical_columns:
|
| 22 |
+
inputs[col] = st.selectbox(col, [0, 1])
|
| 23 |
+
else:
|
| 24 |
+
# Menggunakan np.expm1 untuk membalikkan transformasi log untuk tampilan dan input
|
| 25 |
+
default_val = np.expm1(50.0) if col != 'time' else 50.0
|
| 26 |
+
inputs[col] = np.log1p(st.number_input(col, value=default_val))
|
| 27 |
+
return pd.DataFrame([inputs])
|
| 28 |
+
|
| 29 |
+
def main():
|
| 30 |
+
st.title("Prediksi Gagal Jantung")
|
| 31 |
+
|
| 32 |
+
# Muat model dan komponen preprocessing
|
| 33 |
+
model, scaler, numeric_columns, categorical_columns = load_components()
|
| 34 |
+
|
| 35 |
+
# Dapatkan input dari pengguna
|
| 36 |
+
user_input = get_user_input(numeric_columns, categorical_columns)
|
| 37 |
+
|
| 38 |
+
# Preprocess input
|
| 39 |
+
user_input[numeric_columns] = scaler.transform(user_input[numeric_columns])
|
| 40 |
+
|
| 41 |
+
# Lakukan prediksi
|
| 42 |
+
prediction = model.predict(user_input)
|
| 43 |
+
if prediction[0] == 0:
|
| 44 |
+
st.write("Prediksi: Tidak Ada Gagal Jantung")
|
| 45 |
+
else:
|
| 46 |
+
st.write("Prediksi: Gagal Jantung")
|
| 47 |
+
|
| 48 |
+
# Opsi: Tampilkan data input (untuk verifikasi)
|
| 49 |
+
st.subheader("Data Input Pengguna")
|
| 50 |
+
st.write(user_input)
|
| 51 |
+
|
| 52 |
+
if __name__ == "__main__":
|
| 53 |
+
main()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
streamlit
|
| 2 |
+
pandas
|
| 3 |
+
seaborn
|
| 4 |
+
matplotlib
|
| 5 |
+
numpy
|
| 6 |
+
scikit-learn==1.2.2
|
stacked_es_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d9053ef824aec6ff6f04fc049702d52b829c992e50e661641c38357dad77ed95
|
| 3 |
+
size 641887
|