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

ロジスティック回帰の実装

Introduction

今日はロジスティック回帰の実装を行いました。
初めに、僕のComputerはosはwindowsです。実装はPython3で行います。
最適化にはIRLSを用いています。
ロジスティック回帰の理論偏にロジスティック回帰の詳しい理論や説明を書いています。
ロジスティック回帰の理論編

概要

  • 使うデータ集合について
  • Pythonでのコードを紹介
  • コマンドラインでの実行結果

Dataset

データセットはこちらを使います。
dataset
このデータセットには住宅街のデータが入っています。
Python3のPandas.DataFrameでの表示を貼っておきます。
enter image description here
上から五行目までを貼っています。
もし、その家に住人がいるのであれば、Occupancyには1が入っています。
反対に、その家に住人がいるのであれば、Ouucpancyには0が入ります。
このデータセットは約8000個のサンプルが入ったトレーニング用データと、約2000個のサンプルが入ったテスト用のデータがあります。
しかし、今回はトレーニング用に100個、テスト用に100個のデータを使います。僕のcomputerがプログラミング用ではないためです。。。
すいません。。。

CODE

このコードはとても長いので、僕のGithubのページに乗せてあるものを見てください。。
githubのページ
ロジスティック回帰(def file)
ロジスティック回帰(main file)
mainファイルには、以下のようなコードが入っているファイルです。コマンドラインで入力するファイルになります。
if __name__ == '__mian__'
defファイルには様々な関数が入っています。クラスも使われています。Pythonのクラスについてはまた、機会があれば書きたいと思います。

いざ、実行!

w を推定します…
enter image description here
wが更新されるごとのクロスエントロピー誤差関数の様子をplotしておきます。
enter image description here
scatterplot でクロスエントロピー誤差関数をplotした画像です。
enter image description here
うまく減少しているのがわかります。
最適化は終わりました。
ではこのモデルのテストを行っていきましょう。
enter image description here
enter image description here
予測値と正しい値を比べてみます。
正答率は98パーセントと高い値を出しています。
ところで、ロジスティック回帰ではそれぞれのデータ点が\(C_1\)に属している確率を出してくれます。
Pの列を確認してみてください。
0.5に近い値のものがあまり、無いと思います。(見えてるところだけですが、、(笑))

コメント

このブログの人気の投稿

Thorem of SVM part 2

Introduction 日本語 ver This post is written about theorem of SVM. This post is part 2. Part 1 is written about deriving the objective function. I will write about dual problem. The objective function which deriving in Part 1 is called the main problem. Typically, We optimize not the main problem, but the dual problem. I will write about deriving the dual problem from the main problem. If you until look up part 1, please look up Theorem of SVM part 1 . I implement SVM. It post is Implement linear SVM Implement kernel SVM Overview Main problem Dual problem  Lagurange function maximize L about dual variable, minimize L about primal variable minimize L about primal variable, maximize L about dual variable. Main problem I will review the main problem in Part 1. $$\min_{w,b} \frac{1}{2}||W||^2 + C\sum_{i \in N} \epsilon_i$$ $$~~s.t~~ \forall i \in N, y_i (w^T \phi(x_i) + b) \geq 1 - \epsilon_i ,~~~, \forall i \in N~\epsilon \geq 0$$ This problem is called s...

K-means 理論編

Introduction English ver 今日はK-meansアルゴリズムの理論について書きます。 K-meansアルゴリズムはクラスタリングのためのアルゴリズムです。 K-meansの実装の記事は カーネルK-meansの実装 を御覧ください。 この記事はカーネルK-menasの実装についての記事ですが、通常のK-meansの実装も行っています。カーネルK-meansについてはまた、今度別の記事で紹介したいと思います。 概要 1 of K 符号化法 プロトタイプ 歪み尺度 最適化 1 of K 符号化法 K-meansはK個のクラスについて分類することを考えます。 K-meansでは $x_n$がkのクラスに属していることを次のように表します。 ベクトル$r_n:1 \times K$ を $$r_n := (0,0,..,1,..,0)$$ このベクトルはk番目にのみ1を持ち、それ以外は0を要素に持つようなベクトルです。 こののような表現の仕方を1 of K符号化法と呼びます。 プロトタイプ K-meansではプロトタイプと呼ばれるベクトルを選びます。このベクトルは各クラスに一つあり、そのクラスの代表のようなベクトルです。 K-means ではそのようなベクトルは各クラスの平均ベクトルとなります。これは目的関数から自然と導かれます。 歪み尺度 プロトタイプベクトルを $\mu_i ~\forall k \in K$とします。 この時、k-meansの目的関数は次のようになります。 $$J = \sum_{n=1}^{N} \sum_{k=1}^{K} r_{nk} ||x_n-\mu_k||^2$$ ここで、 $r_{nk}$ は$r_n$のk番目の要素です。 この目的関数について少し説明をします。$r_{n}$は$x_n$が属しているクラスのラベルの場所だけ1で他は0であるので、 $$J = \sum_{n=1}^{N} ||x_n - \mu_{x_n}||$$ ここで、$\mu_{k_n}$は$x_n$が属しているクラスのプロトタイプです。 よって、 $$J = ||x_1 - \mu_{x_1}|| + ||x_2 -\mu_{x_2}|| + ...

Theorem of K-means

Introduction 日本語 ver Today, I will write about the theorem of K-means algorithm. K-means algorithm is a method to do clustering about K of class. The post of implement K-means is Implement kernel K-means This post is written about kernel K-means. The kernel K-means is the method which covers the weak point of K-means. I will explain kernel K-means another post. Overview 1 of K coding scheme prototype vector Distortion measure Computing 1 of K coding scheme K-means algorithm is clustering K of class. Now, K-means algorithm expresses that $x_n$ be belong to k like as follows. Let vector $r_n:1 \times K$ is $$r_n := (0,0,..,1,..,0)$$ this vector have 1 in element of k'th and have 0 in else. This expression is called 1 of K coding scheme. Prototype vector K-means algorithm chooses vector which called a prototype. this vector has represented by the cluster. K-means algorithm is regard mean vector as representative of the cluster. I...