-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
73 lines (55 loc) · 2.27 KB
/
app.py
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
import streamlit as st
import pandas as pd
import pickle
# Load the Random Forest model from the pickle file
model = pickle.load(open('randomforest.pkl', 'rb'))
# Define the columns for user input
columns = ['tenure', 'PhoneService', 'Contract',
'PaperlessBilling', 'PaymentMethod', 'MonthlyCharges']
# Create a function to preprocess user input and make predictions
def predict_churn(input_data):
# Preprocess the input data
input_df = pd.DataFrame([input_data], columns=columns)
# Make predictions using the loaded model
prediction = model.predict(input_df)
probability = model.predict_proba(input_df)[:, 1]
return prediction[0], probability[0]
# Create the Streamlit app
def main():
st.title("Telecom Churn Prediction")
st.write("Enter the customer details below to predict churn.")
# Create input fields for user input
tenure = st.slider("Tenure (months)", 0, 100, 1)
phone_service = st.selectbox("Phone Service", [0, 1])
st.write("0: No, 1: Yes")
contract = st.selectbox("Contract", [0, 1, 2])
st.write("0: Month-to-month, 1: One year, 2: Two year")
paperless_billing = st.selectbox("Paperless Billing", [0, 1])
st.write("0: No, 1: Yes")
payment_method = st.selectbox("Payment Method", [0, 1, 2, 3])
st.write("0: Bank transfer (automatic), 1: Credit card (automatic), 2: Electronic check, 3: Mailed check")
monthly_charges = st.number_input("Monthly Charges")
# Create a dictionary to store the user input
input_data = {
'tenure': tenure,
'PhoneService': phone_service,
'Contract': contract,
'PaperlessBilling': paperless_billing,
'PaymentMethod': payment_method,
'MonthlyCharges': monthly_charges
}
# Predict churn based on user input
churn_probability = predict_churn(input_data)
churn_prediction=churn_probability[1]
# Display the prediction
st.subheader("Churn Prediction")
if churn_prediction >= 0.4:
st.write("The customer is likely to churn.")
else:
st.write("The customer is unlikely to churn.")
# Display the churn probability
st.subheader("Churn Probability")
st.write("The probability of churn is:", churn_probability)
# Run the Streamlit app
if __name__ == '__main__':
main()