求解逻辑回归—-梯度下降
2020-06-28 14:38:03 来源:易采站长站 作者:易采站长站整理
theta = theta - alpha*grad # 参数更新
costs.append(costFunction(X,y,theta)) # 计算新的损失
i += 1
if stopType == STOP_ITER: value = i
elif stopType == STOP_COST: value = costs
elif stopType == STOP_GRAD: value = grad
if stopCriterion(stopType,value,thresh): break
return theta,i-1,costs,grad,time.time()-init_time
import numpy.random# 洗牌
def shuffleData(data):
np.random.shuffle(data)
cols = data.shape[1] X = data[:, 0:cols-1] y = data[:, cols-1:] return X, y
def runExpe(data, theta, batchSize, stopType, thresh, alpha):
#import pdb; pdb.set_trace();
theta, iter, costs, grad, dur = descent(data, theta, batchSize, stopType, thresh, alpha)
name = "Original" if (data[:,1]>2).sum() > 1 else "Scaled"
name += " data - learning rate: {} - ".format(alpha)
if batchSize==n: strDescType = "Gradient"
elif batchSize==1: strDescType = "Stochastic"
else: strDescType = "Mini-batch ({})".format(batchSize)
name += strDescType + " descent - Stop: "
if stopType == STOP_ITER: strStop = "{} iterations".format(thresh)
elif stopType == STOP_COST: strStop = "costs change < {}".format(thresh)
else: strStop = "gradient norm < {}".format(thresh)
name += strStop
print ("***{}nTheta: {} - Iter: {} - Last cost: {:03.2f} - Duration: {:03.2f}s".format(
name, theta, iter, costs[-1], dur))
fig, ax = plt.subplots(figsize=(12,4))
ax.plot(np.arange(len(costs)), costs, 'r')
ax.set_xlabel('Iterations')
ax.set_ylabel('Cost')
ax.set_title(name.upper() + ' - Error vs. Iteration')
return theta
# 选择的梯度下降方法是基于所有样本的
n=100
runExpe(orig_data, theta, n, STOP_ITER, thresh=5000, alpha=0.000001)
***Original data - learning rate: 1e-06 - Gradient descent - Stop: 5000 iterations
Theta: [[-0.00026672] [ 0.00668449] [ 0.00450233]] - Iter: 5000 - Last cost: 0.63 - Duration: 0.95sarray([[-0.00026672],
[ 0.00668449],
[ 0.00450233]])
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-gGTs4Cl3-1585660078918)(output_24_2.png)]](https://www.easck.com/d/file/200628/20200628143636705.jpg)
# 根据损失值停止,设定阈值为1E-6
runExpe(orig_data, theta, n, STOP_COST, thresh=0.000001, alpha=0.001)
***Original data - learning rate: 0.001 - Gradient descent - Stop: costs change < 1e-06
暂时禁止评论













闽公网安备 35020302000061号