pranayreddy316 commited on
Commit
1d0b3ce
·
verified ·
1 Parent(s): 6f2ec69

Create Support vector machine.py

Browse files
Files changed (1) hide show
  1. pages/Support vector machine.py +114 -0
pages/Support vector machine.py ADDED
@@ -0,0 +1,114 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+ st.set_page_config(page_title="SVM Theory App", layout="centered")
4
+ st.title("📘 Support Vector Machine (SVM) - Theoretical Overview")
5
+
6
+ # Section: What is SVM
7
+ st.header("🧠 What is SVM?")
8
+ st.write("""
9
+ Support Vector Machine (SVM) is a supervised learning algorithm used for classification and regression tasks.
10
+ It tries to find the optimal boundary (called a hyperplane) that separates different classes of data with the **maximum margin**.
11
+ """)
12
+
13
+ # Section: Linearly Separable Case
14
+ st.header("📈 Linearly Separable Case")
15
+ st.write("""
16
+ If the data can be separated by a straight line (in 2D) or a hyperplane (in higher dimensions), SVM finds the one that maximizes the margin.
17
+
18
+ **Equation of the hyperplane:**
19
+
20
+ $$
21
+ \\mathbf{w}^T \\mathbf{x} + b = 0
22
+ $$
23
+
24
+ Where:
25
+ - \\( \\mathbf{w} \\): weight vector
26
+ - \\( b \\): bias
27
+ - \\( \\mathbf{x} \\): input feature vector
28
+
29
+ For correct classification:
30
+
31
+ $$
32
+ y_i (\\mathbf{w}^T \\mathbf{x}_i + b) \\geq 1
33
+ $$
34
+ """)
35
+
36
+ # Section: Soft Margin
37
+ st.header("🛑 Soft Margin SVM")
38
+ st.write("""
39
+ When perfect separation isn't possible, SVM introduces a soft margin. It allows some misclassifications using a **regularization parameter C**.
40
+
41
+ - **Small C** → wider margin, more tolerance → better generalization.
42
+ - **Large C** → narrow margin, less tolerance → overfitting risk.
43
+ """)
44
+
45
+ # Section: Kernel Trick
46
+ st.header("🔁 Kernel Trick")
47
+ st.write("""
48
+ SVM can handle **non-linearly separable data** by projecting it into higher-dimensional space using kernels.
49
+
50
+ **Kernel function** computes similarity without explicitly transforming the data.
51
+
52
+ Common kernels:
53
+ - **Linear**: \\( K(x, y) = x^T y \\)
54
+ - **Polynomial**: \\( K(x, y) = (x^T y + c)^d \\)
55
+ - **RBF (Gaussian)**: \\( K(x, y) = e^{-\\gamma \\|x - y\\|^2} \\)
56
+ - **Sigmoid**: \\( K(x, y) = \\tanh(\\alpha x^T y + c) \\)
57
+ """)
58
+
59
+ # Section: Optimization
60
+ with st.expander("📐 Dual Optimization Problem (For Math Curious Folks)"):
61
+ st.latex(r"""
62
+ \max_{\alpha} \sum_i \alpha_i - \frac{1}{2} \sum_i \sum_j \alpha_i \alpha_j y_i y_j K(x_i, x_j)
63
+ """)
64
+ st.markdown("""
65
+ Subject to:
66
+ - \\( \\sum_i \\alpha_i y_i = 0 \\)
67
+ - \\( 0 \\leq \\alpha_i \\leq C \\)
68
+
69
+ Here, \\( \\alpha_i \\) are Lagrange multipliers. The optimization is solved using Quadratic Programming.
70
+ """)
71
+
72
+ # Section: Support Vectors
73
+ st.header("🧷 Support Vectors")
74
+ st.write("""
75
+ Support vectors are the data points that lie closest to the decision boundary. These points define the hyperplane.
76
+
77
+ Only the support vectors influence the final decision boundary. Other points can be removed without changing it.
78
+ """)
79
+
80
+ # Section: SVM vs Logistic Regression
81
+ st.header("🆚 SVM vs Logistic Regression")
82
+ st.table({
83
+ "Aspect": ["Objective", "Handles Non-Linearity", "Probabilities", "Works with Kernels"],
84
+ "SVM": ["Maximize margin", "✅ Yes", "❌ No (but can be calibrated)", "✅ Yes"],
85
+ "Logistic Regression": ["Maximize likelihood", "❌ No", "✅ Yes", "❌ No"]
86
+ })
87
+
88
+ # Section: Pros and Cons
89
+ st.header("✅ Pros and ❌ Cons")
90
+ st.markdown("""
91
+ **Pros:**
92
+ - Great for high-dimensional spaces
93
+ - Effective for non-linear problems with the right kernel
94
+ - Robust to overfitting
95
+
96
+ **Cons:**
97
+ - Slow for large datasets
98
+ - Sensitive to kernel and parameter choices
99
+ - Not very interpretable
100
+ """)
101
+
102
+ # Section: Applications
103
+ st.header("💡 Real-World Applications")
104
+ st.write("""
105
+ - Email spam detection
106
+ - Handwriting recognition (e.g. digits)
107
+ - Image and face classification
108
+ - Cancer diagnosis
109
+ - Intrusion detection
110
+ """)
111
+
112
+ st.markdown("---")
113
+ st.success("Want to see this in action? You can add a classification demo with SVM below!")
114
+