ml_intuitions / app.py
Bhupen
Add ML intuitions py file
c3c5c9a
import streamlit as st
from sklearn.datasets import load_breast_cancer
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, confusion_matrix
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import time
from sklearn.datasets import make_classification, make_circles, make_moons
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
def load_and_train_model():
data = load_breast_cancer()
X = pd.DataFrame(data.data, columns=data.feature_names)
y = pd.Series(data.target)
X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, random_state=42)
model = LogisticRegression(max_iter=10000)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
acc = accuracy_score(y_test, y_pred)
cm = confusion_matrix(y_test, y_pred)
return model, X, y, acc, cm, data
# Helper function to plot decision boundary
def plot_decision_boundary(clf, X, y, ax, title):
h = .02
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
ax.contourf(xx, yy, Z, alpha=0.3)
ax.scatter(X[:, 0], X[:, 1], c=y, edgecolors='k', s=40)
ax.set_title(title)
def main():
st.set_page_config(page_title="Breast Cancer Classifier", layout="wide")
# Load dataset
data = load_breast_cancer()
X = pd.DataFrame(data.data, columns=data.feature_names)
y = pd.Series(data.target)
st.subheader("Breast Cancer Classification with Logistic Regression")
#st.write(f"**Problem Statement**")
with st.expander("📌 What we're trying to accomplish..."):
st.markdown("""
**Classify whether a tumor is malignant or benign** using medical imaging features.
We use **logistic regression** to build a classifier using the Breast Cancer dataset.
**Key Objectives**
1. Data and `labels` for Machine Learning
2. `Discriminative` AI
3. Model training and metrics
4. `Generate new samples` for prediction
5. `Introducing Generative` AI
""")
with st.expander("📘 Why Clean & Labeled Data is Important", expanded=False):
st.markdown("""
**The Role of Clean and Labeled Data in Machine Learning**
- Machine Learning relies on **historical data to learn** patterns and make predictions. If the data is messy, inconsistent, or poorly labeled, the model’s output can be misleading or even harmful.
- **Labels are the foundation.** In supervised learning, we train models using input features and their corresponding correct answers (labels).
- Example: In medical AI:
- **Input**: features from a breast scan (mean texture, perimeter, radius...)
- **Label**: 0 = Malignant, 1 = Benign
- Without labels, the system cannot learn what patterns lead to which outcomes.
- **Labeling requires domain expertise.**
- Healthcare: A radiologist may label tumors
- Finance: An analyst marks transactions as fraudulent or not
- Without expertise, labels may be inconsistent or biased.
- **Discriminative features are key.**
- If malignant tumors tend to have larger mean radius and benign ones have smaller, the model can learn to discriminate effectively.
- If features overlap across classes, the model struggles to make decisions.
- For **Agentic AI systems** — those acting **autonomously on behalf of humans** — poor data quality has real consequences.
- E.g., An AI assistant triaging patients or approving insurance claims may act unfairly if trained on mislabeled or ambiguous data.
- So, **clean, well-labeled, and well-separated data is not just technical hygiene — it’s a leadership mandate** for building trustworthy, safe AI systems.
""")
#st.markdown(f"**Dataset Overview**")
feature_descriptions = {
"radius_mean": "Mean of distances from center to points on the perimeter",
"texture_mean": "Standard deviation of gray-scale values (texture)",
"perimeter_mean": "Mean size of the perimeter of the tumor",
"area_mean": "Mean area of the tumor",
"smoothness_mean": "Mean of local variation in radius lengths",
"compactness_mean": "Mean of (perimeter² / area - 1.0)",
"concavity_mean": "Mean severity of concave portions of the contour",
"concave points_mean": "Mean number of concave portions of the contour",
"symmetry_mean": "Mean symmetry of the tumor shape",
"fractal_dimension_mean": "Mean complexity of the contour (coastline approximation)",
"radius_se": "Standard error of radius",
"texture_se": "Standard error of texture",
"perimeter_se": "Standard error of perimeter",
"area_se": "Standard error of area",
"smoothness_se": "Standard error of smoothness",
"compactness_se": "Standard error of compactness",
"concavity_se": "Standard error of concavity",
"concave points_se": "Standard error of concave points",
"symmetry_se": "Standard error of symmetry",
"fractal_dimension_se": "Standard error of fractal dimension",
"radius_worst": "Largest radius across all scans",
"texture_worst": "Largest texture",
"perimeter_worst": "Largest perimeter",
"area_worst": "Largest area",
"smoothness_worst": "Largest smoothness",
"compactness_worst": "Largest compactness",
"concavity_worst": "Largest concavity",
"concave points_worst": "Largest number of concave points",
"symmetry_worst": "Largest symmetry",
"fractal_dimension_worst": "Largest fractal dimension"
}
# Convert to DataFrame
df_features = pd.DataFrame(
list(feature_descriptions.items()),
columns=["Feature Name", "Description"]
)
# Display inside an expander
with st.expander("🧬 Breast Cancer Dataset Features", expanded=False):
st.dataframe(df_features, use_container_width=True)
# Demo the discriminatory abilities of the features
# Additional expander: Discriminative abilities
with st.expander("🧠 Discriminative Abilities of Features & How ML Depends on Them", expanded=False):
st.markdown("""
**Discriminative features** are those that help distinguish between different classes. ML algorithms learn patterns by identifying such features. If features don't differ across classes, even the best model will struggle to make good predictions.
Here are examples from different domains:
1. **Healthcare (Breast Cancer Detection)**
- *Feature:* `mean radius`, `mean concavity`
- *Discrimination:* Malignant tumors often have higher mean radius and greater concavity. These differences help separate them from benign ones.
2. **Finance (Fraud Detection)**
- *Feature:* `transaction amount`, `location mismatch`, `login time`
- *Discrimination:* Fraudulent transactions are more likely to be high-value, come from unusual locations, or occur at odd hours.
3. **Retail (Customer Churn Prediction)**
- *Feature:* `number of support tickets`, `monthly usage decline`, `payment delays`
- *Discrimination:* Customers likely to churn often show reduced usage, frequent issues, or late payments — all useful predictors for ML models.
These examples underline the importance of **feature engineering** and **domain knowledge** in building effective ML systems.
""")
# Create 3 plots for different domains
fig, axes = plt.subplots(1, 3, figsize=(20, 6))
# 1. Healthcare - Linearly Separable
X1, y1 = make_classification(n_samples=200, n_features=2, n_redundant=0,
n_informative=2, n_clusters_per_class=1, class_sep=2.0, random_state=1)
clf1 = make_pipeline(StandardScaler(), SVC(kernel='linear', C=1.0))
clf1.fit(X1, y1)
plot_decision_boundary(clf1, X1, y1, axes[0], "Healthcare (Linear SVM)")
# 2. Finance - Concentric Circles
X2, y2 = make_circles(n_samples=200, factor=0.5, noise=0.05, random_state=2)
clf2 = make_pipeline(StandardScaler(), SVC(kernel='rbf', C=1.0, gamma='auto'))
clf2.fit(X2, y2)
plot_decision_boundary(clf2, X2, y2, axes[1], "Finance (RBF SVM - Circles)")
# 3. Retail - Semi Circles
X3, y3 = make_moons(n_samples=200, noise=0.1, random_state=3)
clf3 = make_pipeline(StandardScaler(), SVC(kernel='rbf', C=1.0, gamma='auto'))
clf3.fit(X3, y3)
plot_decision_boundary(clf3, X3, y3, axes[2], "Retail (RBF SVM - Moons)")
# Show plot in Streamlit
st.pyplot(fig)
st.markdown("""
When the classes are heavily **intertwined** and not linearly separable, **Logistic Regression** struggles to draw a good boundary. Here's a demonstration.
""")
# Generate an intertwined dataset
X, y = make_classification(
n_samples =200,
n_features =2,
n_redundant =0,
n_informative=2,
n_clusters_per_class=2,
class_sep=0.3, # Low separation
flip_y=0.1, # Add some label noise
random_state=42
)
# Train logistic regression
clf = make_pipeline(StandardScaler(), LogisticRegression())
clf.fit(X, y)
# Plot
fig, ax = plt.subplots(figsize=(8, 6))
plot_decision_boundary(clf, X, y, ax, title='Logistic regression')
ax.set_title("Logistic Regression Decision Boundary - intertwined classes")
st.pyplot(fig)
# feature discrimination
# Load dataset
data = load_breast_cancer()
df = pd.DataFrame(data.data, columns=data.feature_names)
df["target"] = data.target
X = pd.DataFrame(data.data, columns=data.feature_names)
y = pd.Series(data.target)
# List of numeric features
numeric_features = list(df.select_dtypes(include=["float64", "int64"]).columns)
#numeric_features.remove("target")
with st.expander("📊 Explore Feature Discrimination vs Target", expanded=False):
# 3 column layout
col1, col2, col3 = st.columns([1, 2, 2])
with col1:
selected_feature = st.selectbox("Select a numeric feature", options=numeric_features)
with col2:
st.write(f"**Distribution of `{selected_feature}` by Target**")
fig, ax = plt.subplots()
sns.boxplot(data=df, x="target", y=selected_feature, ax=ax)
ax.set_xticklabels(["Malignant (0)", "Benign (1)"])
st.pyplot(fig)
with col3:
mean_diff = df.groupby("target")[selected_feature].mean().diff().abs().iloc[-1]
st.markdown(f"""
**Feature Discrimination Analysis**
- The feature **`{selected_feature}`** shows how values differ between benign and malignant cases.
- On average, the difference between classes for this feature is **{mean_diff:.2f}** units.
- If the box plots show **clear separation** between classes, the feature has high discriminative power.
- Better separation = better feature = higher ML success.
- If there's too much overlap, this feature alone may not contribute much to prediction.
""")
with st.expander("🎯 See Class Summary"):
class_counts = df["target"].value_counts().sort_index()
st.markdown(f"""
- **Class 0 (Malignant)**: {class_counts[0]} samples
- **Class 1 (Benign)**: {class_counts[1]} samples
""")
if st.button("Start the Training"):
# X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, random_state=42)
# model = LogisticRegression(max_iter=10000)
# model.fit(X_train, y_train)
# Display a progress bar
progress_bar = st.progress(0)
status_text = st.empty() # To update the progress with a message
# Split dataset
X_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y, random_state=42)
# Initialize the Logistic Regression model
model = LogisticRegression(max_iter=10000)
# Simulate training with a progress update
for i in range(1, 50): # Simulate 100 steps of training
time.sleep(0.1) # Simulate training time (adjust if needed)
progress_bar.progress(i) # Update progress bar
status_text.text(f"Training... {i}% completed")
# Fit the model after the progress bar finishes
model.fit(X_train, y_train)
# Display a message when training is completed
#st.success("Training completed!")
status_text.text("Training completed!")
y_pred = model.predict(X_test)
accuracy = accuracy_score(y_test, y_pred)
cm = confusion_matrix(y_test, y_pred)
st.success("✅ Training Completed")
with st.expander("📊 Model Performance & Interpretation", expanded=True):
col1, col2 = st.columns(2)
with col1:
st.write("Confusion Matrix:")
fig, ax = plt.subplots()
sns.heatmap(cm, annot=True, fmt="d", cmap="Blues",
xticklabels=data.target_names,
yticklabels=data.target_names,
ax=ax)
ax.set_xlabel("Predicted")
ax.set_ylabel("Actual")
st.pyplot(fig)
with col2:
TP, FN = cm[0]
FP, TN = cm[1]
st.markdown("**🔍 Confusion Matrix Interpretation**")
st.markdown(f"""
- **True Positives (TP)** = {TP}: Model correctly predicted **malignant** tumors.
- **False Negatives (FN)** = {FN}: Model predicted **benign**, but it was actually malignant. ⚠️
- **False Positives (FP)** = {FP}: Model predicted **malignant**, but it was actually benign.
- **True Negatives (TN)** = {TN}: Model correctly predicted **benign** tumors.
#### 📌 Implications:
- **FP (False Alarms)** may lead to unnecessary stress and diagnostic procedures.
- **FN (Missed Diagnoses)** are critical: the model misses malignant tumors — this can delay treatment.
> **Clinical Priority:** Minimize **False Negatives**, even if it slightly increases False Positives. This ensures high **recall** and safer patient outcomes.
""")
st.write(f"Accuracy: **{accuracy:.4f}**")
# What variables are the model’s biggest influencers?
with st.expander("🔍 What variables are the model’s biggest influencers?"):
n_top = 8
st.markdown("""
The model doesn't just make predictions — it **prioritizes certain variables** that strongly influence whether it flags a tumor as benign or malignant.
These are the top {n_top} influential features:
""")
coef = model.coef_[0]
feature_importance = pd.Series(np.abs(coef), index=X_train.columns)
top_features = feature_importance.sort_values(ascending=False).head(n_top)
fig, ax = plt.subplots()
sns.barplot(x=top_features.values, y=top_features.index, palette="viridis", ax=ax)
ax.set_xlabel("Importance Score")
ax.set_title("Top Influential Features in Prediction")
st.pyplot(fig)
st.markdown("""
- **Why this matters :** These features highlight **what the model pays the most attention to**.
- It helps **domain experts** verify if the model’s focus makes clinical sense.
- For example, `concavity_worst` and `radius_mean` reflect how irregular or large a tumor appears — both key medical indicators.
This kind of **transparency builds trust** in AI decisions.
""")
st.session_state["model"] = model
st.session_state["X"] = X
st.session_state["y"] = y
st.session_state["target_names"] = data.target_names
if "model" in st.session_state:
st.markdown(f"**Test the model ...**")
selected_class = st.radio("Generate sample for class:", options=[0, 1],
format_func=lambda x: st.session_state["target_names"][x])
if st.button("🎲 Generate new Sample (Synthetic) for prediction ..."):
# 🔑 Get the trained model first
model = st.session_state["model"]
class_data = st.session_state["X"][st.session_state["y"] == selected_class]
# Compute means and stds
means = class_data.mean()
stds = class_data.std()
# Replace any NaN stds with 0 to avoid generating NaNs
stds = stds.fillna(0)
# Add small random noise (scaled by std dev)
noise = np.random.normal(loc=0, scale=stds * 0.1)
# Generate sample with proper structure
sample_dict = (means + noise).to_dict()
sample_df = pd.DataFrame([sample_dict], columns=model.feature_names_in_).astype(float)
st.write(f'Generated 1 sample for class : {selected_class}')
# Display in 2-column layout
col1, col2 = st.columns([1, 1])
with col1:
st.markdown("**🧪 How Was This Sample Generated?**")
st.markdown(f"""
We used a method called **Class-Conditioned Sampling**, where:
- We calculate the **average feature values** (mean) for all training samples belonging to class **`{selected_class}`**.
- Then we add **small random noise** (proportional to feature-wise standard deviation) to make it realistic.
""")
with col2:
st.markdown(f"""
🔍 **Example with 2 Features:**
Suppose for `mean radius` and `mean texture`, we had:
- Class `{selected_class}` averages:
`mean radius` = 14.2, `mean texture` = 20.1
- Feature std devs:
`mean radius` = 3.2, `mean texture` = 2.5
- We generate:
`mean radius` ≈ 14.2 ± (0.1 × 3.2) → ~14.4
`mean texture` ≈ 20.1 ± (0.1 × 2.5) → ~20.3
✨ This creates a **new, plausible sample** that belongs to class `{selected_class}`.
""")
# Try to predict
try:
prediction = model.predict(sample_df)[0]
proba = model.predict_proba(sample_df)[0]
#st.success(f"🎯 Predicted Class: {prediction} with probability {max(proba):.2f}")
except Exception as e:
st.error(f"🔥 Prediction failed: {e}")
st.markdown(f"**Prediction:** {prediction} ({st.session_state['target_names'][prediction]})")
st.markdown(f"**Probability:** Malignant: `{proba[0]:.3f}`, Benign: `{proba[1]:.3f}`")
with st.expander("🧠 How This Relates to Generative AI"):
st.markdown("""
##### What We’re Doing Here
You're generating **new synthetic samples** that resemble real ones from a particular class by using:
- The **mean** of real data (signal)
- A bit of **noise** (variation)
This is a basic form of how **Generative AI (GenAI)** works.
---
##### What is Generative AI?
> **Generative AI** refers to a class of AI models that can **generate new content** — like text, images, or data samples — that resemble what they’ve seen during training.
It does this by:
- Learning the **distribution** of real data
- Sampling from that distribution to create **new but realistic** outputs
---
##### Why this Method Counts
This "Class-Conditioned Sampling" is like a **simple statistical GenAI**:
- You use **mean and std dev** to describe the feature distribution for a class.
- You **generate new samples** by adding random noise to the means.
So even without deep learning or neural networks, you're applying the **core idea of GenAI**.
---
##### 🧪 the connection ...
> Before diving into large GenAI models like GPT or DALL·E, this gives us a simple, intuitive feel of what it means to "generate" something new based on existing patterns.
""")
if __name__ == "__main__":
main()