Spaces:
Sleeping
Sleeping
| import streamlit as st | |
| st.set_page_config(page_title="Support Vector Machine", page_icon="π§ ", layout="wide") | |
| # Title | |
| st.markdown("<h1 style='color:#4CAF50;'>π§ Support Vector Machine (SVM)</h1>", unsafe_allow_html=True) | |
| # Introduction | |
| st.markdown("### π What is SVM?") | |
| st.markdown(""" | |
| Support Vector Machine (SVM) is a powerful **supervised learning algorithm** used for both **classification** and **regression**, though it is mostly used for classification tasks. | |
| The core idea is to find the **optimal hyperplane** that best separates the data points of different classes by maximizing the **margin** between them. | |
| """) | |
| # Use Cases | |
| st.markdown("### π― Where is SVM Used?") | |
| st.markdown(""" | |
| - Face Recognition | |
| - Handwriting Recognition | |
| - Bioinformatics (e.g., gene classification) | |
| - Email Spam Detection | |
| - Image Classification | |
| """) | |
| # How It Works | |
| st.markdown("### βοΈ How Does SVM Work?") | |
| with st.expander("πΉ Step 1: Find a Hyperplane"): | |
| st.markdown(""" | |
| A **hyperplane** is a decision boundary that separates the data points of different classes. | |
| SVM tries to find the hyperplane that **maximizes the margin** between classes. | |
| """) | |
| with st.expander("πΉ Step 2: Identify Support Vectors"): | |
| st.markdown(""" | |
| **Support vectors** are the data points that lie closest to the hyperplane. | |
| These points are critical in defining the position and orientation of the hyperplane. | |
| """) | |
| with st.expander("πΉ Step 3: Handle Non-Linearly Separable Data"): | |
| st.markdown(""" | |
| When the data is not linearly separable, SVM uses the **kernel trick** to project it into a higher-dimensional space where it becomes separable. | |
| """) | |
| # Kernel Functions | |
| st.markdown("### π§ͺ Kernels in SVM") | |
| with st.expander("π Common Kernel Functions"): | |
| st.markdown(""" | |
| - **Linear Kernel**: For linearly separable data | |
| - **Polynomial Kernel**: For curved decision boundaries | |
| - **RBF (Radial Basis Function)**: Most popular, handles complex data | |
| - **Sigmoid Kernel**: Similar to neural networks | |
| """) | |
| # Mathematical Intuition | |
| st.markdown("### π§ Mathematical Formulation") | |
| with st.expander("π Decision Function"): | |
| st.latex(r"f(x) = w \cdot x + b") | |
| with st.expander("π Classification Rule"): | |
| st.markdown(""" | |
| - If \\( f(x) > 0 \\): Predict **Class 1** | |
| - If \\( f(x) < 0 \\): Predict **Class 0** | |
| """) | |
| with st.expander("π Optimization Objective"): | |
| st.latex(r"\text{Maximize Margin} = \frac{2}{\|w\|}") | |
| st.markdown("We want to maximize the margin between support vectors and the hyperplane.") | |
| with st.expander("π Soft Margin & C Parameter"): | |
| st.latex(r" \min \frac{1}{2} \|w\|^2 + C \sum \xi_i ") | |
| st.markdown(""" | |
| - The **C parameter** balances margin maximization vs classification error. | |
| - A **small C** allows for a wider margin but more errors. | |
| - A **large C** aims for perfect classification but might overfit. | |
| """) | |
| # Evaluation Metrics | |
| st.markdown("### π Evaluation Metrics") | |
| st.markdown("#### β Accuracy") | |
| st.latex(r"Accuracy = \frac{TP + TN}{TP + TN + FP + FN}") | |
| st.markdown("The percentage of correct predictions.") | |
| st.markdown("#### π― Precision") | |
| st.latex(r"Precision = \frac{TP}{TP + FP}") | |
| st.markdown("Out of all predicted positives, how many are actually positive?") | |
| st.markdown("#### π£ Recall (Sensitivity)") | |
| st.latex(r"Recall = \frac{TP}{TP + FN}") | |
| st.markdown("Out of all actual positives, how many did we correctly predict?") | |
| st.markdown("#### βοΈ F1 Score") | |
| st.latex(r"F1 = 2 \cdot \frac{Precision \cdot Recall}{Precision + Recall}") | |
| st.markdown("Balances precision and recall β especially useful in imbalanced datasets.") | |
| st.markdown("#### π ROC-AUC") | |
| st.markdown(""" | |
| - Plots True Positive Rate (TPR) vs False Positive Rate (FPR). | |
| - **AUC (Area Under Curve)** closer to 1 indicates a better model. | |
| """) | |
| # Pros and Cons | |
| st.markdown("### β Advantages of SVM") | |
| st.markdown(""" | |
| - Effective in high-dimensional spaces | |
| - Works well even when features > samples | |
| - Memory efficient (uses support vectors) | |
| - Handles non-linearity with kernels | |
| """) | |
| st.markdown("### β Limitations of SVM") | |
| st.markdown(""" | |
| - Not ideal for large datasets (computationally expensive) | |
| - Requires careful parameter tuning (C, kernel) | |
| - Hard to interpret compared to decision trees | |
| """) | |
| # Summary | |
| st.markdown("### π Summary") | |
| st.markdown(""" | |
| Support Vector Machine is a **robust**, **flexible**, and **accurate** classification algorithm. | |
| Great for: | |
| - Text data | |
| - Image recognition | |
| - Biomedical data | |
| Make sure to: | |
| - Scale your features | |
| - Use kernel wisely | |
| - Tune the **C** and **gamma** parameters | |
| β Powerful for **both linear and non-linear** decision boundaries! | |
| """) | |