スキップしてメイン コンテンツに移動

Implement linear SVM

Introduction


Today, I implement liner SVM (support vector machine).
SVM is one of the most strong algorithm of machine learning before becoming popular Deep learning.
I will write detail of Logic of liner SVM in other posts.

# I finished writing theorem of SVM.
Theorem of SVM part1

My computer is windows. OS is windows. Program is written by Python3.
This program is used Interrior point method in Oputimization.

Dataset

I used two dataset.
First, I used data separated by hyper plane.
Second, I used data mixed \(C_1\) and \(C_2\).
First data is distributed such as the following.
enter image description here
I can write separate line of distribution.
Second data is distributed such as the following.
enter image description here
I can not write separate line of distribution.
I will try liner SVM about these data.

Implementation

  • data1
    enter image description here
This line is hyper plane.
It is written like copperplate.
  • data2
    I tried to estimate hyper plane by variable C.
enter image description here
enter image description here
enter image description here
C value cause this line.
The smoller C value is the easier permitting miss classification.
It is difficult for our to confime effect of C.
It is easy to confime effect of C when I use non liner SVM.
I will write non liner SVM another post.

CODE

My code of liner SVM is opend to the github.
My SVM code is here
File used this time is git_SVM_check.py and git_SVM_def.py .
git_SVM_check.py is main file had following code.
if __name__ == '__main__':
git_SVM_def have class of SVM and code of interror point method.
I will write post of nonliner SVM.
If you see next post also ,I am very glad.

コメント

このブログの人気の投稿

Rolle’s theorem

Introduction 日本語 ver This post is written Rolle’s theorem. The mean-value theorem is proved by Rolle’s theorem. I will write Mean-value theorem at a later. I introduce Maximum principle because proving Rolle’s theorem need Maximum principle. Maximum principle It is very easy. f is continuous function on bounded closed interval.\(\implies\)** f have max value.** Proof This proof is difficult. I write this proof in other posts. Maximum Principle Rolle’s theorem f is continuous function on [a,b] and differentiable function on (a,b). \[f(a) = f(b) \implies \exists ~~c ~~s.t~~ f'(c) = 0 , a<c<b\] Proof f(x) is constant function \[\forall c \in (a,b) , f'(c) = 0\] else when \(\exists t ~~s.t~~f(a) < f(t)\), \(\exists c ~~s.t~~ \max f(x) = f(c)\) by Maximum principle I proof \(f'(c)=0\) f is differentiable on \(x = c\) and \(f(c) >= f(c+h)\). Thus \[f'(c) = \lim_{h \rightarrow +0} \frac{f(c+h) - f(c)}{h} \leq 0\] \[f'(c) = \lim...

MAP推定

Introduction English ver 今日はMAP推定(事後確率最大化法)について書きました。MAP推定ではベイズの定理を使います。データが少ないとき、最尤推定の結果をあまり信用できない話は、最尤推定の時に書きました。この時、MAP推定では自分の事前に持っている情報を取り入れることができます。 概要 ベイズの定理 MAP推定 共役分布 MAP推定の例 ベイズの定理 ベイズの定理は $$P(A|B) = \frac{P(B|A)P(A)}{P(B)}$$ です。 ただし、 $P(A|B)$ はBが起こった時のAの起こる確率です。 詳しくは  http://takutori.blogspot.com/2018/04/bayes-theorem.html  を見てください。 Map推定 MAP推定ではベイズの定理を使います。MAP推定は事後確率が最大になるようなパラメータを選びます。 いま、$x_1,x_2,...,x_n$というデータを$\theta$というパラメータを持つ分布から得られたとする。この時$P(\theta|x_1,x_2,...,x_n)$を求めたい。 ここで、ベイズの定理を使う。 $$P(\theta|x_1,x_2,...,x_n) = \frac{P(x_1,x_2,...,x_n | \theta ) P(\theta)}{P(x_1,x_2,...,x_n)}$$ ここで、$P(\theta)$は$\theta$の事前分布である。 $x_1,x_2,...,x_n$はそれぞれ独立であるので、 $$P(x_1,x_2,...,x_n | \theta ) = \Pi_{i=1}^n P(x_i|\theta)$$. よって、マップ推定は $$\theta^{\star} = \arg \max_{\theta} \frac{\Pi_{i=1}^n P(x_i|\theta) P(\theta)}{P(x_1,x_2,...,x_n)}$$ となる。 $P(x_1,x_2,...,x_n)$という値は$\theta$には依存しない。よって、定数であり、最適化に定数は関係ないので、排除すると、MAP推定は次のようになる。 $$\th...

変分法の可視化

Introduction English ver 今日は、変分法の可視化を実装しました。変分法は、汎関数を最小化させるために使われます。汎関数とは、関数の関数のようなものです。変分法については、  [1] , [2] , [3] , [5] ,  [6] などを参考にしてください。 概要 汎関数 実装 可視化 汎関数 今回は、次のような汎関数を使います。 $$F(x) = \sqrt{1+(\frac{du}{dx}(x))^2}$$ $$l(u) = \int_{0}^{1} \sqrt{1+(\frac{du}{dx}(x))^2} dx$$ l(u)はu(x)という曲線の長さです。.  $u(0)=a$ and $u(1)=b$という制約のもと、$l(u)$を最小化したいといます。 最適な$l(u)$は $$u(x) = (b-a)x+a$$ となります。 (0,a) から (1,b)への直線になっているのがわかります。 これは、$l(u)$は$u$の曲線の長さなので、これを最小化するためには直線が一番であることが直観的にわかります。 変分法での導出は、 [5] を参考にしてください。 実装 変分法における最適な曲線とそうでない曲線の違いを可視化する実装をしました。 $u_A$を $$u_A = (b-a)x+a + A sin(8t)$$ とします。 $A sin(8t)$ は$u$から話す役割を持ちます。. $A \in [0,0.5]$であり、もし$A=0$であれば、$u_A=u$です。 github でcodeを公開しています。 可視化 上側の画像は$u_A(x)$を表しています。下側の画像は$l(u_A)$の値を表しています。 $u_A(x)$が$u$に近づくほど、$l(u_A)$が小さくなることがわかります。 Reference [1] http://www2.kaiyodai.ac.jp/~takenawa/optimization/resume10-4.pdf [2] http://hooktail.sub.jp/mathInPhys/brach...