梯度下降的实现
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/794bd33b2c5c77b08a491529a34e4345.png)
import numpy as np
import matplotlib.pyplot as plt
import datetime
data = np.array([
[80,200],
[95,230],
[104,245],
[112,247],
[125,259],
[135,262]
])
b=1
learningrate = 0.1
def gradentdecent():
global b
slop=0
for item in data[:,1]:
slop = slop + (b - item)
b=b-learningrate*slop
print("b={},mse的变换率={}".format(b,slop))
starttime = datetime.datetime.now()
for i in range(0,300):
gradentdecent()
endtime = datetime.datetime.now()
c=(endtime-starttime).seconds
print(starttime)
print(endtime)
利用偏导数实现梯度下降
import numpy as np
import matplotlib.pyplot as plt
data = np.array([
[80,200],
[95,230],
[104,245],
[112,247],
[125,259],
[135,262]
])
m=1
b=1
learningrate = 0.00001
def gradentdecent():
global m,b
bslop=0
mslop=0
mse=0
for index,item in enumerate(data[:,1]):
bslop = bslop + (m*data[:,0][index]+b-item)
mslop = mslop + (m*data[:,0][index]+b-item)*data[:,0][index]
mse = mse +(m+data[:,0][index]+b-item)**2
m = m-learningrate*mslop
b = b-learningrate*bslop
return m,b,mslop,bslop,mse
for i in range(10000000):
m, b, mslop, bslop, mse = gradentdecent()
if(i%100000==0):
print("m={},b={},mslop={},bslop={},mse={}".format(m, b, mslop, bslop, mse))
利用sklearn
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
model = LinearRegression()
feature = np.array([80,95,104,112,125,135])
label = np.array([200,230,245,247,259,262])
model.fit(feature.reshape(-1,1),label)
print(model.coef_)
print(model.intercept_)
model.predict(np.array([[90]]))
多项式回归预测天猫双十一销售额