代码1.
import numpy as np
import matplotlib.pyplot as plt
#init some data
x_data = np.arange(-10,11).reshape([21,1])
y_data = np.square(x_data) * 3 + x_data * 4 + 5
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.plot(x_data,y_data,lw = 3)
plt.ion()
plt.show()
start_x = 10
step = 0.02
current_x = start_x
current_y = 3 * current_x * current_x + 4 * current_x + 5
print ("(loop_count,current_x,current_y)")
for i in range(10):
print (i,current_x,current_y)
der_f_x = 6 * current_x + 4
current_x = current_x - step * der_f_x
current_y = 3 * current_x * current_x + 4 * current_x + 5
ax.scatter(current_x,current_y)
plt.pause(0.8)
代码2 梯度下降+线性回归
import numpy as np
import matplotlib.pyplot as plt
# init some data
x_data = np.arange(1, 21).reshape([20, 1])
y_data = x_data*3 + 5
# for polt picture
fig = plt.figure()
ax = fig.add_subplot(1,1,1)
ax.scatter(x_data, y_data)
plt.ion()
plt.show()
w1 = 0
b1 = 0
step = 0.001
#损失函数: 预测值和实际值的平方和
def cost_function(y_prediction):
return 1.0/(2 * 20) * np.sum(np.square(y_prediction - y_data))
y_prediction = x_data * w1 + b1
ax.plot(x_data, y_prediction, 'black', lw=3)
m = 20
print("(i, cost_function)")
for i in range(250):
print(i, cost_function(y_prediction))
#记:梯度下降中的权重w和偏执b公式
derivative_f_w1 = 1.0/m * np.sum(np.multiply(y_prediction - y_data, x_data))
derivative_f_b1 = 1.0/m * np.sum(y_prediction - y_data)
#更新权重w和偏执b
w1 = w1 - step * derivative_f_w1
b1 = b1 - step * derivative_f_b1
y_prediction = x_data * w1 + b1
# try:
# ax.lines.remove(lines[0])
# except Exception:
# pass
lines = ax.plot(x_data, y_prediction, 'b-', lw=3)
plt.pause(0.7)
print('w1:', w1, 'b1:', b1)
代码3 梯度下降+线性回归
import pandas as pd
pga=pd.read_csv('./data/pga.csv')
pga.head()
# Data preprocessing
# Normalize the data
# 1.Mean_Std
pga['distance']=(pga['distance']-pga['distance'].mean())/pga['distance'].std()
pga['accuracy']=(pga['accuracy']-pga['accuracy'].mean())/pga['accuracy'].std()
# 2. Min_Max
#pga['distance']=(pga['distance']-pga['distance'].min())/pga['distance'].max()
#pga['accuracy']=(pga['accuracy']-pga['accuracy'].min())/pga['accuracy'].max()
import matplotlib.pyplot as plt
plt.scatter(pga['distance'],pga['accuracy'])
plt.xlabel('normalized distance')
plt.ylabel('normalized accuracy')
plt.show()
# Use linear model to model this data.
from sklearn.linear_model import LinearRegression
import numpy as np
lr=LinearRegression()
lr.fit(pga.distance[:,np.newaxis],pga['accuracy'])
#也可以这样写
#lr.fit(pga[['distance']],pga['accuracy'])
theta0=lr.intercept_
theta1=lr.coef_
print(theta0)
print(theta1)
#calculating cost-function for each theta1
#计算平均累积误差
def cost(x,y,theta0,theta1):
J=0
for i in range(len(x)):
mse=(x[i]*theta1+theta0-y[i])**2
J+=mse
return J/(2*len(x))
theta0=100
theta1s = np.linspace(-3,2,100)
costs=[]
for theta1 in theta1s:
costs.append(cost(pga['distance'],pga['accuracy'],theta0,theta1))
plt.plot(theta1s,costs)
plt.show()
print(pga.distance)
#调整theta
def partial_cost_theta0(x,y,theta0,theta1):
#我们的模型时线性拟合函数:y=theta1*x + theta0,而不是sigmoid函数,当非线性时我们可以用sigmoid
h=theta1*x+theta0
diff=(h-y)
partial=diff.sum()/len(diff)
return partial
partial0=partial_cost_theta0(pga.distance,pga.accuracy,1,1)
def partial_cost_theta1(x,y,theta0,theta1):
h=theta1*x+theta0#我们的模型时线性拟合函数:y=theta1*x + theta0,而不是sigmoid函数,当非线性时我们可以用sigmoid
diff=(h-y)*x
partial=diff.sum()/len(diff)
return partial
partial1=partial_cost_theta1(pga.distance,pga.accuracy,0,5)
print(partial0)
print(partial1)
# In[52]:
def gradient_descent(x,y,alpha=0.1,theta0=0,theta1=0): #设置默认参数
#计算成本
#调整权值
#计算错误代价,判断是否收敛或者达到最大迭代次数
most_iterations=1000
convergence_thres=0.000001
c=cost(x,y,theta0,theta1)
costs=[c]
cost_pre=c+convergence_thres+1.0
counter=0
while( (np.abs(c-cost_pre)>convergence_thres) & (counter<most_iterations) ):
update0=alpha*partial_cost_theta0(x,y,theta0,theta1)
update1=alpha*partial_cost_theta1(x,y,theta0,theta1)
theta0-=update0
theta1-=update1
cost_pre=c
c=cost(x,y,theta0,theta1)
costs.append(c)
counter+=1
return {'theta0': theta0, 'theta1': theta1, "costs": costs}
print("Theta1 =", gradient_descent(pga.distance, pga.accuracy)['theta1'])
costs=gradient_descent(pga.distance,pga.accuracy,alpha=.01)['costs']
print(gradient_descent(pga.distance, pga.accuracy,alpha=.01)['theta1'])
plt.scatter(range(len(costs)),costs)
plt.show()
# result=gradient_descent(pga.distance, pga.accuracy,alpha=.01)
# print("Theta0 =", result['theta0'])
# print("Theta1 =", result['theta1'])
# costs=result['costs']
# plt.scatter(range(len(costs)),costs)
# plt.show()
- 数据集地址:
- Gradient-Descent-Algorithm-master
链接:https://pan.baidu.com/s/19UO_sD9dn0_1DTM27MgzCw
提取码:4rqp
参考 : 机器学习实战之梯度下降算法: https://www.imooc.com/article/39831
点击查看更多内容
为 TA 点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