CustomerChurn / app.py
DataWizard9742's picture
Create app.py
32133fa
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()