logist回归
最佳回归系数与Sigmod函数
σ
=
1
/
(
1
+
e
−
z
)
\sigma = 1/(1 + e^{-z})
σ=1/(1+e−z)
z
=
w
0
x
0
+
w
1
x
1
+
w
2
x
2
+
.
.
.
.
w
n
x
n
z = w_0x_0+ w_1x_1+ w_2x_2+ ....w_nx_n
z=w0x0+w1x1+w2x2+....wnxn
z
=
w
T
X
z = w^TX
z=wTX
梯度上升法
w = w + α ∗ g r a d f ( w ) w = w + α* grad f(w) w=w+α∗gradf(w)
"""
函数说明:梯度上升算法测试函数
求函数f(x) = -x^2+4x的极大值
Parameters:
None
Returns:
None
"""
def Gradient_Ascent_test():
# f(x)的导数
def f_prime(x_old):
return -2 * x_old + 4
# 初始值,给一个小于x_new的值
x_old = -1
# 梯度上升算法初始值,即从(0, 0)开始
x_new = 0
# 步长,也就是学习速率,控制更新的幅度
alpha = 0.01
# 精度,也就是更新阈值
presision = 0.00000001
while abs(x_new - x_old) > presision:
x_old = x_new
# 利用上面的公式
x_new = x_old + alpha * f_prime(x_old)
# 打印最终求解的极值近似值
print(x_new)
"""
函数说明:sigmoid函数
Parameters:
inX - 数据
Returns:
sigmoid函数
"""
def sigmoid(inX):
return 1.0 / (1 + np.exp(-inX))
"""
函数说明:梯度上升法
Parameters:
dataMath - 数据集
classLabels - 数据标签
Returns:
weights.getA() - 求得的权重数组(最优参数)
weights_array - 每次更新的回归系数
"""
def gradAscent(dataMath, classLabels):
# 转换成numpy的mat(矩阵)
dataMatrix = np.mat(dataMath)
# 转换成numpy的mat(矩阵)并进行转置
labelMat = np.mat(classLabels).transpose()
# 返回dataMatrix的大小,m为行数,n为列数
m, n = np.shape(dataMatrix)
# 移动步长,也就是学习效率,控制更新的幅度
alpha = 0.01
# 最大迭代次数
maxCycles = 500
weights = np.ones((n, 1))
weights_array = np.array([])
for k in range(maxCycles):
# 梯度上升矢量化公式
h = sigmoid(dataMatrix * weights)
error = labelMat - h
weights = weights + alpha * dataMatrix.transpose() * error
# numpy.append(arr, values, axis=None):就是arr和values会重新组合成一个新的数组,做为返回值。
# 当axis无定义时,是横向加成,返回总是为一维数组
weights_array = np.append(weights_array, weights)
weights_array = weights_array.reshape(maxCycles, n)
# 将矩阵转换为数组,返回权重数组
# mat.getA()将自身矩阵变量转化为ndarray类型变量
return weights.getA(), weights_array
绘制决策边界
"""
函数说明:绘制数据集
Parameters:
weights - 权重参数数组
Returns:
None
"""
def plotBestFit(weights):
# 加载数据集
dataMat, labelMat = loadDataSet()
# 转换成numpy的array数组
dataArr = np.array(dataMat)
# 数据个数
# 例如建立一个4*2的矩阵c,c.shape[1]为第一维的长度2, c.shape[0]为第二维的长度4
n = np.shape(dataMat)[0]
# 正样本
xcord1 = []
ycord1 = []
# 负样本
xcord2 = []
ycord2 = []
# 根据数据集标签进行分类
for i in range(n):
if int(labelMat[i]) == 1:
# 1为正样本
xcord1.append(dataArr[i, 1])
ycord1.append(dataArr[i, 2])
else:
# 0为负样本
xcord2.append(dataArr[i, 1])
ycord2.append(dataArr[i, 2])
# 新建图框
fig = plt.figure()
# 添加subplot
ax = fig.add_subplot(111)
# 绘制正样本
ax.scatter(xcord1, ycord1, s=20, c='red', marker='s', alpha=.5)
# 绘制负样本
ax.scatter(xcord2, ycord2, s=20, c='green', alpha=.5)
# x轴坐标
x = np.arange(-3.0, 3.0, 0.1)
# w0*x0 + w1*x1 * w2*x2 = 0
# x0 = 1, x1 = x, x2 = y
y = (-weights[0] - weights[1] * x) / weights[2]
ax.plot(x, y)
# 绘制title
plt.title('BestFit')
# 绘制label
plt.xlabel('x1')
plt.ylabel('y2')
# 显示
plt.show()
随机梯度上升法
"""
函数说明:改进的随机梯度上升法
Parameters:
dataMatrix - 数据数组
classLabels - 数据标签
numIter - 迭代次数
Returns:
weights - 求得的回归系数数组(最优参数)
weights_array - 每次更新的回归系数
"""
def stocGradAscent1(dataMatrix, classLabels, numIter=150):
# 返回dataMatrix的大小,m为行数,n为列数
m, n = np.shape(dataMatrix)
# 参数初始化
weights = np.ones(n)
weights_array = np.array([])
for j in range(numIter):
dataIndex = list(range(m))
for i in range(m):
# 每次都降低alpha的大小
alpha = 4/(1.0+j+i)+0.01
# 随机选择样本
randIndex = int(random.uniform(0, len(dataIndex)))
# 随机选择一个样本计算h
h = sigmoid(sum(dataMatrix[randIndex] * weights))
# 计算误差
error = classLabels[randIndex] - h
# 更新回归系数
weights = weights + alpha * error * dataMatrix[randIndex]
# 添加返回系数到数组中当axis为0时,数组是加在下面(列数要相同)
weights_array = np.append(weights_array, weights, axis=0)
# 删除已使用的样本
del(dataIndex[randIndex])
# 改变维度
weights_array = weights_array.reshape(numIter*m, n)
# 返回
return weights, weights_array
"""
函数说明:绘制回归系数与迭代次数的关系
Parameters:
weights_array1 - 回归系数数组1
weights_array2 - 回归系数数组2
Returns:
None
"""
def plotWeights(weights_array1, weights_array2):
# 设置汉字格式为14号简体字
font = FontProperties(fname=r"C:\Windows\Fonts\simsun.ttc", size=14)
# 将fig画布分隔成1行1列,不共享x轴和y轴,fig画布的大小为(20, 10)
# 当nrows=3,ncols=2时,代表fig画布被分为6个区域,axs[0][0]代表第一行第一个区域
fig, axs = plt.subplots(nrows=3, ncols=2, sharex=False, sharey=False, figsize=(20, 10))
# x1坐标轴的范围
x1 = np.arange(0, len(weights_array1), 1)
# 绘制w0与迭代次数的关系
axs[0][0].plot(x1, weights_array1[:, 0])
axs0_title_text = axs[0][0].set_title(u'改进的梯度上升算法,回归系数与迭代次数关系', FontProperties=font)
axs0_ylabel_text = axs[0][0].set_ylabel(u'w0', FontProperties=font)
plt.setp(axs0_title_text, size=20, weight='bold', color='black')
plt.setp(axs0_ylabel_text, size=20, weight='bold', color='black')
# 绘制w1与迭代次数的关系
axs[1][0].plot(x1, weights_array1[:, 1])
axs1_ylabel_text = axs[1][0].set_ylabel(u'w1', FontProperties=font)
plt.setp(axs1_ylabel_text, size=20, weight='bold', color='black')
# 绘制w2与迭代次数的关系
axs[2][0].plot(x1, weights_array1[:, 2])
axs2_title_text = axs[2][0].set_title(u'迭代次数', FontProperties=font)
axs2_ylabel_text = axs[2][0].set_ylabel(u'w2', FontProperties=font)
plt.setp(axs2_title_text, size=20, weight='bold', color='black')
plt.setp(axs2_ylabel_text, size=20, weight='bold', color='black')
# x2坐标轴的范围
x2 = np.arange(0, len(weights_array2), 1)
# 绘制w0与迭代次数的关系
axs[0][1].plot(x2, weights_array2[:, 0])
axs0_title_text = axs[0][1].set_title(u'梯度上升算法,回归系数与迭代次数关系', FontProperties=font)
axs0_ylabel_text = axs[0][1].set_ylabel(u'w0', FontProperties=font)
plt.setp(axs0_title_text, size=20, weight='bold', color='black')
plt.setp(axs0_ylabel_text, size=20, weight='bold', color='black')
# 绘制w1与迭代次数的关系
axs[1][1].plot(x2, weights_array2[:, 1])
axs1_ylabel_text = axs[1][1].set_ylabel(u'w1', FontProperties=font)
plt.setp(axs1_ylabel_text, size=20, weight='bold', color='black')
# 绘制w2与迭代次数的关系
axs[2][1].plot(x2, weights_array2[:, 2])
axs2_title_text = axs[2][1].set_title(u'迭代次数', FontProperties=font)
axs2_ylabel_text = axs[2][1].set_ylabel(u'w2', FontProperties=font)
plt.setp(axs2_title_text, size=20, weight='bold', color='black')
plt.setp(axs2_ylabel_text, size=20, weight='bold', color='black')
plt.show()
if __name__ == '__main__':
# 测试简单梯度上升法
# Gradient_Ascent_test()
# 加载数据集
dataMat, labelMat = loadDataSet()
# 训练权重
weights2, weights_array2 = gradAscent(dataMat, labelMat)
# 新方法训练权重
weights1, weights_array1 = stocGradAscent1(np.array(dataMat), labelMat)
# 绘制数据集中的y和x的散点图
# plotBestFit(weights)
# print(gradAscent(dataMat, labelMat))
plotWeights(weights_array1, weights_array2)
梯度下降法
w = w − α ∗ g r a d f ( w ) w = w - α*gradf(w) w=w−α∗gradf(w)