File size: 5,088 Bytes
f154da5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
25c5609
f154da5
 
25c5609
f154da5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import streamlit as st

# Page configuration
st.set_page_config(page_title="Support Vector Machines (SVM)", page_icon="🤖", layout="wide")

# Custom CSS styling for the app
st.markdown("""
    <style>
        .stApp {
            background-color: #E8F0FE; /* A light, modern background */
        }
        h1, h2, h3 {
            color: #002244;
        }
        .custom-font, p {
            font-family: 'Arial', sans-serif;
            font-size: 18px;
            color: #000000;
            line-height: 1.6;
        }
    </style>
    """, unsafe_allow_html=True)

# Page title
st.markdown("<h1 style='color: #002244;'>Support Vector Machines (SVM) in Machine Learning</h1>", unsafe_allow_html=True)

def main():
    # Introduction to SVM
    st.write("""
    Support Vector Machines (SVM) is a powerful **supervised learning algorithm** employed for both **classification** and **regression** tasks.
    Although it can perform regression (SVR), SVM is predominantly applied for classification in many real-world scenarios.
    
    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.
    """)
    st.image("svm.png", width=700)
    
    # Types of SVM
    st.subheader("Types of SVM")
    st.write("""
    - **Support Vector Classifier (SVC)**: Used mainly for classification tasks.
    - **Support Vector Regression (SVR)**: Applied for regression challenges.
    """)
    
    # SVC Explanation
    st.subheader("Working of Support Vector Classifier (SVC)")
    st.write("""
    The SVC algorithm works by:
    1. Randomly initializing the hyperplane (decision boundary).
    2. Identifying support vectors—data points closest to the decision boundary.
    3. Calculating the **margin**, which is the distance between these support vectors.
    4. Optimizing the hyperplane position such that the margin is maximized while minimizing misclassifications.
    """)
    
    # Hard Margin vs Soft Margin Explanation
    st.subheader("Hard Margin vs Soft Margin")
    st.write("""
    - **Hard Margin SVC**: Assumes that data is perfectly separable with no errors allowed.
    - **Soft Margin SVC**: Permits some misclassifications to enhance model generalization on unseen data.
    """)
    st.image("soft vs hard.png", width=700)
    
    # Mathematical Formulation of SVM
    st.subheader("Mathematical Formulation")
    
    st.write("**Hard Margin Constraint:** The model enforces that for all data points,")
    st.latex(r"y_i (w^T x_i + b) \geq 1")
    
    st.write("**Soft Margin Constraint:** With slack variable \\( \\xi_i \\) to allow misclassification,")
    st.latex(r"y_i (w^T x_i + b) \geq 1 - \xi_i")
    
    st.write("**Interpretation of the Slack Variable \\( \\xi_i \\):**")
    st.latex(r"""
        \begin{cases}
        \xi_i = 0 & \text{Correct classification, point lies outside the margin} \\
        0 < \xi_i \leq 1 & \text{Correct classification, but the point lies within the margin} \\
        \xi_i > 1 & \text{Misclassification occurs}
        \end{cases}
    """)
    
    # Advantages and Disadvantages of SVM
    st.subheader("Advantages & Disadvantages")
    st.write("""
    **Advantages:**
    - Effective in high-dimensional spaces.
    - Versatile as it works with both linearly separable and non-linearly separable datasets (using kernel methods).
    - Robust against overfitting, especially in scenarios with many features.
    
    **Disadvantages:**
    - Can be computationally intensive for large datasets.
    - Requires careful hyperparameter tuning (e.g., regularization parameter **C** and kernel parameters like **gamma**).
    """)
    
    # Dual Form and Kernel Trick Explanation
    st.subheader("Dual Form and Kernel Trick")
    st.write("""
    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.
    
    **Common Kernel Functions:**
    - **Linear Kernel:** For data that is already linearly separable.
    - **Polynomial Kernel:** For transforming data into a polynomial feature space.
    - **Radial Basis Function (RBF) Kernel:** Captures complex relationships with non-linear boundaries.
    - **Sigmoid Kernel:** Behaves similarly to activation functions in neural networks.
    """)
    st.image("dualform.png", width=700)
    
    # Hyperparameter Tuning
    st.header("Hyperparameter Tuning in SVM")
    st.write("""
    **C Parameter (Regularization):**
    - **High C:** Less tolerance for misclassification resulting in a smaller margin (risk of overfitting).
    - **Low C:** Higher tolerance for misclassification, which can lead to a larger margin (better generalization).
    
    **Gamma (for RBF Kernel):**
    - **High Gamma:** Each training point has more influence, potentially leading to overfitting.
    - **Low Gamma:** Each training point has less influence, which might result in underfitting.
    """)

if __name__ == "__main__":
    main()