import numpy as np
import matplotlib.pyplot as plt
# 输入数据
x_ori = np.array([0.95, 3, 4, 5.07, 6.03, 8.21, 8.85, 12.02, 15])
y = np.array([5.1, 8.7, 11.5, 13, 15.3, 18, 21, 26.87, 32.5])
# 代价函数
def cost_function(theta, x, y):
m = len(y)
J = np.sum((np.dot(x, theta) - y) ** 2) / (2 * m)
return J
# 初始化theta
theta = np.zeros(2)
# 增加截距项
x = np.c_[np.ones(x_ori.shape[0]), x_ori]
# 梯度下降算法
alpha = 0.01
iterations = 1500
m = len(y)
for i in range(iterations):
theta = theta - (alpha / m) * np.dot(x.T, (np.dot(x, theta) - y))
J = cost_function(theta, x, y)
if i % 100 == 0:
print("Iteration %d | Cost: %f" % (i, J))
print("斜率:", theta[1])
print("截距:", theta[0])
# 计算预测的y值
y_pred = theta[1]*x_ori + theta[0]
# 绘制结果图形
plt.scatter(x_ori, y)
plt.plot(x_ori, y_pred, color='r')
plt.title("Linear Regression")
plt.xlabel("x")
plt.ylabel("y")
plt.show()
运行结果: