Understanding SVM

本文探讨了支持向量机(SVM)在处理线性可分数据时的原理和方法,包括如何找到最优决策边界,以及如何通过映射将非线性可分数据转换为线性可分。

1,Linearly Separable Data 线性数据切分


考虑下面这张图,有红色数据和蓝色数据。在KNN 中,作为测试数据,要测量所有的训练数据的距离并取最小距离值。这要消耗大量的时间来计算距离和大量的内存来保存训练数据。考虑这些数据都是图像提供的,我们是否正的需要那么多?



有另一种想法,找到一条线,f(x)= ax+by+c,把所有的数据分成两部分。当我们测试数据 X 来取代 它 ,如果 f(X)>0 则在蓝色组,否则在红色组。我们叫这条线为 决定边界(Decision Boundary)。这非常高效与节省内存。这样可以被直线分为两部分的数据,我们叫做线性可分(Linear Separable)。

下面的这张图,可以看到有许多这样的线。那一条才是我们要的?很直观,我们可以说里所有数据最远的那条就是我们要的。why?因为我们会被传入的数据干扰。数据不能影响分类精度。所以取最远的线有更强的抗干扰。SVM要做的事情就是找到这条离所有训练样本最大最小距离的直线。下图中的粗线通过图形的中间。


查找决定边界(Decision Boundary)需要所有的训练数据,需要么?当然不需要,只要靠近相反组的数据就够了。在本图中,有1个蓝色圆圈和两个红色方框,把它们叫做支持向量(Support Vectors),把通过他们所有的直线叫做支持平面(Support Planes),通过他们就足以找到决定边界,不用关系所有的数据,这样数据量减少了。


发生了什么,首先找到两个最能代表数据的超平面,比如蓝色数据的代表w^Tx+b_0 > 1,而红色数据的代表w^Tx+b_0 < -1,其中w是权重,w=[w_1, w_2,..., w_n],x是特征向量x = [x_1,x_2,..., x_n],b0是偏移量。权重向量决定了决定边界的方向,偏移决定了位置。现在边界被定义在超平面的中间,所以表达式是w^Tx+b_0 = 0。这到支持向量的最短距离就可以确定了,distance_{support \, vectors}=\frac{1}{||w||}。边缘是距离的两倍,我们要最大化这个边缘。比如我们得到一个新的函数L(w,b0)的一些约束,可以表示为:

\min_{w, b_0} L(w, b_0) = \frac{1}{2}||w||^2 \; \text{subject to} \; t_i(w^Tx+b_0) \geq 1 \; \forall i


ti是每一个分类,t_i \in [-1,1].


2,Non-Linearly Separable Data。分线性分割数据。

有些数据是无法用一条直线分为两半。比如一维数据  -3<X<3, -1<Q<1,很明显,这是无法线性分割的。单有一些方法可以解决这类问题,我们可以映射数据集到一个函数上F(x)=X^2。得到X在9,Q在1,这样就线性可分了。

另外我们将一位数据转为二维数据,可以使用F(x,x^2)来映射这些数据。X 变成(3,9)和(-3,9),Q是(-1,1)和(1,1),这样就线性可分了。总之,在低维非线性可分可以增加维数变为线性可分。

通常,将d维空间上的点映射到D维空间(D>d)来改变线性可分。有个概念可以帮我实现低维空间里计算高维空间里的点运算。


可以用下面的例子来说明

2维空间里面的两点,p=(p_1,p_2)q=(q_1,q_2)\phi是可以将二维空间的点映射到三维空间的映射函数,

\phi (p) = (p_{1}^2,p_{2}^2,\sqrt{2} p_1 p_2)\phi (q) = (q_{1}^2,q_{2}^2,\sqrt{2} q_1 q_2)


定义一个核函数K(p,q),可以在为二维空间执行两点间的点运算


