Update pages/6SVM-Algorthim.py
Browse files- pages/6SVM-Algorthim.py +119 -0
pages/6SVM-Algorthim.py
CHANGED
@@ -0,0 +1,119 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
|
3 |
+
# Page configuration
|
4 |
+
st.set_page_config(page_title="Support Vector Machines (SVM)", page_icon="🤖", layout="wide")
|
5 |
+
|
6 |
+
# Custom CSS styling for the app
|
7 |
+
st.markdown("""
|
8 |
+
<style>
|
9 |
+
.stApp {
|
10 |
+
background-color: #E8F0FE; /* A light, modern background */
|
11 |
+
}
|
12 |
+
h1, h2, h3 {
|
13 |
+
color: #002244;
|
14 |
+
}
|
15 |
+
.custom-font, p {
|
16 |
+
font-family: 'Arial', sans-serif;
|
17 |
+
font-size: 18px;
|
18 |
+
color: #000000;
|
19 |
+
line-height: 1.6;
|
20 |
+
}
|
21 |
+
</style>
|
22 |
+
""", unsafe_allow_html=True)
|
23 |
+
|
24 |
+
# Page title
|
25 |
+
st.markdown("<h1 style='color: #002244;'>Support Vector Machines (SVM) in Machine Learning</h1>", unsafe_allow_html=True)
|
26 |
+
|
27 |
+
def main():
|
28 |
+
# Introduction to SVM
|
29 |
+
st.write("""
|
30 |
+
Support Vector Machines (SVM) is a powerful **supervised learning algorithm** employed for both **classification** and **regression** tasks.
|
31 |
+
Although it can perform regression (SVR), SVM is predominantly applied for classification in many real-world scenarios.
|
32 |
+
|
33 |
+
At its core, SVM is a **parametric** and **linear model**, designed to determine the optimal decision boundary (hyperplane) that separates classes by maximizing the margin between them.
|
34 |
+
""")
|
35 |
+
st.image("svm.png", width=700)
|
36 |
+
|
37 |
+
# Types of SVM
|
38 |
+
st.subheader("Types of SVM")
|
39 |
+
st.write("""
|
40 |
+
- **Support Vector Classifier (SVC)**: Used mainly for classification tasks.
|
41 |
+
- **Support Vector Regression (SVR)**: Applied for regression challenges.
|
42 |
+
""")
|
43 |
+
|
44 |
+
# SVC Explanation
|
45 |
+
st.subheader("Working of Support Vector Classifier (SVC)")
|
46 |
+
st.write("""
|
47 |
+
The SVC algorithm works by:
|
48 |
+
1. Randomly initializing the hyperplane (decision boundary).
|
49 |
+
2. Identifying support vectors—data points closest to the decision boundary.
|
50 |
+
3. Calculating the **margin**, which is the distance between these support vectors.
|
51 |
+
4. Optimizing the hyperplane position such that the margin is maximized while minimizing misclassifications.
|
52 |
+
""")
|
53 |
+
|
54 |
+
# Hard Margin vs Soft Margin Explanation
|
55 |
+
st.subheader("Hard Margin vs Soft Margin")
|
56 |
+
st.write("""
|
57 |
+
- **Hard Margin SVC**: Assumes that data is perfectly separable with no errors allowed.
|
58 |
+
- **Soft Margin SVC**: Permits some misclassifications to enhance model generalization on unseen data.
|
59 |
+
""")
|
60 |
+
st.image("soft vs hard.png", width=700)
|
61 |
+
|
62 |
+
# Mathematical Formulation of SVM
|
63 |
+
st.subheader("Mathematical Formulation")
|
64 |
+
|
65 |
+
st.write("**Hard Margin Constraint:** The model enforces that for all data points,")
|
66 |
+
st.latex(r"y_i (w^T x_i + b) \geq 1")
|
67 |
+
|
68 |
+
st.write("**Soft Margin Constraint:** With slack variable \( \xi_i \) to allow misclassification,")
|
69 |
+
st.latex(r"y_i (w^T x_i + b) \geq 1 - \xi_i")
|
70 |
+
|
71 |
+
st.write("**Interpretation of the Slack Variable \( \xi_i \):**")
|
72 |
+
st.latex(r"""
|
73 |
+
\begin{cases}
|
74 |
+
\xi_i = 0 & \text{Correct classification, point lies outside the margin} \\
|
75 |
+
0 < \xi_i \leq 1 & \text{Correct classification, but the point lies within the margin} \\
|
76 |
+
\xi_i > 1 & \text{Misclassification occurs}
|
77 |
+
\end{cases}
|
78 |
+
""")
|
79 |
+
|
80 |
+
# Advantages and Disadvantages of SVM
|
81 |
+
st.subheader("Advantages & Disadvantages")
|
82 |
+
st.write("""
|
83 |
+
**Advantages:**
|
84 |
+
- Effective in high-dimensional spaces.
|
85 |
+
- Versatile as it works with both linearly separable and non-linearly separable datasets (using kernel methods).
|
86 |
+
- Robust against overfitting, especially in scenarios with many features.
|
87 |
+
|
88 |
+
**Disadvantages:**
|
89 |
+
- Can be computationally intensive for large datasets.
|
90 |
+
- Requires careful hyperparameter tuning (e.g., regularization parameter **C** and kernel parameters like **gamma**).
|
91 |
+
""")
|
92 |
+
|
93 |
+
# Dual Form and Kernel Trick Explanation
|
94 |
+
st.subheader("Dual Form and Kernel Trick")
|
95 |
+
st.write("""
|
96 |
+
When the data is not linearly separable, SVM employs the **Kernel Trick** to map data into a higher-dimensional space where a linear separation becomes possible.
|
97 |
+
|
98 |
+
**Common Kernel Functions:**
|
99 |
+
- **Linear Kernel:** For data that is already linearly separable.
|
100 |
+
- **Polynomial Kernel:** For transforming data into a polynomial feature space.
|
101 |
+
- **Radial Basis Function (RBF) Kernel:** Captures complex relationships with non-linear boundaries.
|
102 |
+
- **Sigmoid Kernel:** Behaves similarly to activation functions in neural networks.
|
103 |
+
""")
|
104 |
+
st.image("dualform.png", width=700)
|
105 |
+
|
106 |
+
# Hyperparameter Tuning
|
107 |
+
st.header("Hyperparameter Tuning in SVM")
|
108 |
+
st.write("""
|
109 |
+
**C Parameter (Regularization):**
|
110 |
+
- **High C:** Less tolerance for misclassification resulting in a smaller margin (risk of overfitting).
|
111 |
+
- **Low C:** Higher tolerance for misclassification, which can lead to a larger margin (better generalization).
|
112 |
+
|
113 |
+
**Gamma (for RBF Kernel):**
|
114 |
+
- **High Gamma:** Each training point has more influence, potentially leading to overfitting.
|
115 |
+
- **Low Gamma:** Each training point has less influence, which might result in underfitting.
|
116 |
+
""")
|
117 |
+
|
118 |
+
if __name__ == "__main__":
|
119 |
+
main()
|