File size: 3,524 Bytes
4e775a5
 
 
e5f994d
4e775a5
 
e5f994d
 
 
 
5a559c9
4e775a5
5a559c9
4e775a5
51e74d6
4e775a5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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 = 'joblibmodel_rfbest_pipe_rfbest_pipe_rfbest_pipe_rf.pkl'
with open(model_path, 'rb') as file:
    model = joblib.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}")