假設(shè)房子的面積和價格的對應(yīng)關(guān)系如下圖所示扳抽,那么如何 面積和價格的關(guān)系呢篡帕?
假設(shè) 訓(xùn)練集如下:
面積 : 150 , 200 摔蓝, 250 赂苗, 300, 350贮尉, 400拌滋, 600
價格 : 6450,6450猜谚,8450败砂,9450,11450魏铅,15450昌犹,18450
假設(shè)我們設(shè)定為線性:Y=θ0+θ1x
使用梯度下降的方法求解線性回歸
梯度下降的原理:
image.png
也就是說使用梯度下降尋找代價函數(shù)的最小值的原理就是給J求關(guān)于θ0和θ1的偏導(dǎo)。
手寫Y=θ0+θ1x览芳,梯度下降的θ0斜姥,θ1的迭代過程。
image.png
實驗
假設(shè)存在x=[1,2,3,4,5,6]沧竟,y=[4,7,10,13,16,19] (它們的關(guān)系是y=3x+1)铸敏,然后使用梯度下降求解線性回歸。
x=[1,2,3,4,5,6]
y=[4,7,10,13,16,19]
#θ的參數(shù)設(shè)置悟泵,這個一般是隨機取的
theta0=0.1
theta1=0.2
#α 是學(xué)習(xí)率杈笔,代表的是迭代的步長
alpha=0.01
m=len(x)
def h(i):
return theta0+theta1*x[i]
def diff(i):
return h(i)-y[i]
#times 表示迭代1000次,那么基本上可以下降到梯度的最小值了
#每次迭代做的事情糕非,就是我上面手寫的那個公式蒙具,不停的去修改theta0土榴,theta1
for times in range(1000):
sum1=0
sum2=0
for i in range(m):
sum1=sum1+diff(i)
sum2=sum2+diff(i)*x[i]
theta0=theta0-(alpha/m)*sum1
theta1=theta1-(alpha/m)*sum2
print ("theta0 : ",theta0)
print ("theta1 : ",theta1)
輸出結(jié)果:
2017-06-01 09-18-49屏幕截圖.png
后來想到片效,把每次迭代計算出來的theta0 裁着,theta1 畫出來:
最后的結(jié)果是用*號的線表示出來的.png
源碼:
import numpy as np
import matplotlib.pyplot as plt
x=[1,2,3,4,5,6]
y=[4,7,10,13,16,19]
test_x=[8,9,10,11,12]
#參數(shù)投储,這個是隨機的
theta0=0.1
theta1=0.2
#學(xué)習(xí)率
alpha=0.01
m=len(x)
def h_(x):
return theta0+theta1*x
def h(i):
return theta0+theta1*x[i]
def diff(i):
return h(i)-y[i]
for times in range(1000):
sum1=0
sum2=0
for i in range(m):
sum1=sum1+diff(i)
sum2=sum2+diff(i)*x[i]
theta0=theta0-(alpha/m)*sum1
theta1=theta1-(alpha/m)*sum2
plt.plot(test_x, [h_(xi) for xi in test_x ])
plt.plot(test_x, [h_(xi) for xi in test_x ],'b*')
print ("theta0 : ",theta0)
print ("theta1 : ",theta1)
plt.show()
Part two
剛才的問題是Y=θ0+θ1x,當時問題也有可能是Y=theta0+theta1*x1+theta*x2 融师。這樣的到的代價函數(shù)就是一個和θ0右钾,θ1,θ2有關(guān)的函數(shù)了旱爆。問題可能會稍稍復(fù)雜一點舀射。
代碼:
import numpy as np
import matplotlib.pyplot as plt
#y=2 * (x1) + (x2) + 3
x_train = np.array([ [1, 2], [2, 1], [2, 3], [3, 5], [1, 3], [4, 2], [7, 3], [4, 5], [11, 3], [8, 7] ])
y_train = np.array([7, 8, 10, 14, 8, 13, 20, 16, 28, 26])
x_test = np.array([ [1, 4], [2, 2], [2, 5], [5, 3], [1, 5], [4, 1] ])
alpha = 0.001
theta0=np.random.normal()
theta1=np.random.normal()
theta2=np.random.normal()
m=len(x_train)
def h_(x):
return theta0+theta1*x[0]+theta2*x[1]
def h(i):
return theta0+theta1*x_train[i][0]+theta2*x_train[i][1]
def diff(i):
return h(i)-y_train[i]
for times in range(10000):
sum1=0
sum2=0
sum3=0
for i in range(len(x_train)):
sum1=sum1+diff(i)
sum2=sum2+diff(i)*x_train[i][0]
sum3=sum3+diff(i)*x_train[i][1]
theta0=theta0-(alpha/m)*sum1
theta1=theta1-(alpha/m)*sum2
theta2=theta2-(alpha/m)*sum3
"""
for i in range(len(x_train)):
sum1=sum1+alpha*diff(i)
sum2=sum2+alpha*diff(i)*x_train[i][0]
sum3=sum3+alpha*diff(i)*x_train[i][1]
theta0=theta0-sum1
theta1=theta1-sum2
theta2=theta2-sum3
"""
#plt.plot([x_train],[h_(xi) for xi in x_train ])
plt.plot([h_(xi) for xi in x_train ])
#plt.plot([h_(xi) for xi in x_train ])
print ("theta0 : ",theta0)
print ("theta1 : ",theta1)
print ("theta2 : ",theta2)
plt.show()
實驗結(jié)果:
2017-06-01 19-39-52屏幕截圖.png
2017-06-01 19-40-14屏幕截圖.png