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

Implementation of Logistic Regression

Introduction


Today, I implement Logistic Regression.
My OS of computer is the windows10.
Implementation is used by Python3.
I use the IRLS to estimate optimization value.
I introduce the theory of Logistic Regression in another post.
If you interested, look at this post.

Overview

  • I will introduce used data set
  • I will introduce my code in Python
  • I will show you result on Command line.

Dataset

I use this dataset to implement Logistic Regression.
This dataset is Residential area data.
I diplay this data in Pandas DataFrame Python3.
enter image description here
This is data set from top to five elements.
if people live the house, occupancy is 1.
if people do not live the house, occupancy is 0.
This data consist of 8000 samples to use as training data, and 2000 samples to use as test data.
However I use 100 samples as training data and 100 samples as test data, because my computer is not designated programing.
Sorry, .

CODE

This code is very long.
Thus, I publish my code of Logistice Regression in my Github.
My github page
My Logistic Regression code(def file)
My Logistic Regression code(main file)
Separating my code have reason. It is that I want to separate define file and main file. main file have
if __name__ == '__mian__'
my def file have algorithm of Logistic Regression. My code is defined class in def file. I will write about class in Python.

Execution!

w is estimating…
enter image description here
I will save figure of value of Closs-entropy error function
enter image description here
This is sactter plot of closs entropy error function.
enter image description here
I find out decreasing of value of closs entropy error funtion.
I finished estimating optimization.
I will test my model of Logistic Regression.
enter image description here
enter image description here
I compare my predict by Logistic Regression and correct class.
Percentage of correct answer is 98per.
I think this is high score.
By the way, Logistic Regression find out probability that each data point exists \(C_1\)
Please check out P columns.
As long as I identify, Almost the P is not near 0.5.

コメント

このブログの人気の投稿

変分法の可視化

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...

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 今日はヒープ構造について書きます。ヒープ構造はデータ構造の一種です。ちょうど大学の自主ゼミグループのセミナー合宿に参加させてもらい、そこでグラフ理論を勉強したので、メモをしておこうと思います。   slide  はこんなのを使いました。 Overview データ構造 二分木 ヒープ 実装 ヒープソート データ構造 ヒープ構造の前に、データ構造について、説明します。データ構造とは、データを保存する手法であります。データ構造は、そのデータについてどのような操作を行いたいかによって、最適なものを選ぶことになります。 ヒープ構造はプライオリティキューと呼ばれれるデータ構造を表す方法です。プライオリティキューで行いたい操作は以下の二つです。 データの追加 最小値の抽出 二分木 まず、グラフを定義します。E と V は集合とし、 $e \in E$、つまりEの要素をedge(枝)と呼びます。また、$v \in V$、つまりVの要素をnodeと呼びます。 g:E->V×V をEからV × Vへの写像とします。この時、.(E,V,g)をグラフを言います。 例えば、次のようなものがあります。 丸いのがそれぞれのnodeで、矢印がedgeになります。 各edgeに対して、始点v1と始点v2を対応させるのが写像gの役目です。 根付き木とは次のような木のことです。 これはnode1からnodeが二つずつどんどん派生していっています。 特に、次のような木を 二分木 といいます。 特徴は、ノードが上からなおかつ左から敷き詰められています。一番上のノードを根といいます。また、例えば2を基準にすると、1は2の親、4,5は2の子、3は2の兄弟、8,9,10,11,12は葉と呼ばれます。 ヒープ ヒープ構造はプライオリティキューを二分木で表現したものです。プライオリティキューでやりたいことは次のことでした。 データの追加 最小値の抽出 . では、どのようにこの二つの操作を実現するのでしょうか。 初めにデータの追加について説明します。 1. 二分木の最後に追加す...