Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import pandas as pd | |
| import joblib | |
| import numpy as np | |
| import seaborn as sns | |
| import matplotlib.pyplot as plt | |
| # Function to load data | |
| def load_data(): | |
| df = pd.read_csv('h8dsft_P1G3_Muhammad_Arief_Kurniawan.csv') | |
| return df | |
| # Function to load model and preprocessing components | |
| def load_components(): | |
| rf_model = joblib.load('rf_model.pkl') | |
| gb_model = joblib.load('gb_model.pkl') | |
| stacked_model = joblib.load('stacked_es_model.pkl') | |
| scaler = joblib.load('model_scaler.pkl') | |
| numeric_columns = ['age', 'creatinine_phosphokinase', 'ejection_fraction', 'platelets', 'serum_creatinine', 'serum_sodium', 'time'] | |
| categorical_columns = ['anaemia', 'diabetes', 'high_blood_pressure', 'sex', 'smoking'] | |
| trained_features = rf_model.feature_names_in_ | |
| return rf_model, gb_model, stacked_model, scaler, numeric_columns, categorical_columns, trained_features | |
| # Function to get user input | |
| def get_user_input(numeric_columns, categorical_columns): | |
| inputs = {} | |
| for col in numeric_columns + categorical_columns: | |
| if col in categorical_columns: | |
| inputs[col] = st.selectbox(col, [0, 1]) | |
| else: | |
| default_val = np.expm1(50.0) if col != 'time' else 50.0 | |
| inputs[col] = np.log1p(st.number_input(col, value=default_val)) | |
| user_data = pd.DataFrame([inputs]) | |
| user_data = user_data[numeric_columns + categorical_columns] | |
| return user_data | |
| def main(): | |
| st.title("Prediksi dan Eksplorasi Gagal Jantung") | |
| # Sidebar for navigation | |
| choice = st.sidebar.selectbox("Pilih Halaman", ["Beranda", "Eksplorasi Data", "Prediksi"]) | |
| if choice == "Beranda": | |
| st.write("Selamat datang di aplikasi prediksi dan eksplorasi gagal jantung.") | |
| st.write("Silakan pilih halaman di sidebar untuk melanjutkan.") | |
| elif choice == "Eksplorasi Data": | |
| data = load_data() | |
| st.write("### Informasi Dasar Data") | |
| st.write(data.info()) | |
| st.write("### Deskripsi Statistik") | |
| st.write(data.describe()) | |
| st.write("### 5 Data Pertama") | |
| st.write(data.head()) | |
| st.write("### Visualisasi Distribusi Target: DEATH_EVENT") | |
| fig, ax = plt.subplots(figsize=(10, 6)) | |
| sns.countplot(x=data['DEATH_EVENT'], ax=ax) | |
| st.pyplot(fig) | |
| elif choice == "Prediksi": | |
| rf_model, gb_model, stacked_model, scaler, numeric_columns, categorical_columns, trained_features = load_components() | |
| user_input = get_user_input(numeric_columns, categorical_columns) | |
| user_input_ordered = user_input[trained_features].to_numpy() | |
| user_input_ordered[:, :len(numeric_columns)] = scaler.transform(user_input_ordered[:, :len(numeric_columns)]) | |
| # Predictions | |
| rf_prediction = rf_model.predict(user_input_ordered) | |
| gb_prediction = gb_model.predict(user_input_ordered) | |
| stacked_prediction = stacked_model.predict(user_input_ordered) | |
| st.write(f"Random Forest Prediction: {'Gagal Jantung' if rf_prediction[0] == 1 else 'Tidak Ada Gagal Jantung'}") | |
| st.write(f"Gradient Boosting Prediction: {'Gagal Jantung' if gb_prediction[0] == 1 else 'Tidak Ada Gagal Jantung'}") | |
| st.write(f"Stacked Model Prediction: {'Gagal Jantung' if stacked_prediction[0] == 1 else 'Tidak Ada Gagal Jantung'}") | |
| st.subheader("Data Input Pengguna") | |
| st.write(user_input) | |
| if __name__ == "__main__": | |
| main() | |