| import streamlit as st |
| import pandas as pd |
| import numpy as np |
| import joblib |
| import matplotlib.pyplot as plt |
| import seaborn as sns |
|
|
| from sklearn.model_selection import train_test_split |
| from sklearn.pipeline import Pipeline |
| from sklearn.compose import ColumnTransformer |
| from sklearn.preprocessing import OneHotEncoder, StandardScaler |
| from sklearn.ensemble import RandomForestClassifier |
| from sklearn.metrics import accuracy_score, confusion_matrix |
|
|
| st.set_page_config(page_title="Lead Conversion Predictor", layout="wide") |
|
|
| st.title("π ExtraaLearn Lead Conversion Predictor") |
|
|
| |
| uploaded_file = st.file_uploader("Upload CSV Dataset", type=["csv"]) |
|
|
| if uploaded_file: |
| data = pd.read_csv(uploaded_file) |
| st.subheader("Dataset Preview") |
| st.dataframe(data.head()) |
|
|
| |
| if "ID" in data.columns: |
| data = data.drop("ID", axis=1) |
|
|
| |
| target = "status" |
|
|
| |
| X = data.drop(target, axis=1) |
| y = data[target] |
|
|
| |
| cat_cols = X.select_dtypes(include="object").columns.tolist() |
| num_cols = X.select_dtypes(exclude="object").columns.tolist() |
|
|
| |
| preprocessor = ColumnTransformer([ |
| ("num", StandardScaler(), num_cols), |
| ("cat", OneHotEncoder(handle_unknown="ignore"), cat_cols) |
| ]) |
|
|
| |
| model = Pipeline([ |
| ("preprocessor", preprocessor), |
| ("classifier", RandomForestClassifier(random_state=42)) |
| ]) |
|
|
| |
| X_train, X_test, y_train, y_test = train_test_split( |
| X, y, test_size=0.2, random_state=42 |
| ) |
|
|
| model.fit(X_train, y_train) |
|
|
| preds = model.predict(X_test) |
| acc = accuracy_score(y_test, preds) |
|
|
| st.success(f"Model Accuracy: {acc:.2f}") |
|
|
| |
| st.subheader("Confusion Matrix") |
| cm = confusion_matrix(y_test, preds) |
|
|
| fig, ax = plt.subplots() |
| sns.heatmap(cm, annot=True, fmt="d", ax=ax) |
| st.pyplot(fig) |
|
|
| |
| |
| |
| st.subheader("π Exploratory Data Analysis") |
|
|
| col1, col2 = st.columns(2) |
|
|
| with col1: |
| st.write("### Age Distribution") |
| fig, ax = plt.subplots() |
| sns.histplot(data["age"], kde=True, ax=ax) |
| st.pyplot(fig) |
|
|
| with col2: |
| st.write("### Website Visits") |
| fig, ax = plt.subplots() |
| sns.histplot(data["website_visits"], kde=True, ax=ax) |
| st.pyplot(fig) |
|
|
| st.write("### Conversion by Occupation") |
| fig, ax = plt.subplots() |
| sns.countplot(data=data, x="current_occupation", hue="status", ax=ax) |
| st.pyplot(fig) |
|
|
| |
| |
| |
| st.subheader("π― Predict New Lead") |
|
|
| input_data = {} |
|
|
| for col in X.columns: |
| if col in cat_cols: |
| input_data[col] = st.selectbox(col, data[col].unique()) |
| else: |
| input_data[col] = st.number_input(col, float(data[col].min()), float(data[col].max())) |
|
|
| if st.button("Predict"): |
| input_df = pd.DataFrame([input_data]) |
| prediction = model.predict(input_df)[0] |
| prob = model.predict_proba(input_df)[0][1] |
|
|
| st.success(f"Prediction: {'Converted' if prediction == 1 else 'Not Converted'}") |
| st.info(f"Conversion Probability: {prob:.2f}") |
|
|