习题6.2

  写出Logistic回归模型学习的梯度下降算法。


解答:

对于Logistic模型:
习题6.2 - 图1
对数似然函数为:习题6.2 - 图2

似然函数求偏导,可得 习题6.2 - 图3

梯度函数为:习题6.2 - 图4

Logistic回归模型学习的梯度下降算法:
(1) 取初始值习题6.2 - 图5,置 k=0
(2) 计算习题6.2 - 图6
(3) 计算梯度习题6.2 - 图7,当习题6.2 - 图8时,停止迭代,令习题6.2 - 图9;否则,求习题6.2 - 图10,使得习题6.2 - 图11
(4) 置习题6.2 - 图12,计算习题6.2 - 图13,当习题6.2 - 图14习题6.2 - 图15时,停止迭代,令习题6.2 - 图16
(5) 否则,置 k=k+1,转(3)

  1. %matplotlib inline
  2. import numpy as np
  3. import time
  4. import matplotlib.pyplot as plt
  5. from mpl_toolkits.mplot3d import Axes3D
  6. from pylab import mpl
  7. # 图像显示中文
  8. mpl.rcParams['font.sans-serif'] = ['Microsoft YaHei']
  9. class LogisticRegression:
  10. def __init__(self, learn_rate=0.1, max_iter=10000, tol=1e-2):
  11. self.learn_rate = learn_rate # 学习率
  12. self.max_iter = max_iter # 迭代次数
  13. self.tol = tol # 迭代停止阈值
  14. self.w = None # 权重
  15. def preprocessing(self, X):
  16. """将原始X末尾加上一列,该列数值全部为1"""
  17. row = X.shape[0]
  18. y = np.ones(row).reshape(row, 1)
  19. X_prepro = np.hstack((X, y))
  20. return X_prepro
  21. def sigmod(self, x):
  22. return 1 / (1 + np.exp(-x))
  23. def fit(self, X_train, y_train):
  24. X = self.preprocessing(X_train)
  25. y = y_train.T
  26. # 初始化权重w
  27. self.w = np.array([[0] * X.shape[1]], dtype=np.float)
  28. k = 0
  29. for loop in range(self.max_iter):
  30. # 计算梯度
  31. z = np.dot(X, self.w.T)
  32. grad = X * (y - self.sigmod(z))
  33. grad = grad.sum(axis=0)
  34. # 利用梯度的绝对值作为迭代中止的条件
  35. if (np.abs(grad) <= self.tol).all():
  36. break
  37. else:
  38. # 更新权重w 梯度上升——求极大值
  39. self.w += self.learn_rate * grad
  40. k += 1
  41. print("迭代次数:{}次".format(k))
  42. print("最终梯度:{}".format(grad))
  43. print("最终权重:{}".format(self.w[0]))
  44. def predict(self, x):
  45. p = self.sigmod(np.dot(self.preprocessing(x), self.w.T))
  46. print("Y=1的概率被估计为:{:.2%}".format(p[0][0])) # 调用score时,注释掉
  47. p[np.where(p > 0.5)] = 1
  48. p[np.where(p < 0.5)] = 0
  49. return p
  50. def score(self, X, y):
  51. y_c = self.predict(X)
  52. error_rate = np.sum(np.abs(y_c - y.T)) / y_c.shape[0]
  53. return 1 - error_rate
  54. def draw(self, X, y):
  55. # 分离正负实例点
  56. y = y[0]
  57. X_po = X[np.where(y == 1)]
  58. X_ne = X[np.where(y == 0)]
  59. # 绘制数据集散点图
  60. ax = plt.axes(projection='3d')
  61. x_1 = X_po[0, :]
  62. y_1 = X_po[1, :]
  63. z_1 = X_po[2, :]
  64. x_2 = X_ne[0, :]
  65. y_2 = X_ne[1, :]
  66. z_2 = X_ne[2, :]
  67. ax.scatter(x_1, y_1, z_1, c="r", label="正实例")
  68. ax.scatter(x_2, y_2, z_2, c="b", label="负实例")
  69. ax.legend(loc='best')
  70. # 绘制p=0.5的区分平面
  71. x = np.linspace(-3, 3, 3)
  72. y = np.linspace(-3, 3, 3)
  73. x_3, y_3 = np.meshgrid(x, y)
  74. a, b, c, d = self.w[0]
  75. z_3 = -(a * x_3 + b * y_3 + d) / c
  76. ax.plot_surface(x_3, y_3, z_3, alpha=0.5) # 调节透明度
  77. plt.show()
  78. # 训练数据集
  79. X_train = np.array([[3, 3, 3], [4, 3, 2], [2, 1, 2], [1, 1, 1], [-1, 0, 1],[2, -2, 1]])
  80. y_train = np.array([[1, 1, 1, 0, 0, 0]])
  81. # 构建实例,进行训练
  82. clf = LogisticRegression()
  83. clf.fit(X_train, y_train)
  84. clf.draw(X_train, y_train)
  85. 迭代次数:3232
  86. 最终梯度:[ 0.00144779 0.00046133 0.00490279 -0.00999848]
  87. 最终权重:[ 2.96908597 1.60115396 5.04477438 -13.43744079]

**

image.png