python實(shí)現(xiàn)邏輯回歸的示例
代碼
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets.samples_generator import make_classification
def initialize_params(dims):
w = np.zeros((dims, 1))
b = 0
return w, b
def sigmoid(x):
z = 1 / (1 + np.exp(-x))
return z
def logistic(X, y, w, b):
num_train = X.shape[0]
y_hat = sigmoid(np.dot(X, w) + b)
loss = -1 / num_train * np.sum(y * np.log(y_hat) + (1-y) * np.log(1-y_hat))
cost = -1 / num_train * np.sum(y * np.log(y_hat) + (1 - y) * np.log(1 - y_hat))
dw = np.dot(X.T, (y_hat - y)) / num_train
db = np.sum(y_hat - y) / num_train
return y_hat, cost, dw, db
def linear_train(X, y, learning_rate, epochs):
# 參數(shù)初始化
w, b = initialize_params(X.shape[1])
loss_list = []
for i in range(epochs):
# 計(jì)算當(dāng)前的預(yù)測(cè)值、損失和梯度
y_hat, loss, dw, db = logistic(X, y, w, b)
loss_list.append(loss)
# 基于梯度下降的參數(shù)更新
w += -learning_rate * dw
b += -learning_rate * db
# 打印迭代次數(shù)和損失
if i % 10000 == 0:
print("epoch %d loss %f" % (i, loss))
# 保存參數(shù)
params = {
'w': w,
'b': b
}
# 保存梯度
grads = {
'dw': dw,
'db': db
}
return loss_list, loss, params, grads
def predict(X, params):
w = params['w']
b = params['b']
y_pred = sigmoid(np.dot(X, w) + b)
return y_pred
if __name__ == "__main__":
# 生成數(shù)據(jù)
X, labels = make_classification(n_samples=100,
n_features=2,
n_informative=2,
n_redundant=0,
random_state=1,
n_clusters_per_class=2)
print(X.shape)
print(labels.shape)
# 生成偽隨機(jī)數(shù)
rng = np.random.RandomState(2)
X += 2 * rng.uniform(size=X.shape)
# 劃分訓(xùn)練集和測(cè)試集
offset = int(X.shape[0] * 0.9)
X_train, y_train = X[:offset], labels[:offset]
X_test, y_test = X[offset:], labels[offset:]
y_train = y_train.reshape((-1, 1))
y_test = y_test.reshape((-1, 1))
print('X_train=', X_train.shape)
print('y_train=', y_train.shape)
print('X_test=', X_test.shape)
print('y_test=', y_test.shape)
# 訓(xùn)練
loss_list, loss, params, grads = linear_train(X_train, y_train, 0.01, 100000)
print(params)
# 預(yù)測(cè)
y_pred = predict(X_test, params)
print(y_pred[:10])
以上就是python實(shí)現(xiàn)邏輯回歸的示例的詳細(xì)內(nèi)容,更多關(guān)于python 邏輯回歸的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python使用paramiko實(shí)現(xiàn)遠(yuǎn)程拷貝文件的方法
這篇文章主要介紹了python使用paramiko實(shí)現(xiàn)遠(yuǎn)程拷貝文件的方法,分析了paramiko庫(kù)的安裝以及遠(yuǎn)程下載文件的實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-04-04
python mysql 字段與關(guān)鍵字沖突的解決方式
這篇文章主要介紹了python mysql 字段與關(guān)鍵字沖突的解決方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03
基于sklearn實(shí)現(xiàn)Bagging算法(python)
這篇文章主要為大家詳細(xì)介紹了基于sklearn實(shí)現(xiàn)Bagging算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-07-07
詳解Python實(shí)現(xiàn)多進(jìn)程異步事件驅(qū)動(dòng)引擎
本篇文章主要介紹了詳解Python實(shí)現(xiàn)多進(jìn)程異步事件驅(qū)動(dòng)引擎,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-08-08
Python使用PyQt快速搭建桌面應(yīng)用的完整指南
在Python生態(tài)中,PyQt憑借其跨平臺(tái)特性、豐富的控件庫(kù)和成熟的開發(fā)模式,成為桌面應(yīng)用開發(fā)的首選框架之一,本文將以實(shí)戰(zhàn)為導(dǎo)向,通過具體案例拆解開發(fā)流程,幫助開發(fā)者快速掌握PyQt的核心技巧2025-09-09
淺談Tensorflow 動(dòng)態(tài)雙向RNN的輸出問題
今天小編就為大家分享一篇淺談Tensorflow 動(dòng)態(tài)雙向RNN的輸出問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-01-01
Pytorch中求模型準(zhǔn)確率的兩種方法小結(jié)
這篇文章主要介紹了Pytorch中求模型準(zhǔn)確率的兩種方法小結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05
詳解python的幾種標(biāo)準(zhǔn)輸出重定向方式
這篇文章是基于Python2.7版本,介紹常見的幾種標(biāo)準(zhǔn)輸出(stdout)重定向方式。顯然,這些方式也適用于標(biāo)準(zhǔn)錯(cuò)誤重定向。學(xué)習(xí)python的小伙伴們可以參考借鑒。2016-08-08

