import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
x = np.array([4,3,3,4,2,2,0,1,2,5,1,2,5,1,3])
y = np.array([8,6,6,7,4,4,2,4,5,9,3,4,8,3,6])
m = len(x)
x = np.c_[np.ones([m,1]),x] #增加一列1
y = np.c_[y]
print(x.shape)#(15, 2)
print(y.shape)#(15, 1)
theta = np.zeros([2,1])
#theta 初始化
alpha = 0.01 #初始化学习率alpha
m_iter = 100 #定义最大迭代次数
cost = np.zeros([m_iter]) #记录代价值
for i in range(m_iter):
y_hat = x.dot(theta)
error = y_hat - y
cost_value = 1/2*m * error.T.dot(error)
cost[i] = cost_value
delt_theta = 1/m *x.T.dot(error)
theta = theta - alpha*delt_theta
print(theta) #梯度下降求得最优参数值
array([[ 0.80067129],
[ 1.66911044]])
#进行绘图显示
plt.scatter(x[:,1],y,c ='r')
plt.plot(x[:,1],y_hat,'r-')
plt.show()
plt.plot(cost)
plt.show()
x:
array([[ 1., 4.],
[ 1., 3.],
[ 1., 3.],
[ 1., 4.],
[ 1., 2.],
[ 1., 2.],
[ 1., 0.],
[ 1., 1.],
[ 1., 2.],
[ 1., 5.],
[ 1., 1.],
[ 1., 2.],
[ 1., 5.],
[ 1., 1.],
[ 1., 3.]])
y:
array([[8],
[6],
[6],
[7],
[4],
[4],
[2],
[4],
[5],
[9],
[3],
[4],
[8],
[3],
[6]])