Skip to content

Commit 507d3fa

Browse files
committed
Logistic Regression 说明
1 parent 2b59aac commit 507d3fa

5 files changed

Lines changed: 25 additions & 3 deletions

File tree

‎LogisticRegression/LogisticRegression.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def gradient(initial_theta,X,y,inital_lambda):
9898
def sigmoid(z):
9999
h = np.zeros((len(z),1)) # 初始化,与z的长度一置
100100

101-
h = 1.0/(1+np.exp(-z))
101+
h = 1.0/(1.0+np.exp(-z))
102102
return h
103103

104104

‎formula/LogisticRegression_01.wmf‎

3.08 KB
Binary file not shown.

‎images/LogisticRegression_01.png‎

17.9 KB
Loading

‎images/LogisticRegression_02.png‎

15.6 KB
Loading

‎readme.md‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ def featureNormaliza(X):
8080
![enter description here][1]
8181

8282

83-
[1]: ./images/LinearRegression_01.png "LinearRegression_01.png"
84-
8583
### 5、[使用scikit-learn库中的线性模型实现](/LinearRegression/LinearRegression_scikit-learn.py)
8684
- 导入包
8785
```
@@ -109,3 +107,27 @@ from sklearn.preprocessing import StandardScaler #引入缩放的包
109107
```
110108

111109
## 二、逻辑回归
110+
- [全部代码](/LogisticRegression/LogisticRegression.py)
111+
### 1、代价函数
112+
- ![\left\{ \begin{gathered}
113+
J(\theta ) = \frac{1}{m}\sum\limits_{i = 1}^m {\cos t({h_\theta }({x^{(i)}}),{y^{(i)}})} \hfill \\
114+
\cos t({h_\theta }(x),y) = \left\{ {\begin{array}{c} { - \log ({h_\theta }(x))\begin{array}{c} {}& {y = 1} \end{array} } \\ { - \log (1 - {h_\theta }(x))\begin{array}{c} {}& {y = 0} \end{array} } \end{array} } \right. \hfill \\
115+
\end{gathered} \right.](http://chart.apis.google.com/chart?cht=tx&chs=1x0&chf=bg,s,FFFFFF00&chco=000000&chl=%5Cleft%5C%7B%20%5Cbegin%7Bgathered%7D%0A%20%20J%28%5Ctheta%20%29%20%3D%20%5Cfrac%7B1%7D%7Bm%7D%5Csum%5Climits_%7Bi%20%3D%201%7D%5Em%20%7B%5Ccos%20t%28%7Bh_%5Ctheta%20%7D%28%7Bx%5E%7B%28i%29%7D%7D%29%2C%7By%5E%7B%28i%29%7D%7D%29%7D%20%20%5Chfill%20%5C%5C%0A%20%20%5Ccos%20t%28%7Bh_%5Ctheta%20%7D%28x%29%2Cy%29%20%3D%20%5Cleft%5C%7B%20%7B%5Cbegin%7Barray%7D%7Bc%7D%20%20%20%20%7B%20-%20%5Clog%20%28%7Bh_%5Ctheta%20%7D%28x%29%29%5Cbegin%7Barray%7D%7Bc%7D%20%20%20%20%7B%7D%26%20%7By%20%3D%201%7D%20%20%5Cend%7Barray%7D%20%7D%20%5C%5C%20%20%20%20%7B%20-%20%5Clog%20%281%20-%20%7Bh_%5Ctheta%20%7D%28x%29%29%5Cbegin%7Barray%7D%7Bc%7D%20%20%20%20%7B%7D%26%20%7By%20%3D%200%7D%20%20%5Cend%7Barray%7D%20%7D%20%20%5Cend%7Barray%7D%20%7D%20%5Cright.%20%5Chfill%20%5C%5C%20%0A%5Cend%7Bgathered%7D%20%20%5Cright.)
116+
- 可以综合起来为:
117+
![J(\theta ) = - \frac{1}{m}\sum\limits_{i = 1}^m {[{y^{(i)}}\log ({h_\theta }({x^{(i)}}) + (1 - } {y^{(i)}})\log (1 - {h_\theta }({x^{(i)}})]](http://chart.apis.google.com/chart?cht=tx&chs=1x0&chf=bg,s,FFFFFF00&chco=000000&chl=J%28%5Ctheta%20%29%20%3D%20%20-%20%5Cfrac%7B1%7D%7Bm%7D%5Csum%5Climits_%7Bi%20%3D%201%7D%5Em%20%7B%5B%7By%5E%7B%28i%29%7D%7D%5Clog%20%28%7Bh_%5Ctheta%20%7D%28%7Bx%5E%7B%28i%29%7D%7D%29%20%2B%20%281%20-%20%7D%20%7By%5E%7B%28i%29%7D%7D%29%5Clog%20%281%20-%20%7Bh_%5Ctheta%20%7D%28%7Bx%5E%7B%28i%29%7D%7D%29%5D)
118+
其中:
119+
![{h_\theta }(x) = \frac{1}{{1 + {e^{ - x}}}}](http://chart.apis.google.com/chart?cht=tx&chs=1x0&chf=bg,s,FFFFFF00&chco=000000&chl=%7Bh_%5Ctheta%20%7D%28x%29%20%3D%20%5Cfrac%7B1%7D%7B%7B1%20%2B%20%7Be%5E%7B%20-%20x%7D%7D%7D%7D)
120+
- 为什么不用线性回归的代价函数表示,因为线性回归的代价函数可能是非凸的,对于分类问题,使用梯度下降很难得到最小值,上面的代价函数是凸函数
121+
- ![{ - \log ({h_\theta }(x))}](http://chart.apis.google.com/chart?cht=tx&chs=1x0&chf=bg,s,FFFFFF00&chco=000000&chl=%7B%20-%20%5Clog%20%28%7Bh_%5Ctheta%20%7D%28x%29%29%7D)的图像如下,即`y=1`时:
122+
![enter description here][2]
123+
可以看出,当![{{h_\theta }(x)}](http://chart.apis.google.com/chart?cht=tx&chs=1x0&chf=bg,s,FFFFFF00&chco=000000&chl=%7B%7Bh_%5Ctheta%20%7D%28x%29%7D)趋于`1`,`y=1`,与预测值一致,此时付出的代价`cost`趋于`0`,若![{{h_\theta }(x)}](http://chart.apis.google.com/chart?cht=tx&chs=1x0&chf=bg,s,FFFFFF00&chco=000000&chl=%7B%7Bh_%5Ctheta%20%7D%28x%29%7D)趋于`0`,`y=1`,此时的代价`cost`值非常大,我们最终的目的是最小化带价值代价值
124+
- 同理![{ - \log (1 - {h_\theta }(x))}](http://chart.apis.google.com/chart?cht=tx&chs=1x0&chf=bg,s,FFFFFF00&chco=000000&chl=%7B%20-%20%5Clog%20%281%20-%20%7Bh_%5Ctheta%20%7D%28x%29%29%7D)的图像如下(`y=0`):
125+
![enter description here][3]
126+
127+
128+
129+
130+
131+
[1]: ./images/LinearRegression_01.png "LinearRegression_01.png"
132+
[2]: ./images/LogisticRegression_01.png "LogisticRegression_01.png"
133+
[3]: ./images/LogisticRegression_02.png "LogisticRegression_02.png"

0 commit comments

Comments
 (0)