您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關使用python實現邏輯回歸,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
代碼
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): # 參數初始化 w, b = initialize_params(X.shape[1]) loss_list = [] for i in range(epochs): # 計算當前的預測值、損失和梯度 y_hat, loss, dw, db = logistic(X, y, w, b) loss_list.append(loss) # 基于梯度下降的參數更新 w += -learning_rate * dw b += -learning_rate * db # 打印迭代次數和損失 if i % 10000 == 0: print("epoch %d loss %f" % (i, loss)) # 保存參數 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__": # 生成數據 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) # 生成偽隨機數 rng = np.random.RandomState(2) X += 2 * rng.uniform(size=X.shape) # 劃分訓練集和測試集 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) # 訓練 loss_list, loss, params, grads = linear_train(X_train, y_train, 0.01, 100000) print(params) # 預測 y_pred = predict(X_test, params) print(y_pred[:10])
上述就是小編為大家分享的使用python實現邏輯回歸了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。