File size: 3,341 Bytes
754cfcd 2d3963d 754cfcd 2d3963d | 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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | 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")
# Upload dataset
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())
# Drop ID if exists
if "ID" in data.columns:
data = data.drop("ID", axis=1)
# Target
target = "status"
# Split features
X = data.drop(target, axis=1)
y = data[target]
# Identify columns
cat_cols = X.select_dtypes(include="object").columns.tolist()
num_cols = X.select_dtypes(exclude="object").columns.tolist()
# Preprocessing
preprocessor = ColumnTransformer([
("num", StandardScaler(), num_cols),
("cat", OneHotEncoder(handle_unknown="ignore"), cat_cols)
])
# Model
model = Pipeline([
("preprocessor", preprocessor),
("classifier", RandomForestClassifier(random_state=42))
])
# Train-test split
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}")
# Confusion Matrix
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)
# ---------------------------
# EDA SECTION
# ---------------------------
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)
# ---------------------------
# PREDICTION UI
# ---------------------------
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}")
|