Download pages/SVM_Algorithm.py from Phaneendrabayi/Zero_To_Hero_Machine_Learning: direct link, hf CLI and curl.
- Browser
- Download file 3.34 kB
-
https://huggingface.co/spaces/Phaneendrabayi/Zero_To_Hero_Machine_Learning/resolve/main/pages/SVM_Algorithm.py
- Command line
-
hf download hf://spaces/Phaneendrabayi/Zero_To_Hero_Machine_Learning/pages/SVM_Algorithm.py
-
curl -L -o SVM_Algorithm.py https://huggingface.co/spaces/Phaneendrabayi/Zero_To_Hero_Machine_Learning/resolve/main/pages/SVM_Algorithm.py
3.34 kB
| import streamlit as st | |
| import pandas as pd | |
| st.set_page_config(page_title="ML Model Explorer") | |
| st.title("π€ Support Vector Machine (SVM) - In Depth") | |
| st.markdown(""" | |
| Support Vector Machine (SVM) is a **supervised learning algorithm** used for both **classification** and **regression** tasks. It finds the optimal hyperplane that best separates data points of different classes (in classification) or fits the best margin for predicting continuous values (in regression). | |
| --- | |
| ## π Key Concepts | |
| ### 1. **Hyperplane** | |
| A decision boundary that separates different classes. In higher dimensions, it's a plane or manifold. | |
| ### 2. **Support Vectors** | |
| The data points that are closest to the hyperplane and influence its position. | |
| ### 3. **Margin** | |
| The distance between the hyperplane and the nearest data points (support vectors). SVM maximizes this margin. | |
| --- | |
| ## π Mathematical Formulation | |
| ### Classification Objective: | |
| For a binary classification task with input vector `x`, label `y β {-1, 1}`: | |
| SVM aims to solve: | |
| \[ \min_{w, b} \ \frac{1}{2} \|w\|^2 \quad \text{subject to} \quad y_i(w^T x_i + b) \geq 1 \] | |
| ### Regression (SVR) Objective: | |
| Support Vector Regression (SVR) aims to find a function that deviates from the actual values by a margin \( \varepsilon \): | |
| \[ \min_{w, b} \ \frac{1}{2} \|w\|^2 \quad \text{subject to} \quad |y_i - (w^T x_i + b)| \leq \varepsilon \] | |
| --- | |
| ## βοΈ SVM Kernels | |
| Kernels transform data into higher dimensions to make it linearly separable: | |
| - **Linear Kernel**: \( K(x, x') = x^T x' \) | |
| - **Polynomial Kernel**: \( K(x, x') = (\gamma x^T x' + r)^d \) | |
| - **RBF (Gaussian) Kernel**: \( K(x, x') = \exp(-\gamma \|x - x'\|^2) \) | |
| - **Sigmoid Kernel**: \( K(x, x') = \tanh(\gamma x^T x' + r) \) | |
| --- | |
| ## π Hyperparameters of SVM (`sklearn.svm.SVC` and `SVR`) | |
| | Parameter | Description | | |
| |----------------|-------------| | |
| | `C` | Regularization parameter. High value = low bias, high variance | | |
| | `kernel` | Type of kernel function (linear, poly, rbf, sigmoid) | | |
| | `degree` | Degree for polynomial kernel | | |
| | `gamma` | Kernel coefficient for 'rbf', 'poly', and 'sigmoid' | | |
| | `coef0` | Independent term in kernel function (used with 'poly' and 'sigmoid') | | |
| | `epsilon` | (SVR only) Epsilon-tube within which no penalty is associated | | |
| | `shrinking` | Whether to use shrinking heuristic | | |
| | `probability` | Enable probability estimates (slower) | | |
| --- | |
| ### π§ͺ Hyperparameter Tuning with Optuna (Conceptual) | |
| Optuna can help optimize: | |
| - `C` and `gamma` values | |
| - Kernel type | |
| - `degree` and `coef0` for poly/sigmoid kernels | |
| - Cross-validation scores (accuracy, RMSE) | |
| Optuna uses **Bayesian Optimization** to efficiently explore the parameter space. | |
| --- | |
| ### β Advantages | |
| - Effective in high-dimensional spaces | |
| - Memory efficient (uses support vectors only) | |
| - Works with nonlinear data using kernels | |
| ### π« Limitations | |
| - Not suitable for very large datasets | |
| - Choosing the right kernel and parameters can be tricky | |
| - Less interpretable compared to tree-based models | |
| --- | |
| ### π‘ Real-World Applications | |
| - Email spam detection | |
| - Image classification | |
| - Stock price prediction (SVR) | |
| - Face and handwriting recognition | |
| π **Tip**: Always scale features (e.g., StandardScaler) before using SVM for optimal performance. | |
| """) | |