生活随笔
收集整理的這篇文章主要介紹了
逻辑回归(二)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
邏輯回歸
在學(xué)習(xí)邏輯回歸之前我們先回顧一下線性回歸。線性回歸解決的是回歸問(wèn)題,簡(jiǎn)單來(lái)說(shuō)就是,我們需要找到一個(gè)函數(shù),這個(gè)函數(shù)需要盡可能的擬合所有訓(xùn)練集的樣本點(diǎn)。
邏輯回歸解決的是分類問(wèn)題,它的目標(biāo)是找到一個(gè)函數(shù),利用這個(gè)函數(shù)盡可能把不同種類的數(shù)據(jù)區(qū)分開(kāi)來(lái)。利用sigmod激活函數(shù)可以將前面類似線性回歸的計(jì)算結(jié)果映射為離散值。
通過(guò)激活函數(shù)我們可以將下面圖片中不同位置的點(diǎn)映射為不同的離散值。
需要注意:這個(gè)圖橫縱坐標(biāo)都是自變量X,跟上面的線性回歸是有區(qū)別的。
:return 1.0 / (1 + np
.exp
(-x
))def cost(xMat
, yMat
, ws
):left
= np
.multiply
(yMat
, np
.log
(sigmoid
(xMat
* ws
)))right
= np
.multiply
(1 - yMat
, np
.log
(1 - sigmoid
(xMat
* ws
)))return np
.sum(left
+ right
) / -(len(xMat
))def gradAscent(xArr
, yArr
):if scale
== True:xArr
= preprocessing
.scale
(xArr
)xMat
= np
.mat
(xArr
)yMat
= np
.mat
(yArr
)lr
= 0.001epochs
= 10000costList
= []m
, n
= np
.shape
(xMat
)ws
= np
.mat
(np
.ones
((n
, 1)))for i
in range(epochs
+ 1):h
= sigmoid
(xMat
* ws
)ws_grad
= xMat
.T
* (h
- yMat
) / mws
= ws
- lr
* ws_grad
if i
% 50 == 0:costList
.append
(cost
(xMat
, yMat
, ws
))return ws
, costList
def plot(x_data
, y_data
):x0
= []x1
= []y0
= []y1
= []for i
in range(len(x_data
)):if y_data
[i
] == 0:x0
.append
(x_data
[i
, 0])y0
.append
(x_data
[i
, 1])else:x1
.append
(x_data
[i
, 0])y1
.append
(x_data
[i
, 1])scatter0
= plt
.scatter
(x0
, y0
, c
='b', marker
='o')scatter1
= plt
.scatter
(x1
, y1
, c
='r', marker
='x')plt
.title
("訓(xùn)練數(shù)據(jù)集散點(diǎn)分布")plt
.xlabel
("自變量:x0")plt
.ylabel
("自變量:x1")plt
.legend
(handles
=[scatter0
, scatter1
], labels
=['label0', 'label1'], loc
='best')plt
.savefig
("LR_scatter.png")def plot_result(ws
,x_data
,y_data
):plot
(x_data
,y_data
)x_test
= [[-4],[3]]y_test
= (-ws
[0] - x_test
*ws
[1])/ws
[2]plt
.plot
(x_test
, y_test
, 'k')plt
.savefig
("LR_result.png")plt
.show
()def plot_loss(costList
):x
= np
.linspace
(0, 10000, 201)plt
.plot
(x
, costList
, c
='r')plt
.title
('Train')plt
.xlabel
('Epochs')plt
.ylabel
('Cost')plt
.savefig
("LR_Loss.png")plt
.show
()
def predict(x_data
, ws
):if scale
== True:x_data
= preprocessing
.scale
(x_data
)xMat
= np
.mat
(x_data
)ws
= np
.mat
(ws
)return [1 if x
>= 0.5 else 0 for x
in sigmoid
(xMat
*ws
)]def train():data
= np
.genfromtxt
("LR-testSet.csv", delimiter
=",")x_data
= data
[:, :-1]y_data
= data
[:, -1]plot
(x_data
, y_data
)x_data
= data
[:, :-1]y_data
= data
[:, -1, np
.newaxis
]print("x_data的數(shù)據(jù)形狀為:", np
.mat
(x_data
).shape
)print("y_data的數(shù)據(jù)形狀為:", np
.mat
(y_data
).shape
)X_data
= np
.concatenate
((np
.ones
((100, 1)), x_data
), axis
=1)print("x_data添加偏執(zhí)后X_data的數(shù)據(jù)形狀為:", X_data
.shape
)ws
, costList
= gradAscent
(X_data
, y_data
)print("訓(xùn)練后得到的權(quán)值列表為:", ws
)print("保存決策邊界結(jié)果圖像")plot_result
(ws
, x_data
, y_data
)predictions
= predict
(X_data
, ws
)print(classification_report
(y_data
, predictions
))print("保存loss下降結(jié)果……")plot_loss
(costList
)if __name__
== '__main__':train
()
運(yùn)行結(jié)果:
x_data的數(shù)據(jù)形狀為: (100, 2)
y_data的數(shù)據(jù)形狀為: (100, 1)
x_data添加偏執(zhí)后X_data的數(shù)據(jù)形狀為: (100, 3)
訓(xùn)練后得到的權(quán)值列表為: [[ 2.05836354]
[ 0.3510579 ]
[-0.36341304]]
保存決策邊界結(jié)果圖像
precision recall f1-score support
0.0 0.82 1.00 0.90 471.0 1.00 0.81 0.90 53accuracy 0.90 100
macro avg 0.91 0.91 0.90 100
weighted avg 0.92 0.90 0.90 100
保存loss下降結(jié)果……
輸出圖片:
總結(jié)
以上是生活随笔為你收集整理的逻辑回归(二)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。