Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,12 +4,10 @@ import numpy as np
|
|
| 4 |
from sklearn.datasets import load_iris
|
| 5 |
from sklearn.model_selection import train_test_split
|
| 6 |
from sklearn.svm import SVC
|
| 7 |
-
from sklearn.linear_model import LogisticRegression
|
| 8 |
from sklearn.preprocessing import StandardScaler
|
| 9 |
from sklearn.metrics import classification_report, accuracy_score
|
| 10 |
import matplotlib.pyplot as plt
|
| 11 |
import seaborn as sns
|
| 12 |
-
from io import StringIO
|
| 13 |
|
| 14 |
# Page config
|
| 15 |
st.set_page_config(page_title="Explore SVM Algorithm", layout="wide")
|
|
@@ -20,7 +18,6 @@ st.title("π Support Vector Machine (SVM)")
|
|
| 20 |
# -----------------------------------
|
| 21 |
st.markdown("""
|
| 22 |
## π€ What is a Support Vector Machine (SVM)?
|
| 23 |
-
|
| 24 |
SVM is a powerful supervised learning algorithm used for classification and regression.
|
| 25 |
It works by finding a hyperplane that best separates the classes in the feature space.
|
| 26 |
|
|
@@ -30,8 +27,8 @@ It works by finding a hyperplane that best separates the classes in the feature
|
|
| 30 |
- Can use **kernel tricks** to handle non-linear classification
|
| 31 |
|
| 32 |
---
|
| 33 |
-
## βοΈ How SVM Works
|
| 34 |
|
|
|
|
| 35 |
1. Find the optimal hyperplane that separates classes.
|
| 36 |
2. Use **support vectors** β data points closest to the hyperplane.
|
| 37 |
3. Maximize the margin between these support vectors.
|
|
@@ -41,6 +38,7 @@ It works by finding a hyperplane that best separates the classes in the feature
|
|
| 41 |
- *Linear*: Straight line separation
|
| 42 |
- *RBF (Gaussian)*: Circular, good for complex boundaries
|
| 43 |
- *Polynomial*: Curved boundaries
|
|
|
|
| 44 |
---
|
| 45 |
""")
|
| 46 |
|
|
@@ -84,23 +82,6 @@ svm_report = classification_report(y_test, svm_pred, target_names=iris.target_na
|
|
| 84 |
st.markdown("### π SVM Classification Report")
|
| 85 |
st.text(svm_report)
|
| 86 |
|
| 87 |
-
# -----------------------------------
|
| 88 |
-
# Compare with Logistic Regression
|
| 89 |
-
# -----------------------------------
|
| 90 |
-
st.markdown("### π Compare with Logistic Regression")
|
| 91 |
-
|
| 92 |
-
log_model = LogisticRegression(max_iter=200)
|
| 93 |
-
log_model.fit(X_train, y_train)
|
| 94 |
-
log_pred = log_model.predict(X_test)
|
| 95 |
-
log_acc = accuracy_score(y_test, log_pred)
|
| 96 |
-
|
| 97 |
-
st.info(f"π Logistic Regression Accuracy: {log_acc*100:.2f}%")
|
| 98 |
-
|
| 99 |
-
if log_acc > svm_acc:
|
| 100 |
-
st.warning("π€ Logistic Regression outperformed SVM! Try tuning SVM parameters or switching kernels.")
|
| 101 |
-
else:
|
| 102 |
-
st.success("β
SVM performed better than Logistic Regression on this dataset.")
|
| 103 |
-
|
| 104 |
# -----------------------------------
|
| 105 |
# Visualize Decision Boundaries
|
| 106 |
# -----------------------------------
|
|
@@ -154,14 +135,13 @@ Use them when:
|
|
| 154 |
- You want flexibility via kernels.
|
| 155 |
|
| 156 |
---
|
| 157 |
-
### π§ Did You Know?
|
| 158 |
|
|
|
|
| 159 |
- SVMs are **robust to overfitting**, especially in high-dimensional space.
|
| 160 |
- The **'C' parameter** controls the trade-off between training error and margin size.
|
| 161 |
- The **kernel trick** allows SVMs to operate in infinite-dimensional space.
|
| 162 |
|
| 163 |
### π Pros & Cons
|
| 164 |
-
|
| 165 |
| Pros | Cons |
|
| 166 |
|---------------------------------|-------------------------------------|
|
| 167 |
| Works well on complex boundaries| Slower on large datasets |
|
|
@@ -169,8 +149,8 @@ Use them when:
|
|
| 169 |
| Can handle non-linear data | Less interpretable than simpler models |
|
| 170 |
|
| 171 |
---
|
| 172 |
-
### π Kernel Choice Summary
|
| 173 |
|
|
|
|
| 174 |
| Kernel | Use Case |
|
| 175 |
|-------------|---------------------------------|
|
| 176 |
| Linear | Simple, linearly separable data |
|
|
@@ -181,3 +161,4 @@ Use them when:
|
|
| 181 |
""")
|
| 182 |
|
| 183 |
|
|
|
|
|
|
| 4 |
from sklearn.datasets import load_iris
|
| 5 |
from sklearn.model_selection import train_test_split
|
| 6 |
from sklearn.svm import SVC
|
|
|
|
| 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 |
# Page config
|
| 13 |
st.set_page_config(page_title="Explore SVM Algorithm", layout="wide")
|
|
|
|
| 18 |
# -----------------------------------
|
| 19 |
st.markdown("""
|
| 20 |
## π€ What is a Support Vector Machine (SVM)?
|
|
|
|
| 21 |
SVM is a powerful supervised learning algorithm used for classification and regression.
|
| 22 |
It works by finding a hyperplane that best separates the classes in the feature space.
|
| 23 |
|
|
|
|
| 27 |
- Can use **kernel tricks** to handle non-linear classification
|
| 28 |
|
| 29 |
---
|
|
|
|
| 30 |
|
| 31 |
+
## βοΈ How SVM Works
|
| 32 |
1. Find the optimal hyperplane that separates classes.
|
| 33 |
2. Use **support vectors** β data points closest to the hyperplane.
|
| 34 |
3. Maximize the margin between these support vectors.
|
|
|
|
| 38 |
- *Linear*: Straight line separation
|
| 39 |
- *RBF (Gaussian)*: Circular, good for complex boundaries
|
| 40 |
- *Polynomial*: Curved boundaries
|
| 41 |
+
|
| 42 |
---
|
| 43 |
""")
|
| 44 |
|
|
|
|
| 82 |
st.markdown("### π SVM Classification Report")
|
| 83 |
st.text(svm_report)
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
# -----------------------------------
|
| 86 |
# Visualize Decision Boundaries
|
| 87 |
# -----------------------------------
|
|
|
|
| 135 |
- You want flexibility via kernels.
|
| 136 |
|
| 137 |
---
|
|
|
|
| 138 |
|
| 139 |
+
### π§ Did You Know?
|
| 140 |
- SVMs are **robust to overfitting**, especially in high-dimensional space.
|
| 141 |
- The **'C' parameter** controls the trade-off between training error and margin size.
|
| 142 |
- The **kernel trick** allows SVMs to operate in infinite-dimensional space.
|
| 143 |
|
| 144 |
### π Pros & Cons
|
|
|
|
| 145 |
| Pros | Cons |
|
| 146 |
|---------------------------------|-------------------------------------|
|
| 147 |
| Works well on complex boundaries| Slower on large datasets |
|
|
|
|
| 149 |
| Can handle non-linear data | Less interpretable than simpler models |
|
| 150 |
|
| 151 |
---
|
|
|
|
| 152 |
|
| 153 |
+
### π Kernel Choice Summary
|
| 154 |
| Kernel | Use Case |
|
| 155 |
|-------------|---------------------------------|
|
| 156 |
| Linear | Simple, linearly separable data |
|
|
|
|
| 161 |
""")
|
| 162 |
|
| 163 |
|
| 164 |
+
|