Spaces:
Sleeping
Sleeping
Redeploy
Browse files- app.py +28 -20
- gb_model.pkl +3 -0
- model_scaler.pkl +1 -1
- numeric_columns.txt +1 -1
- prediction.py +30 -25
- requirements.txt +1 -1
- rf_model.pkl +3 -0
- stacked_es_model.pkl +2 -2
app.py
CHANGED
|
@@ -1,27 +1,27 @@
|
|
| 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 |
-
#
|
| 10 |
def load_data():
|
| 11 |
df = pd.read_csv('h8dsft_P1G3_Muhammad_Arief_Kurniawan.csv')
|
| 12 |
return df
|
| 13 |
|
| 14 |
-
#
|
| 15 |
def load_components():
|
| 16 |
-
|
|
|
|
|
|
|
| 17 |
scaler = joblib.load('model_scaler.pkl')
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
return model, scaler, numeric_columns, categorical_columns
|
| 23 |
|
| 24 |
-
#
|
| 25 |
def get_user_input(numeric_columns, categorical_columns):
|
| 26 |
inputs = {}
|
| 27 |
for col in numeric_columns + categorical_columns:
|
|
@@ -30,12 +30,14 @@ def get_user_input(numeric_columns, categorical_columns):
|
|
| 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 |
-
|
|
|
|
|
|
|
| 34 |
|
| 35 |
def main():
|
| 36 |
st.title("Prediksi dan Eksplorasi Gagal Jantung")
|
| 37 |
|
| 38 |
-
# Sidebar
|
| 39 |
choice = st.sidebar.selectbox("Pilih Halaman", ["Beranda", "Eksplorasi Data", "Prediksi"])
|
| 40 |
|
| 41 |
if choice == "Beranda":
|
|
@@ -57,14 +59,20 @@ def main():
|
|
| 57 |
st.pyplot(fig)
|
| 58 |
|
| 59 |
elif choice == "Prediksi":
|
| 60 |
-
|
| 61 |
user_input = get_user_input(numeric_columns, categorical_columns)
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
st.subheader("Data Input Pengguna")
|
| 69 |
st.write(user_input)
|
| 70 |
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
|
|
|
| 3 |
import joblib
|
| 4 |
+
import numpy as np
|
| 5 |
import seaborn as sns
|
| 6 |
import matplotlib.pyplot as plt
|
|
|
|
| 7 |
|
| 8 |
+
# Function to load data
|
| 9 |
def load_data():
|
| 10 |
df = pd.read_csv('h8dsft_P1G3_Muhammad_Arief_Kurniawan.csv')
|
| 11 |
return df
|
| 12 |
|
| 13 |
+
# Function to load model and preprocessing components
|
| 14 |
def load_components():
|
| 15 |
+
rf_model = joblib.load('rf_model.pkl')
|
| 16 |
+
gb_model = joblib.load('gb_model.pkl')
|
| 17 |
+
stacked_model = joblib.load('stacked_es_model.pkl')
|
| 18 |
scaler = joblib.load('model_scaler.pkl')
|
| 19 |
+
numeric_columns = ['age', 'creatinine_phosphokinase', 'ejection_fraction', 'platelets', 'serum_creatinine', 'serum_sodium', 'time']
|
| 20 |
+
categorical_columns = ['anaemia', 'diabetes', 'high_blood_pressure', 'sex', 'smoking']
|
| 21 |
+
trained_features = rf_model.feature_names_in_
|
| 22 |
+
return rf_model, gb_model, stacked_model, scaler, numeric_columns, categorical_columns, trained_features
|
|
|
|
| 23 |
|
| 24 |
+
# Function to get user input
|
| 25 |
def get_user_input(numeric_columns, categorical_columns):
|
| 26 |
inputs = {}
|
| 27 |
for col in numeric_columns + categorical_columns:
|
|
|
|
| 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 |
+
user_data = pd.DataFrame([inputs])
|
| 34 |
+
user_data = user_data[numeric_columns + categorical_columns]
|
| 35 |
+
return user_data
|
| 36 |
|
| 37 |
def main():
|
| 38 |
st.title("Prediksi dan Eksplorasi Gagal Jantung")
|
| 39 |
|
| 40 |
+
# Sidebar for navigation
|
| 41 |
choice = st.sidebar.selectbox("Pilih Halaman", ["Beranda", "Eksplorasi Data", "Prediksi"])
|
| 42 |
|
| 43 |
if choice == "Beranda":
|
|
|
|
| 59 |
st.pyplot(fig)
|
| 60 |
|
| 61 |
elif choice == "Prediksi":
|
| 62 |
+
rf_model, gb_model, stacked_model, scaler, numeric_columns, categorical_columns, trained_features = load_components()
|
| 63 |
user_input = get_user_input(numeric_columns, categorical_columns)
|
| 64 |
+
user_input_ordered = user_input[trained_features].to_numpy()
|
| 65 |
+
user_input_ordered[:, :len(numeric_columns)] = scaler.transform(user_input_ordered[:, :len(numeric_columns)])
|
| 66 |
+
|
| 67 |
+
# Predictions
|
| 68 |
+
rf_prediction = rf_model.predict(user_input_ordered)
|
| 69 |
+
gb_prediction = gb_model.predict(user_input_ordered)
|
| 70 |
+
stacked_prediction = stacked_model.predict(user_input_ordered)
|
| 71 |
+
|
| 72 |
+
st.write(f"Random Forest Prediction: {'Gagal Jantung' if rf_prediction[0] == 1 else 'Tidak Ada Gagal Jantung'}")
|
| 73 |
+
st.write(f"Gradient Boosting Prediction: {'Gagal Jantung' if gb_prediction[0] == 1 else 'Tidak Ada Gagal Jantung'}")
|
| 74 |
+
st.write(f"Stacked Model Prediction: {'Gagal Jantung' if stacked_prediction[0] == 1 else 'Tidak Ada Gagal Jantung'}")
|
| 75 |
+
|
| 76 |
st.subheader("Data Input Pengguna")
|
| 77 |
st.write(user_input)
|
| 78 |
|
gb_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:9b56ea99334252e1a2cfac5a4e78adc4f70a76ddc8c5049d3f7597da7457de34
|
| 3 |
+
size 242044
|
model_scaler.pkl
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
size 810
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3cba4d82866e259a46c8b31577581f032e6ea1cad13ff97960e019be10c91200
|
| 3 |
size 810
|
numeric_columns.txt
CHANGED
|
@@ -1 +1 @@
|
|
| 1 |
-
["age", "creatinine_phosphokinase", "ejection_fraction", "platelets", "serum_creatinine", "serum_sodium", "time"]
|
|
|
|
| 1 |
+
["age", "creatinine_phosphokinase", "ejection_fraction", "platelets", "serum_creatinine", "serum_sodium", "time"]
|
prediction.py
CHANGED
|
@@ -2,56 +2,61 @@ 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 |
-
|
|
|
|
|
|
|
| 10 |
scaler = joblib.load('model_scaler.pkl')
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
|
|
|
| 16 |
|
| 17 |
-
#
|
| 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 |
-
# Using np.expm1 to reverse the log transformation for display and 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 |
-
user_df = pd.DataFrame([inputs])
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
|
| 33 |
def main():
|
| 34 |
st.title("Prediksi Gagal Jantung")
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
|
| 39 |
-
#
|
| 40 |
user_input = get_user_input(numeric_columns, categorical_columns)
|
| 41 |
|
|
|
|
|
|
|
|
|
|
| 42 |
# Preprocess input
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
if
|
| 48 |
-
st.write("Prediksi: Tidak Ada Gagal Jantung")
|
| 49 |
-
else:
|
| 50 |
-
st.write("Prediksi: Gagal Jantung")
|
| 51 |
|
| 52 |
-
#
|
| 53 |
st.subheader("Data Input Pengguna")
|
| 54 |
st.write(user_input)
|
| 55 |
|
| 56 |
if __name__ == "__main__":
|
| 57 |
main()
|
|
|
|
|
|
| 2 |
import pandas as pd
|
| 3 |
import joblib
|
| 4 |
import numpy as np
|
|
|
|
| 5 |
|
|
|
|
| 6 |
def load_components():
|
| 7 |
+
rf_model = joblib.load('rf_model.pkl')
|
| 8 |
+
gb_model = joblib.load('gb_model.pkl')
|
| 9 |
+
stacked_model = joblib.load('stacked_es_model.pkl')
|
| 10 |
scaler = joblib.load('model_scaler.pkl')
|
| 11 |
+
numeric_columns = ['age', 'creatinine_phosphokinase', 'ejection_fraction', 'platelets', 'serum_creatinine', 'serum_sodium', 'time']
|
| 12 |
+
categorical_columns = ['anaemia', 'diabetes', 'high_blood_pressure', 'sex', 'smoking']
|
| 13 |
+
|
| 14 |
+
trained_features = rf_model.feature_names_in_ # Extract feature names from the model
|
| 15 |
+
|
| 16 |
+
return rf_model, gb_model, stacked_model, scaler, numeric_columns, categorical_columns, trained_features
|
| 17 |
|
| 18 |
+
# Function to get user input
|
| 19 |
def get_user_input(numeric_columns, categorical_columns):
|
| 20 |
inputs = {}
|
| 21 |
for col in numeric_columns + categorical_columns:
|
| 22 |
if col in categorical_columns:
|
| 23 |
inputs[col] = st.selectbox(col, [0, 1])
|
| 24 |
else:
|
|
|
|
| 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 |
|
| 28 |
+
user_data = pd.DataFrame([inputs])
|
| 29 |
+
user_data = user_data[numeric_columns + categorical_columns] # Ensure correct column order
|
| 30 |
+
return user_data
|
| 31 |
|
| 32 |
def main():
|
| 33 |
st.title("Prediksi Gagal Jantung")
|
| 34 |
|
| 35 |
+
# Load models and preprocessing components
|
| 36 |
+
rf_model, gb_model, stacked_model, scaler, numeric_columns, categorical_columns, trained_features = load_components()
|
| 37 |
|
| 38 |
+
# Get user input
|
| 39 |
user_input = get_user_input(numeric_columns, categorical_columns)
|
| 40 |
|
| 41 |
+
# Reorder columns to match trained features and convert to numpy array
|
| 42 |
+
user_input_ordered = user_input[trained_features].to_numpy()
|
| 43 |
+
|
| 44 |
# Preprocess input
|
| 45 |
+
user_input_ordered[:, :len(numeric_columns)] = scaler.transform(user_input_ordered[:, :len(numeric_columns)])
|
| 46 |
+
|
| 47 |
+
# Make predictions using both models
|
| 48 |
+
rf_prediction = rf_model.predict(user_input_ordered)
|
| 49 |
+
gb_prediction = gb_model.predict(user_input_ordered)
|
| 50 |
+
stacked_prediction = stacked_model.predict(user_input_ordered)
|
| 51 |
|
| 52 |
+
st.write(f"Random Forest Prediction: {'Gagal Jantung' if rf_prediction[0] == 1 else 'Tidak Ada Gagal Jantung'}")
|
| 53 |
+
st.write(f"Gradient Boosting Prediction: {'Gagal Jantung' if gb_prediction[0] == 1 else 'Tidak Ada Gagal Jantung'}")
|
| 54 |
+
st.write(f"stacked Prediction: {'Gagal Jantung' if stacked_prediction [0] == 1 else 'Tidak Ada Gagal Jantung'}")
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
# Option: Display user input data (for verification)
|
| 57 |
st.subheader("Data Input Pengguna")
|
| 58 |
st.write(user_input)
|
| 59 |
|
| 60 |
if __name__ == "__main__":
|
| 61 |
main()
|
| 62 |
+
|
requirements.txt
CHANGED
|
@@ -3,4 +3,4 @@ pandas
|
|
| 3 |
seaborn
|
| 4 |
matplotlib
|
| 5 |
numpy
|
| 6 |
-
scikit-learn
|
|
|
|
| 3 |
seaborn
|
| 4 |
matplotlib
|
| 5 |
numpy
|
| 6 |
+
scikit-learn==1.2.2
|
rf_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ba7553ca39091bcf03b2a38d8ef4753f881828d206d0e8396018e31120e20758
|
| 3 |
+
size 761193
|
stacked_es_model.pkl
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:4bfb4266106c8923e3e14fa886edc329eb3dcf8d026b784317d7fb25bfa844a2
|
| 3 |
+
size 632416
|