2018-11-09
閱讀量:
913
邏輯回歸的Python實(shí)現(xiàn)
需要:sigmoid函數(shù)、模型主體、參數(shù)初始化、基于梯度下降的參數(shù)更新訓(xùn)練、數(shù)據(jù)測(cè)試與可視化展示。
先定義一個(gè) sigmoid 函數(shù):
import numpy as np
def sigmoid(x):
? ? z = 1 / (1 + np.exp(-x))? ?
? ? return z
定義模型參數(shù)初始化函數(shù):
def initialize_params(dims):
? ? W = np.zeros((dims, 1))
? ? b = 0
? ? return W, b
定義邏輯回歸模型主體部分,包括模型計(jì)算公式、損失函數(shù)和參數(shù)的梯度公式:
def logistic(X, y, W, b):
? ? num_train = X.shape[0]
? ? num_feature = X.shape[1]
? ? a = sigmoid(np.dot(X, W) + b)
? ? cost = -1/num_train * np.sum(y*np.log(a) + (1-y)*np.log(1-a))
? ? dW = np.dot(X.T, (a-y))/num_train
? ? db = np.sum(a-y)/num_train
? ? cost = np.squeeze(cost)
? ? return a, cost, dW, db
定義基于梯度下降的參數(shù)更新訓(xùn)練過程:
def logistic_train(X, y, learning_rate, epochs):? ?
? ? # 初始化模型參數(shù)
? ? W, b = initialize_params(X.shape[1])??
? ? cost_list = []??
? ? # 迭代訓(xùn)練
? ? for i in range(epochs):? ?? ?
? ?? ???# 計(jì)算當(dāng)前次的模型計(jì)算結(jié)果、損失和參數(shù)梯度
? ?? ???a, cost, dW, db = logistic(X, y, W, b)? ?
? ?? ???# 參數(shù)更新
? ?? ???W = W -learning_rate * dW
? ?? ???b = b -learning_rate * db? ?? ???
? ?? ???# 記錄損失
? ?? ???if i % 100 == 0:
? ?? ?? ?? ?cost_list.append(cost)? ?
? ?? ???# 打印訓(xùn)練過程中的損失
? ?? ???if i % 100 == 0:
? ?? ?? ?? ?print('epoch %d cost %f' % (i, cost))
? ? # 保存參數(shù)
? ? params = {? ?? ?? ?? ?
? ?? ???'W': W,? ?? ?? ?? ?
? ?? ???'b': b
? ? }? ?? ???
? ? # 保存梯度
? ? grads = {? ?? ?? ?? ?
? ?? ???'dW': dW,? ?? ?? ?? ?
? ?? ???'db': db
? ? }? ?? ?? ???
? ? return cost_list, params, grads
定義對(duì)測(cè)試數(shù)據(jù)的預(yù)測(cè)函數(shù):
def predict(X, params):
? ? y_prediction = sigmoid(np.dot(X, params['W']) + params['b'])
? ? for i in range(len(y_prediction)):? ?? ???
? ?? ???if y_prediction[i] > 0.5:
? ?? ?? ?? ?y_prediction[i] = 1
? ?? ???else:
? ?? ?? ?? ?y_prediction[i] = 0
? ?eturn y_prediction
使用 sklearn 生成模擬的二分類數(shù)據(jù)集進(jìn)行模型訓(xùn)練和測(cè)試:
import matplotlib.pyplot as plt
from sklearn.datasets.samples_generator import make_classification
X,labels=make_classification(n_samples=100, n_features=2, n_redundant=0, n_informative=2, random_state=1, n_clusters_per_class=2)
rng=np.random.RandomState(2)
X+=2*rng.uniform(size=X.shape)
unique_lables=set(labels)
colors=plt.cm.Spectral(np.linspace(0, 1, len(unique_lables)))
for k, col in zip(unique_lables, colors):
? ? x_k=X[labels==k]
? ? plt.plot(x_k[:, 0], x_k[:, 1], 'o', markerfacecolor=col, markeredgecolor="k",
? ?? ?? ?? ? markersize=14)
plt.title('data by make_classification()')
plt.show()






評(píng)論(0)


暫無數(shù)據(jù)
CDA考試動(dòng)態(tài)
CDA報(bào)考指南
推薦帖子
0條評(píng)論
0條評(píng)論
0條評(píng)論
0條評(píng)論