Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| import pandas as pd | |
| import pickle | |
| with open ('best_model_lr.pkl', 'rb') as file_1: | |
| model_lr = pickle.load(file_1) | |
| with open ('best_model_svm.pkl', 'rb') as file_2: | |
| model_svm = pickle.load(file_2) | |
| with open ('best_model_knn.pkl', 'rb') as file_3: | |
| model_knn = pickle.load(file_3) | |
| def predict_next_payment(model, input_data): | |
| prediction = model.predict(input_data) | |
| return prediction | |
| def main(): | |
| st.title('Payment Model Predictor') | |
| input_fields = [ | |
| ('Age', st.slider('Age', min_value=18, max_value=70)), | |
| ('Sex', st.selectbox('Sex', [1, 2])), | |
| ('Limit Balance', st.number_input('Limit Balance', min_value=10000, max_value=1000000, value=10000)), | |
| ('Education Level', st.selectbox('Education Level', [1, 2, 3, 4, 5, 6])), | |
| ('Marital Status', st.selectbox('Marital Status', [0, 1, 2, 3])), | |
| ('Pay 1', st.selectbox('Pay 1', [-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8])), | |
| ('Pay 2', st.selectbox('Pay 2', [-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8])), | |
| ('Pay 3', st.selectbox('Pay 3', [-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8])), | |
| ('Pay 4', st.selectbox('Pay 4', [-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8])), | |
| ('Pay 5', st.selectbox('Pay 5', [-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8])), | |
| ('Pay 6', st.selectbox('Pay 6', [-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8])), | |
| ('Bill 1', st.number_input('Bill 1', min_value=-100000, max_value=800000, value=10000)), | |
| ('Bill 2', st.number_input('Bill 2', min_value=-100000, max_value=800000, value=10000)), | |
| ('Bill 3', st.number_input('Bill 3', min_value=-100000, max_value=800000, value=10000)), | |
| ('Bill 4', st.number_input('Bill 4', min_value=-100000, max_value=800000, value=10000)), | |
| ('Bill 5', st.number_input('Bill 5', min_value=-100000, max_value=800000, value=10000)), | |
| ('Bill 6', st.number_input('Bill 6', min_value=-100000, max_value=800000, value=10000)), | |
| ('Pay Amount 1', st.number_input('Pay Amount 1', min_value=0, max_value=2000000, value=10000)), | |
| ('Pay Amount 2', st.number_input('Pay Amount 2', min_value=0, max_value=2000000, value=10000)), | |
| ('Pay Amount 3', st.number_input('Pay Amount 3', min_value=0, max_value=2000000, value=10000)), | |
| ('Pay Amount 4', st.number_input('Pay Amount 4', min_value=0, max_value=2000000, value=10000)), | |
| ('Pay Amount 5', st.number_input('Pay Amount 5', min_value=0, max_value=2000000, value=10000)), | |
| ('Pay Amount 6', st.number_input('Pay Amount 6', min_value=0, max_value=2000000, value=10000)), | |
| ] | |
| # Pemilihan model dengan dropdown. | |
| model_choice = st.selectbox('Select Model', ('Logistic Regression', 'Support Vector Machine', 'K-Nearest Neighbors')) | |
| prediction = None | |
| # Tombol memilih metode prediksi. | |
| if st.button('Predict'): | |
| input_data_values = [field[1] for field in input_fields] | |
| input_data_array = [input_data_values] | |
| if model_choice == 'Logistic Regression': | |
| prediction = predict_next_payment(model_lr, input_data_array) | |
| elif model_choice == 'Support Vector Machine': | |
| prediction = predict_next_payment(model_svm, input_data_array) | |
| elif model_choice == 'K-Nearest Neighbors': | |
| prediction = predict_next_payment(model_knn, input_data_array) | |
| if prediction is not None: | |
| columns = [field[0] for field in input_fields] + ['Prediction'] | |
| data_values = input_data_values + [prediction[0]] | |
| data = [data_values] | |
| input_data_df = pd.DataFrame(data, columns=columns) | |
| st.subheader('Prediction Results') | |
| st.dataframe(input_data_df) | |
| if __name__ == '__main__': | |
| main() |