算法面试高频知识点:逻辑回归知识总结
逻辑回归是用在分类问题中的典型算法。
来考虑简单的二分类问题,我们进行一整套的代码流程学习:
步骤一:生成模拟的数据集
为了编写代码模拟二分类任务,我们的第一步工作是先生成用于测试的数据集。首先看下生成的用于模拟的数据集长得样子,它有两个特征w1,w2组成,共有200个样本点,现在的任务是要对这个数据集进行分类。

下面介绍,如何用梯度下降法,求出两个特征对应的权重参数,进而能正确的预测,当一个新的样本点来的时候,能预测出属于0类,还是1类。
步骤二:梯度下降求权重参数
设定一个学习率迭代参数,当上一次迭代的损失函数与当前的损失函数的差小于阈值时,计算结束,我们将得到3个权重参数,其中包括两个特征的权重参数,和偏置项的权重参数。
假定模型的决策边界为线性模型,梯度下降求逻辑回归模型的权重参数的基本思路和四个公式如下:
'model' 建立的逻辑回归模型:包括Sigmoid映射 'cost' 损失函数 'gradient' 梯度公式 'theta update' 参数更新公式 'stop stratege' 迭代停止策略:代价函数小于阈值时停止
,其中,




接着初始化一列偏置项:做一个偏移量和2个特征的组合。
步骤三:写具体代码
'偏置项b shape = (200,1)'
b = np.ones(200)
'将偏移量与2个特征值组合 shape = (200,3)'
X = np.hstack((b,X))
'model'
def sigmoid(x):
return 1 / (1 + np.exp(-x))
def model(theta,X):
theta = np.array(theta)
return sigmoid(X.dot(theta))
'cost'
def cost(m, theta, X, y):
ele = y * np.log(model(theta, X)) + (1 - y) * np.log(1 - model(theta, X))
item_sum = np.sum(ele)
return -item_sum / m
'gradient'
def gradient(m, theta, X, y, cols):
grad_theta = []
for j in range(cols):
grad = (model(theta,X) - y).dot(X[:,j])
grad_sum = np.sum(grad)
grad_theta.append(grad_sum / m)
return np.array(grad_theta)
'theta update'
def theta_update(grad_theta, theta, sigma):
return theta - sigma * grad_theta
'stop stratege'
def stop_stratege(cost, cost_update, threshold):
return cost - cost_update < threshold
'逻辑回归算法'
def LogicRegression(X, y, threshold, m, xcols):
start = time.clock()
'设置权重参数的初始值'
theta = np.zeros(xcols)
'迭代步数'
items = 0;
'记录代价函数的值'
cost_record=[]
***习率'
sigma = 0.01
cost_val = cost(m, theta, X, y)
cost_record.append(cost_val)
while True:
grad = gradient(m, theta, X, y, xcols)
'参数更新'
theta = theta_update(grad, theta, sigma)
cost_update = cost(m, theta, X, y)
if stop_stratege(cost_val, cost_update, threshold):
break
iters = iters + 1
cost_val = cost_update
print("cost_val:%f" %cost_val)
cost_record.append(cost_val)
end = time.clock()
print("LogicRessionconvergene duration: %f s" % (end -start))
return cost_record, iters, theta 步骤四:分析结果
调用逻辑回归函数:LogicRegression(data[:,[0,1,2]],data[:,3],0.00001,200,3)
结果显示经过,逻辑回归梯度下降经过如下时间得到初步收敛,LogicRegression convergence duration:18.076398 s,经过 56172万 多个时步迭代,每个时步计算代价函数的取值,如下图所示:

收敛时,得到的权重参数为:
array([ 0.48528656, 9.48593954, -9.42256868])
参数的含义:第一个权重参数为偏置项,第二、三个权重参数相当,只不过贡献方向相反而已。
下面画出,二分类的决策边界:

查看19道真题和解析
