UmaKumpatla commited on
Commit
bb5ca83
Β·
verified Β·
1 Parent(s): 1177367

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +109 -0
app.py CHANGED
@@ -0,0 +1,109 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import pandas as pd
3
+ import numpy as np
4
+ from sklearn.datasets import load_wine
5
+ from sklearn.model_selection import train_test_split
6
+ from sklearn.linear_model import LogisticRegression
7
+ from sklearn.preprocessing import StandardScaler
8
+ from sklearn.metrics import classification_report, accuracy_score
9
+ import matplotlib.pyplot as plt
10
+ import seaborn as sns
11
+
12
+ # Streamlit Page Configuration
13
+ st.set_page_config(page_title="Explore Logistic Regression", layout="wide")
14
+ st.title("🍷 Logistic Regression Classifier on Wine Dataset")
15
+
16
+ # Introduction
17
+ st.markdown("""
18
+ ## 🧠 What is Logistic Regression?
19
+
20
+ Logistic Regression is a widely used classification technique that models the probability of class membership.
21
+ It’s particularly useful when the output is categorical (e.g., types of wines πŸ‡).
22
+
23
+ ---
24
+
25
+ ## πŸ“¦ Dataset: Wine Classification
26
+
27
+ We'll be using the Wine dataset, which contains chemical analysis of wines grown in the same region in Italy, but derived from three different cultivars.
28
+
29
+ """)
30
+
31
+ # Load and preview Wine dataset
32
+ wine = load_wine()
33
+ df = pd.DataFrame(wine.data, columns=wine.feature_names)
34
+ df['target'] = wine.target
35
+
36
+ st.markdown("### πŸ“‹ Data Preview")
37
+ st.dataframe(df.head(), use_container_width=True)
38
+
39
+ # User Input: Regularization settings
40
+ st.sidebar.header("βš™οΈ Model Settings")
41
+ penalty = st.sidebar.radio("Penalty Type (Regularization)", ["l2", "none"])
42
+ C = st.sidebar.slider("Inverse Regularization Strength (C)", 0.01, 10.0, value=1.0)
43
+
44
+ # Prepare features and target
45
+ X = df.drop("target", axis=1)
46
+ y = df["target"]
47
+
48
+ # Feature scaling
49
+ scaler = StandardScaler()
50
+ X_scaled = scaler.fit_transform(X)
51
+
52
+ # Train-test split
53
+ X_train, X_test, y_train, y_test = train_test_split(X_scaled, y, test_size=0.2, random_state=42)
54
+
55
+ # Train the Logistic Regression model
56
+ model = LogisticRegression(penalty=penalty, C=C, multi_class='ovr', solver='lbfgs', max_iter=200)
57
+ model.fit(X_train, y_train)
58
+ y_pred = model.predict(X_test)
59
+
60
+ # Accuracy and classification report
61
+ accuracy = accuracy_score(y_test, y_pred)
62
+ st.success(f"βœ… Model Accuracy: {accuracy * 100:.2f}%")
63
+
64
+ st.markdown("### πŸ“Š Classification Report")
65
+ st.text(classification_report(y_test, y_pred, target_names=wine.target_names))
66
+
67
+ # Visualization Section
68
+ st.markdown("## 🎨 Visualizing the Decision Boundary (2 Features Only)")
69
+
70
+ feature_x = st.selectbox("Select X-axis Feature", df.columns[:-1], index=0)
71
+ feature_y = st.selectbox("Select Y-axis Feature", df.columns[:-1], index=1)
72
+
73
+ X_vis = df[[feature_x, feature_y]]
74
+ X_vis_scaled = scaler.fit_transform(X_vis)
75
+
76
+ X_train_v, X_test_v, y_train_v, y_test_v = train_test_split(X_vis_scaled, y, test_size=0.2, random_state=42)
77
+
78
+ model_vis = LogisticRegression(C=C, multi_class='ovr', solver='lbfgs', max_iter=200)
79
+ model_vis.fit(X_train_v, y_train_v)
80
+
81
+ # Meshgrid for decision boundary
82
+ h = .02
83
+ x_min, x_max = X_vis_scaled[:, 0].min() - 1, X_vis_scaled[:, 0].max() + 1
84
+ y_min, y_max = X_vis_scaled[:, 1].min() - 1, X_vis_scaled[:, 1].max() + 1
85
+ xx, yy = np.meshgrid(np.arange(x_min, x_max, h), np.arange(y_min, y_max, h))
86
+ Z = model_vis.predict(np.c_[xx.ravel(), yy.ravel()])
87
+ Z = Z.reshape(xx.shape)
88
+
89
+ fig, ax = plt.subplots(figsize=(8, 6))
90
+ plt.contourf(xx, yy, Z, alpha=0.3)
91
+ sns.scatterplot(x=X_vis_scaled[:, 0], y=X_vis_scaled[:, 1], hue=df['target'], palette='Set1', ax=ax)
92
+ plt.xlabel(feature_x)
93
+ plt.ylabel(feature_y)
94
+ plt.title("Decision Boundaries using Logistic Regression")
95
+ st.pyplot(fig)
96
+
97
+ # Closing Notes
98
+ st.markdown("""
99
+ ---
100
+
101
+ ## βœ… Summary
102
+
103
+ - **Logistic Regression** is great for interpretable, fast classification.
104
+ - It works well when your features are **linearly separable**.
105
+ - The **Wine dataset** is a good example of multiclass classification.
106
+ - Always **scale features** and **tune regularization** for best results.
107
+
108
+ 🎯 *Pro Tip:* Explore different feature combinations in the plot above to see how separation varies!
109
+ """)