CoolFace
Apppublic

Harshitha-01/LogisticRegression

sourceHugging Faceupdated 1y agoView on Hugging Face
0likes
app.py139 linesDownload Raw Back to root
1import streamlit as st2import pandas as pd3import numpy as np4import matplotlib.pyplot as plt5import seaborn as sns6from sklearn.datasets import load_iris7from sklearn.model_selection import train_test_split8from sklearn.linear_model import LogisticRegression9from sklearn.preprocessing import StandardScaler10from sklearn.metrics import accuracy_score, classification_report11 12# Streamlit Page Setup13st.set_page_config(page_title="Logistic Regression", layout="wide")14st.title("๐Ÿ“ˆ Logistic Regression")15 16# Tabs17tab1, tab2, tab3 = st.tabs(["๐Ÿ“˜ Introduction", "๐Ÿ”ฌ Train Model", "๐Ÿ“Š Results & Visualization"])18 19# ----------- TAB 1: INTRODUCTION -----------20with tab1:21    st.header("๐Ÿค– What is Logistic Regression?")22    st.markdown("""23    Logistic Regression is a **classification algorithm** that models the probability of class membership.24 25    ---26    ### ๐Ÿง  Key Concepts:27    - Computes a **weighted sum** of inputs28    - Applies the **sigmoid function** to produce probabilities29    - Uses a **threshold** (e.g. 0.5) for classification30    - Handles **multiclass problems** using One-vs-Rest or softmax31 32    ---33    ### โš™ Advantages:34    - Simple and easy to implement35    - Interpretable outputs (probabilities)36    - Fast training, especially for linearly separable data37 38    ---39    ### ๐Ÿ’ก When to Use?40    - Linearly separable data41    - Need probability estimates42    - As a baseline for other classifiers43    """)44 45# ----------- TAB 2: MODEL TRAINING -----------46with tab2:47    st.header("๐ŸŒผ Try Logistic Regression on the Iris Dataset")48 49    # Load dataset50    iris = load_iris()51    df = pd.DataFrame(iris.data, columns=iris.feature_names)52    df["target"] = iris.target53    st.subheader("๐Ÿ“„ Dataset Preview")54    st.dataframe(df.head(), use_container_width=True)55 56    # Model Parameters57    st.subheader("โš™๏ธ Choose Model Parameters")58    col1, col2 = st.columns(2)59    with col1:60        penalty = st.radio("Penalty Type", ["l2", "none"])61    with col2:62        C = st.slider("Inverse Regularization (C)", 0.01, 10.0, value=1.0)63 64    # Preprocessing65    X = df.drop("target", axis=1)66    y = df["target"]67    scaler = StandardScaler()68    X_scaled = scaler.fit_transform(X)69 70    # Train/Test Split71    X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42)72 73    # Model Training74    model = LogisticRegression(penalty=penalty, C=C, solver='lbfgs', multi_class='ovr', max_iter=200)75    model.fit(X_train, y_train)76    y_pred = model.predict(X_test)77    acc = accuracy_score(y_test, y_pred)78 79    # Store for next tab80    st.session_state["model"] = model81    st.session_state["df"] = df82    st.session_state["scaler"] = scaler83    st.session_state["X"] = X84    st.session_state["y"] = y85    st.session_state["y_pred"] = y_pred86    st.session_state["y_test"] = y_test87    st.session_state["target_names"] = iris.target_names88 89    st.success(f"โœ… Logistic Regression Model Accuracy: **{acc*100:.2f}%**")90 91# ----------- TAB 3: RESULTS & VISUALIZATION -----------92with tab3:93    st.header("๐Ÿ“Š Results and Visualization")94 95    if "model" in st.session_state:96        st.subheader("๐Ÿ“‹ Classification Report")97        st.text(classification_report(98            st.session_state["y_test"],99            st.session_state["y_pred"],100            target_names=st.session_state["target_names"]101        ))102 103        st.subheader("๐ŸŒŒ Decision Boundary Visualization")104 105        # Feature selection106        df = st.session_state["df"]107        feature_x = st.selectbox("X-axis Feature", df.columns[:-1], index=0)108        feature_y = st.selectbox("Y-axis Feature", df.columns[:-1], index=1)109 110        # Prepare Data111        X_vis = df[[feature_x, feature_y]]112        X_vis_scaled = st.session_state["scaler"].fit_transform(X_vis)113        X_train_v, X_test_v, y_train_v, y_test_v = train_test_split(X_vis_scaled, st.session_state["y"], test_size=0.2, random_state=42)114 115        model_vis = LogisticRegression(C=C, multi_class='ovr', solver='lbfgs', max_iter=200)116        model_vis.fit(X_train_v, y_train_v)117 118        # Mesh grid119        h = 0.02120        x_min, x_max = X_vis_scaled[:, 0].min() - 1, X_vis_scaled[:, 0].max() + 1121        y_min, y_max = X_vis_scaled[:, 1].min() - 1, X_vis_scaled[:, 1].max() + 1122        xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))123        Z = model_vis.predict(np.c_[xx.ravel(), yy.ravel()])124        Z = Z.reshape(xx.shape)125 126        # Plotting127        fig, ax = plt.subplots(figsize=(5, 3))128        plt.contourf(xx, yy, Z, alpha=0.3, cmap='coolwarm')129        sns.scatterplot(x=X_vis_scaled[:, 0], y=X_vis_scaled[:, 1], hue=df["target"], palette="deep", ax=ax)130        plt.xlabel(feature_x)131        plt.ylabel(feature_y)132        plt.title("Logistic Regression Decision Boundaries")133        st.pyplot(fig)134    else:135        st.warning("โš ๏ธ Please train the model in the 'Train Model' tab first.")136# Footer137st.markdown("---")138 139