File size: 1,446 Bytes
32133fa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score,confusion_matrix

@st.cache_data
def load_data():
    df = pd.read_csv('Customer_newData.csv')
    return df

def train_model(df):
   x = df.drop(['Churn'],axis=1)
   y = df['Churn']
   x_train , x_test , y_train , y_test = train_test_split(x,y,test_size=0.25)
   model = RandomForestClassifier()
   model.fit(x_train,y_train)
   y_pred = model.predict(x_test)
   accuracy = accuracy_score(y_test,y_pred)
   cm = confusion_matrix(y_test,y_pred)
   return model,accuracy,cm

def main():
    st.title("Customer Churn Prediction")
    df = load_data()
    model, accuracy,cm = train_model(df)
    st.write("Accuracy of the model is: ",accuracy)
    st.write("Confusion matrix")
    st.write(cm)

    st.sidebar.title(" Try your Inputs ")
    new_data = {}
    for column in df.columns:
        if column != 'Churn':
            value = st.sidebar.text_input(column)
            new_data[column] = value
        else:
            continue

    new_df = pd.DataFrame([new_data])

    if st.sidebar.button("perdict Churn"):
        prediction = model.predict(new_df)
        if prediction == 0:
            print(st.write("The customer will not leave"))
        else:
            print(st.write("The customer might quit"))

if __name__ == '__main__':
    main()