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(""" """, unsafe_allow_html=True) # Page title st.markdown("

Support Vector Machines (SVM) in Machine Learning

", 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()