K(p,q)  = \phi(p).\phi(q) &= \phi(p)^T , \phi(q) \\                          &= (p_{1}^2,p_{2}^2,\sqrt{2} p_1 p_2).(q_{1}^2,q_{2}^2,\sqrt{2} q_1 q_2) \\                          &= p_{1}^2 q_{1}^2 + p_{2}^2 q_{2}^2 + 2 p_1 q_1 p_2 q_2 \\                          &= (p_1 q_1 + p_2 q_2)^2 \\          \phi(p).\phi(q) &= (p.q)^2


这就是说,一个三维空间的点运算可以用二维空间里点运算的平方来实现。

这也可以应用在高维空间。我们可以从低位空间计算自身在高维空间的特征。


出了这些概念,还有一些分类的问题。所以找到最大边缘的边界是不够的。我们同样要考虑分类的错误,有时,它可能会找到一个边缘较小的边界,来减少了误分类。无论如何我们需要修正一下,我们需要最大边缘的边界也需要最小边缘的边界,最小化标准:

min \; ||w||^2 + C(distance \; of \; misclassified \; samples \; to \; their \; correct \; regions)

下面的图显示这个概念:


每个训练数据的样本定义为参数\xi_i ,是训练样本到‘决定地区’的距离。还没有分类的的数据在自己相应的超平面上,所以他们的距离是0.


所以优化问题为:

\min_{w, b_{0}} L(w,b_0) = ||w||^{2} + C \sum_{i} {\xi_{i}} \text{ subject to } y_{i}(w^{T} x_{i} + b_{0}) \geq 1 - \xi_{i} \text{ and } \xi_{i} \geq 0 \text{ } \forall i


如何选择参数C,很明显,这个问题的答案依赖于训练数据是如何分布的。

尽管没有一个常规答案,但是有利于我们总结一下规则:


1,大数值C提供的方案是提供最小边缘可以减少分类错误。这样产生错误分类就费劲了(不容易产生分类错误),优化后的目标需要较少的参数,分类错误是不允许的。

2,小数值C的方案是使用大只的边缘和较多的分类错误,这情况最小是不会考虑的很多组数量,这需要用大值边缘找到更多的超平面。









Sure, I'd be happy to explain the Support Vector Machine (SVM) model in the context of scikit-learn, a popular machine learning library for Python. SVM is a supervised learning algorithm that can be used for both classification and regression tasks. It is particularly known for its ability to find the optimal hyperplane that maximally separates data points into different classes while minimizing the margin between them. In the case of classification, SVM works well with linearly separable data, but also has a kernel trick that allows it to handle non-linearly separable data through transformations to higher-dimensional spaces. In scikit-learn, the `sklearn.svm` module provides several classes to work with SVMs: 1. `SVC` (for classification): This is the most commonly used class for Support Vector Classification. It implements the C-Support Vector Classification optimization problem, which involves finding the best decision boundary that trades off misclassification errors against the complexity of the model. 2. `LinearSVC`: A variant of SVC specifically designed for linearly separable data, which can be faster and more memory-efficient than SVC. 3. `SVR` (for regression): The Support Vector Regression model, which uses a similar principle but is applied to predict continuous target values instead of binary or categorical ones. Here's a simple example of how to use a linear SVM for classification in scikit-learn: ```python from sklearn import datasets from sklearn.model_selection import train_test_split from sklearn.svm import SVC # Load dataset, e.g., Iris dataset iris = datasets.load_iris() X = iris.data y = iris.target # Split data into training and testing sets X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42) # Initialize a linear SVM classifier clf = SVC(kernel='linear') # Train the model clf.fit(X_train, y_train) # Make predictions on test data predictions = clf.predict(X_test) ``` To learn more about SVM parameters tuning, kernels, and performance evaluation, you might want to explore topics like: 1. Different kernel functions available in `sklearn.svm.SVC` (e.g., 'linear', 'poly', 'rbf', 'sigmoid'). 2. Regularization parameter 'C' and its impact on model complexity. 3. Choosing the right kernel for your specific problem. 4. Understanding the role of gamma and other parameters in kernel functions.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值