CustChurn / app.py
mnurbani's picture
Update app.py
51e74d6 verified
raw
history blame
3.52 kB
import streamlit as st
import joblib
import pandas as pd
import pickle
# Load model from file
# model_path = 'joblibmodel_rfbest_pipe_rfbest_pipe_rfbest_pipe_rf.pkl'
# with open(model_path, 'rb') as file:
# model = joblib.load(file)
# Load model from file
model_path = 'model_rfbest_pipe_rfbest_pipe_rfbest_pipe_rf.pkl'
with open(model_path, 'rb') as file:
model = pickle.load(file)
# model_path = 'joblibmodel_rfbest_pipe_rfbest_pipe_rfbest_pipe_rf.pkl'
# model = pickle.load('model_rfbest_pipe_rfbest_pipe_rfbest_pipe_rf.pkl')
# Judul aplikasi
st.title("Prediksi Churn Pelanggan")
# Form untuk input data
st.subheader("Masukkan Data Pelanggan")
# Input data pelanggan
gender = st.selectbox('Gender', ['Female', 'Male'])
senior_citizen = st.selectbox('Senior Citizen', [0, 1])
partner = st.selectbox('Partner', ['Yes', 'No'])
dependents = st.selectbox('Dependents', ['Yes', 'No'])
tenure = st.number_input('Tenure (bulan)', min_value=0, max_value=72, value=45)
phone_service = st.selectbox('Phone Service', ['Yes', 'No'])
multiple_lines = st.selectbox('Multiple Lines', ['Yes', 'No'])
internet_service = st.selectbox('Internet Service', ['DSL', 'Fiber optic', 'No'])
online_security = st.selectbox('Online Security', ['Yes', 'No'])
online_backup = st.selectbox('Online Backup', ['Yes', 'No'])
device_protection = st.selectbox('Device Protection', ['Yes', 'No'])
tech_support = st.selectbox('Tech Support', ['Yes', 'No'])
streaming_tv = st.selectbox('Streaming TV', ['Yes', 'No'])
streaming_movies = st.selectbox('Streaming Movies', ['Yes', 'No'])
contract = st.selectbox('Contract', ['Month-to-month', 'One year', 'Two year'])
paperless_billing = st.selectbox('Paperless Billing', ['Yes', 'No'])
payment_method = st.selectbox('Payment Method', ['Electronic check', 'Mailed check', 'Bank transfer (automatic)', 'Credit card (automatic)'])
monthly_charges = st.number_input('Monthly Charges', min_value=0.0, value=70.35)
total_charges = st.number_input('Total Charges', min_value=0.0, value=346.45)
# Membuat DataFrame dari input
data_baru = {
'gender': [gender],
'SeniorCitizen': [senior_citizen],
'Partner': [partner],
'Dependents': [dependents],
'tenure': [tenure],
'PhoneService': [phone_service],
'MultipleLines': [multiple_lines],
'InternetService': [internet_service],
'OnlineSecurity': [online_security],
'OnlineBackup': [online_backup],
'DeviceProtection': [device_protection],
'TechSupport': [tech_support],
'StreamingTV': [streaming_tv],
'StreamingMovies': [streaming_movies],
'Contract': [contract],
'PaperlessBilling': [paperless_billing],
'PaymentMethod': [payment_method],
'MonthlyCharges': [monthly_charges],
'TotalCharges': [total_charges]
}
df_baru = pd.DataFrame(data_baru)
# Melakukan encoding pada data kategorikal
categorical_columns = df_baru.select_dtypes(include=['object']).columns
df_baru = pd.get_dummies(df_baru, columns=categorical_columns, drop_first=True)
# Menampilkan data yang dimasukkan pengguna
st.subheader("Data Pelanggan yang Dimasukkan:")
st.write(df_baru)
# Tombol untuk melakukan prediksi
if st.button('Prediction'):
# Prediksi churn
prediksi = model.predict(df_baru)
# Menampilkan hasil prediksi
if prediksi[0] == 1:
hasil = 'Yes'
else:
hasil = 'No'
st.subheader(f"Hasil Prediksi Churn: {hasil}")
# Probabilitas churn
probabilitas = model.predict_proba(df_baru)[:, 1]
st.subheader(f"Probabilitas Churn: {probabilitas[0]:.2f}")