sigmoid函数：

## 2.2 代码

```import numpy as np
import matplotlib.pyplot as plt
# 定义激活函数sigmoid
def sigmoid(z):
return 1.0 / (1 + np.exp(-z))
# datas NxD
# labs Nx1
# w    Dx1
# 权重更新
def weight_update(datas, labs, w, alpha=0.01):
z = np.dot(datas, w)  # Nx1,神经元接受的数据综合体，有N个数据
h = sigmoid(z)  # Nx1，激活函数的值，0-1之间，相对于预测值
Error = labs - h  # Nx1，预测值与实际值的误差，Y-h
w = w + alpha * np.dot(datas.T, Error)
return w
#进行训练，求解参数，非随机梯度下降
def train_LR(datas, labs, n_epoch=2, alpha=0.005):
N, D = np.shape(datas)# datas NxD
w = np.ones([D, 1])  # Dx1，给权重赋初始值，都是1
# 进行n_epoch轮迭代
for i in range(n_epoch):
w = weight_update(datas, labs, w, alpha)
error_rate = test_accuracy(datas, labs, w)#计算误差率
print("epoch %d error %.3f%%" % (i, error_rate * 100))
return w
# 随机梯度下降，带batchsize的，可以使更快的找到导数为0的点，而不会在此左右徘徊，alpha也不能取太大，导致在导数为0的点处左右徘徊
def train_LR_batch(datas, labs, batchsize, n_epoch=2, alpha=0.005):
N, D = np.shape(datas)
# weight 初始化
w = np.ones([D, 1])  # Dx1
N_batch = N // batchsize
for i in range(n_epoch):
# 数据打乱
rand_index = np.random.permutation(N).tolist()
# 每个batch 更新一下weight
for j in range(N_batch):
# alpha = 4.0/(i+j+1) +0.01
index = rand_index[j * batchsize:(j + 1) * batchsize]
batch_datas = datas[index]
batch_labs = labs[index]
w = weight_update(batch_datas, batch_labs, w, alpha)
error = test_accuracy(datas, labs, w)
print("epoch %d  误差率  %.2f%%" % (i, error * 100))
return w
# 测试精确性，与模型无关，用于提醒展示效果，作用是计算误差率
def test_accuracy(datas, labs, w):
N, D = np.shape(datas)
z = np.dot(datas, w)  # Nx1
h = sigmoid(z)  # Nx1
lab_det = (h > 0.5).astype(np.float)
error_rate = np.sum(np.abs(labs - lab_det)) / N
return error_rate
# 画图，直观地表示出结果
def draw_desion_line(datas, labs, w, name="0.jpg"):
dic_colors = {0: (.8, 0, 0), 1: (0, .8, 0)}
# 画数据点
for i in range(2):
index = np.where(labs == i)[0]
sub_datas = datas[index]
plt.scatter(sub_datas[:, 1], sub_datas[:, 2], s=16., color=dic_colors[i])
# 画判决线
min_x = np.min(datas[:, 1])
max_x = np.max(datas[:, 1])
w = w[:, 0]
x = np.arange(min_x, max_x, 0.01)
y = -(x * w[1] + w[0]) / w[2]
plt.plot(x, y)
plt.savefig(name)
#加载数据集进行训练或者加载测试数据对模型进行检验
with open(file, "r", encoding="utf-8") as f:
# 取 lab 维度为 N x 1
labs = [line.split("\t")[-1] for line in lines]
labs = np.array(labs).astype(np.float32)
labs = np.expand_dims(labs, axis=-1)  # Nx1
# 取数据 增加 一维全是1的特征
datas = [line.split("\t")[:-1] for line in lines]
datas = np.array(datas).astype(np.float32)
N, D = np.shape(datas)
# 增加一个维度
datas = np.c_[np.ones([N, 1]), datas]
return datas, labs
if __name__ == "__main__":
# 加载数据
file = "1.txt"
datas, labs = load_dataset(file)
weights = train_LR_batch(datas, labs,batchsize=2, alpha=0.001, n_epoch=800)
print('w1是{},w2是{},b是{}'.format(weights[0][0],weights[1][0],weights[2][0]))
#回归直线就是w1x+w2y+b=0
draw_desion_line(datas, labs, weights, name="test_1.jpg")#将结果保存为jpg文件```