Skip to content

Commit

Permalink
how to draw w; close issue #87
Browse files Browse the repository at this point in the history
  • Loading branch information
SmirkCao committed Jun 17, 2019
1 parent b333b5f commit d83a545
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 9 deletions.
12 changes: 6 additions & 6 deletions CH07/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,10 @@ $$
\gamma=\frac{\hat \gamma}{\|w\|}\\
$$

和感知机相比,支持向量机引入间隔,将距离
和感知机相比,支持向量机引入间隔,将距离的大小考虑进来,而不只是符号。

#### 间隔最大化



#### 支持向量和间隔边界

由于支持向量在确定分离超平面中起着决定作用,所以将这种分类模型称为支持向量机。
Expand Down Expand Up @@ -199,16 +197,13 @@ $$
>
> In [mathematical optimization](https://en.wikipedia.org/wiki/Mathematical_optimization) theory, **duality** or the **duality principle** is the principle that [optimization problems](https://en.wikipedia.org/wiki/Optimization_problem) may be viewed from either of two perspectives, the **primal problem** or the **dual problem**. The solution to the dual problem provides a lower bound to the solution of the primal (minimization) problem.[[1\]](https://en.wikipedia.org/wiki/Duality_(optimization)#cite_note-Boyd-1) However in general the optimal values of the primal and dual problems need not be equal. Their difference is called the [duality gap](https://en.wikipedia.org/wiki/Duality_gap). For [convex optimization](https://en.wikipedia.org/wiki/Convex_optimization) problems, the duality gap is zero under a [constraint qualification](https://en.wikipedia.org/wiki/Constraint_qualification) condition.


转换后的对偶问题
$$
\min\limits_\alpha \frac{1}{2}\sum_{i=1}^N\sum_{j=1}^N\alpha_i\alpha_jy_iy_j(x_i\cdot x_j)-\sum_{i=1}^N\alpha_i\\
s.t. \ \ \ \sum_{i=1}^N\alpha_iy_i=0\\
\alpha_i\geqslant0, i=1,2,\dots,N
$$


对于任意线性可分的两组点,他们在分类超平面上的投影都是线性不可分的。

$\alpha$不为零的点对应的实例为支持向量,通过支持向量可以求得$b$值
Expand Down Expand Up @@ -278,6 +273,11 @@ $$

线性支持向量机是线性可分支持向量机的超集。

这章单元测试中包含了几个图,可以直接看代码。
关于$w$是如何绘制的,这个添加了个测试案例,体会一下。

![fig_w](assets/fig_w.png)

### 算法

#### 软间隔最大化
Expand Down
Binary file added CH07/assets/fig_w.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 2 additions & 1 deletion CH07/svm.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#! /usr/bin/env python
#! -*- coding=utf-8 -*-

# -*- coding:utf-8 -*-
# Project: Lihang
# Filename: svm
# Date: 9/27/18
Expand Down
31 changes: 29 additions & 2 deletions CH07/unit_test.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#! /usr/bin/env python
#! -*- coding=utf-8 -*-

# -*- coding:utf-8 -*-
# Project: Lihang
# Filename: unit_test
# Date: 9/27/18
Expand Down Expand Up @@ -28,7 +29,7 @@ def test_e71(self):
{'type': 'ineq', 'fun': lambda x: 4 * x[0] + 3 * x[1] + x[2] - 1},
{'type': 'ineq', 'fun': lambda x: -x[0] - x[1] - x[2] - 1})
res = optimize.minimize(fun, np.ones(3), method='SLSQP', constraints=cons)
logger.info("\n res is \n %s \n x is \n %s\n" % (str(res), res["x"]))
print("\n res is \n %s \n x is \n %s\n" % (str(res), res["x"]))

self.assertListEqual(res["x"].round(2).tolist(), [0.5, 0.5, -2])

Expand Down Expand Up @@ -152,6 +153,32 @@ def test_f76(self):
# plt.savefig("fig76.png")
plt.show()

def test_w(self):
# support vector
alpha = np.array([[0, 0], [1, 3], [2.8, 2]])
# hyperplane
kb = np.polyfit(alpha[1:].T[0], alpha[1:].T[1], 1)
x = np.arange(0, 4, 0.1)
y = kb[0]*x + kb[1]
y_ = -x/kb[0]
plt.figure(figsize=(6, 6))
plt.xlim(0, 4)
plt.ylim(0, 4)
# point
plt.scatter(alpha[:, 0], alpha[:, 1],
marker="o", edgecolors="b", c="w", label="p1,p2")
# vector
plt.plot(alpha[:2].T[0], alpha[:2].T[1], label="o-p1")
plt.plot(alpha[[0, 2]].T[0], alpha[[0, 2]].T[1], label="o-p2")
# point1 - point2
plt.plot(alpha[1:].T[0], alpha[1:].T[1], label="o-p1-p2")
# hyperplane
plt.plot(x, y, alpha=0.3, linestyle="--", label="hyperplane")
# w
plt.plot(x, y_, alpha=0.3, label="w")
plt.legend()
# plt.plot(x, y)
plt.show()

if __name__ == '__main__':
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
Expand Down

0 comments on commit d83a545

Please sign in to comment.