Spaces:
Sleeping
Sleeping
Update pages/14_SVM.py
Browse files- pages/14_SVM.py +114 -0
pages/14_SVM.py
CHANGED
|
@@ -0,0 +1,114 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
|
| 3 |
+
st.set_page_config(page_title="Support Vector Machines", page_icon="π§", layout="wide")
|
| 4 |
+
|
| 5 |
+
st.sidebar.title("π Support Vector Machines")
|
| 6 |
+
st.sidebar.markdown("Learn how SVM works for classification and regression tasks.")
|
| 7 |
+
st.sidebar.markdown("---")
|
| 8 |
+
|
| 9 |
+
section = st.radio(
|
| 10 |
+
"π Select a section to explore:",
|
| 11 |
+
[
|
| 12 |
+
"π What is SVM?",
|
| 13 |
+
"π§ Types of SVM",
|
| 14 |
+
"π οΈ Working of SVC",
|
| 15 |
+
"π Hard Margin vs Soft Margin",
|
| 16 |
+
"π Mathematical Formulation",
|
| 17 |
+
"β
Pros & Cons of SVM",
|
| 18 |
+
"π Dual Form & Kernel Trick",
|
| 19 |
+
"βοΈ Hyperparameter Tuning"
|
| 20 |
+
]
|
| 21 |
+
)
|
| 22 |
+
|
| 23 |
+
st.markdown("<h1 style='text-align: center;'>π€ Support Vector Machines (SVM)</h1>", unsafe_allow_html=True)
|
| 24 |
+
|
| 25 |
+
if section == "π What is SVM?":
|
| 26 |
+
st.write("""
|
| 27 |
+
Support Vector Machines (SVM) is a **supervised learning algorithm** used for both **classification** and **regression** problems.
|
| 28 |
+
In practice, it's most often used for **classification** tasks.
|
| 29 |
+
|
| 30 |
+
π§ SVM finds the **optimal decision boundary (hyperplane)** that maximizes the **margin** between classes.
|
| 31 |
+
""")
|
| 32 |
+
|
| 33 |
+
elif section == "π§ Types of SVM":
|
| 34 |
+
st.write("""
|
| 35 |
+
1. **Support Vector Classifier (SVC)**: Used for classification
|
| 36 |
+
2. **Support Vector Regression (SVR)**: Used for predicting continuous values
|
| 37 |
+
""")
|
| 38 |
+
|
| 39 |
+
elif section == "π οΈ Working of SVC":
|
| 40 |
+
st.write("""
|
| 41 |
+
Steps:
|
| 42 |
+
1. Start with a random separating hyperplane.
|
| 43 |
+
2. Identify **support vectors** (closest points from each class).
|
| 44 |
+
3. Adjust the hyperplane to **maximize the margin** between the support vectors.
|
| 45 |
+
|
| 46 |
+
π Goal: Maximize the distance between the hyperplane and the nearest data points.
|
| 47 |
+
""")
|
| 48 |
+
|
| 49 |
+
elif section == "π Hard Margin vs Soft Margin":
|
| 50 |
+
st.write("""
|
| 51 |
+
- **Hard Margin**:
|
| 52 |
+
- Assumes **perfectly separable** data.
|
| 53 |
+
- No misclassifications allowed.
|
| 54 |
+
- **Soft Margin**:
|
| 55 |
+
- Allows **some misclassification**.
|
| 56 |
+
- More flexible, better for real-world noisy data.
|
| 57 |
+
""")
|
| 58 |
+
|
| 59 |
+
elif section == "π Mathematical Formulation":
|
| 60 |
+
st.markdown("### Hard Margin Condition:")
|
| 61 |
+
st.latex(r"y_i (w^T x_i + b) \geq 1")
|
| 62 |
+
|
| 63 |
+
st.markdown("### Soft Margin Condition:")
|
| 64 |
+
st.latex(r"y_i (w^T x_i + b) \geq 1 - \xi_i")
|
| 65 |
+
|
| 66 |
+
st.markdown("### Slack Variable \( \xi_i \) Interpretation:")
|
| 67 |
+
st.write("""
|
| 68 |
+
- \( \xi_i = 0 \): Correct and outside the margin
|
| 69 |
+
- \( 0 < \xi_i \leq 1 \): Inside the margin, but correctly classified
|
| 70 |
+
- \( \xi_i > 1 \): Misclassified
|
| 71 |
+
""")
|
| 72 |
+
|
| 73 |
+
elif section == "β
Pros & Cons of SVM":
|
| 74 |
+
st.markdown("### Advantages:")
|
| 75 |
+
st.write("""
|
| 76 |
+
- Works well in **high-dimensional** spaces
|
| 77 |
+
- Effective with both linear and **non-linear** data (using kernels)
|
| 78 |
+
- Resistant to **overfitting**
|
| 79 |
+
""")
|
| 80 |
+
|
| 81 |
+
st.markdown("### Disadvantages:")
|
| 82 |
+
st.write("""
|
| 83 |
+
- Computationally **slow** for large datasets
|
| 84 |
+
- Requires tuning of hyperparameters (`C`, `gamma`)
|
| 85 |
+
""")
|
| 86 |
+
|
| 87 |
+
elif section == "π Dual Form & Kernel Trick":
|
| 88 |
+
st.markdown("""
|
| 89 |
+
When data is not linearly separable in its original space, we use the **kernel trick** to transform it.
|
| 90 |
+
|
| 91 |
+
### Common Kernels:
|
| 92 |
+
- **Linear Kernel**: \( K(x, x') = x^T x' \)
|
| 93 |
+
- **Polynomial Kernel**: \( K(x, x') = (x^T x' + c)^d \)
|
| 94 |
+
- **RBF (Gaussian)**: \( K(x, x') = \exp(-\gamma \|x - x'\|^2) \)
|
| 95 |
+
- **Sigmoid Kernel**: Mimics activation of neural networks
|
| 96 |
+
|
| 97 |
+
β
The kernel trick allows working in higher dimensions **without explicitly transforming** the data.
|
| 98 |
+
""")
|
| 99 |
+
|
| 100 |
+
elif section == "βοΈ Hyperparameter Tuning":
|
| 101 |
+
st.write("""
|
| 102 |
+
- **C (Regularization)**:
|
| 103 |
+
- Controls the trade-off between maximizing margin and minimizing misclassification.
|
| 104 |
+
- High C β strict on misclassification (may overfit)
|
| 105 |
+
- Low C β allows more slack (better generalization)
|
| 106 |
+
|
| 107 |
+
- **Gamma** (only for RBF/Polynomial Kernels):
|
| 108 |
+
- Defines how far the influence of a single data point reaches.
|
| 109 |
+
- High Gamma β close points matter more β can overfit
|
| 110 |
+
- Low Gamma β wider influence β can underfit
|
| 111 |
+
""")
|
| 112 |
+
|
| 113 |
+
st.markdown("---")
|
| 114 |
+
st.success("SVMs are powerful and flexible. Mastering margins, kernels, and regularization is key to using them effectively!")
|