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神經(jīng)網(wǎng)絡(luò)使用Keras進(jìn)行模型的保存與讀取
這篇文章主要為大家介紹了python神經(jīng)網(wǎng)絡(luò)使用Keras進(jìn)行模型的保存與讀取,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05pandas 強(qiáng)制類型轉(zhuǎn)換 df.astype實(shí)例
這篇文章主要介紹了pandas 強(qiáng)制類型轉(zhuǎn)換 df.astype實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-04-04使用python 計(jì)算百分位數(shù)實(shí)現(xiàn)數(shù)據(jù)分箱代碼
這篇文章主要介紹了使用python 計(jì)算百分位數(shù)實(shí)現(xiàn)數(shù)據(jù)分箱代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧2020-03-03conda創(chuàng)建環(huán)境過程出現(xiàn)"Solving?environment:?failed"報(bào)錯(cuò)的詳細(xì)解
很長(zhǎng)一段時(shí)間沒用conda了,然后突然使用conda創(chuàng)建環(huán)境報(bào)錯(cuò),所以下面這篇文章主要給大家介紹了關(guān)于conda創(chuàng)建環(huán)境過程出現(xiàn)"Solving?environment:?failed"報(bào)錯(cuò)的詳細(xì)解決方法,需要的朋友可以參考下2022-11-11一個(gè)非常簡(jiǎn)單好用的Python圖形界面庫(kù)(PysimpleGUI)
這篇文章主要介紹了一個(gè)非常簡(jiǎn)單好用的Python圖形界面庫(kù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12JetBrains PyCharm(Community版本)的下載、安裝和初步使用圖文教程詳解
這篇文章主要介紹了JetBrains PyCharm(Community版本)的下載、安裝和初步使用教程,本文圖文并茂給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)和工作具有一定的參考借鑒價(jià)值 ,需要的朋友可以參考下2020-03-03