常用方式:
1,解析解
2,梯度下降
一,解析解
1,推到过程:
2,手动代码实现
import numpy as np
import matplotlib.pyplot as plt
seed= np.random.seed(1000)
X=2*np.random.rand(100,1)
# print(X)
# y= c + kx
y= 9+ 3 * X + np.random.randn(100,1)
X_b= np.c_[np.ones((100,1)),X] # 前面补一列值全为1的值 因为前面还有个c=9 对c求导的话就是1 对k求导就是x
# print(X_b)
theta_best= np.linalg.inv(X.T.dot(X)).dot(X.T).dot(y)
# 最大似然估计后,根据高斯分布概率函数
theta_best_b= np.linalg.inv(X_b.T.dot(X_b)).dot(X_b.T).dot(y)
print(theta_best_b)
print(theta_best)
X_new = np.array([[0],[2]])
# print(X_new)
# X_new_b = np.c_[np.ones((2,0)),X_new]
X_new_b = np.array([[1,0],[1,2]])
# print(X_new_b)
y_p = X_new.dot(theta_best)
# print(y_p)
y_p_b = X_new_b.dot(theta_best_b)
# print(y_p_b)
# plt.plot(X_new,y_p,'b-')
plt.plot(X_new_b,y_p_b,'y-')
plt.plot(X,y,'r.')
from sklearn.linear_model import LinearRegression
# 利用sklearn 包去实现
line_reg = LinearRegression()
line_reg.fit(X,y)
print(line_reg.intercept_,line_reg.coef_)
plt.plot(X_new, line_reg.predict(X_new),'b-') #基本和上面一致
plt.show()
二,梯度下降
通俗点讲就是:对多元函数进行求偏导,会得到多个偏导函数,这些偏导函数组成的向量就是梯度。当我们把梯度中的每个偏导函数变为0的时候,就可以找到每个未知数的解了,实际我们要找的就是每个未知数的解。
先看公式:
公式中的 α 叫做学习率
是为了让点沿着梯度方向下降慢慢求得最优解,这个过程我们叫做学习,学习率就是用来限制他每次学习别太过"用功"的,不能步数跨太大
左图是我们希望的,右图就是用功过大。所以给了个学习率,每次的步数不能太大,逐步逼近我们的期望值。
1,公式的推导:
2,手动代码实现:
# 导入必要的库
import numpy as np
import matplotlib.pyplot as plt
# 定义损失函数
def compute_cost(X, y, theta):
m = len(y)
J = 0
predictions = X.dot(theta)
sqrErrors = (predictions - y) ** 2
J = 1 / (2 * m) * np.sum(sqrErrors)
return J
# 梯度下降算法
def gradient_descent(X, y, theta, alpha, num_iters):
m = len(y)
J_history = np.zeros((num_iters, 1))
for i in range(num_iters):
predictions = X.dot(theta)
errors = predictions - y
theta = theta - alpha / m * X.T.dot(errors)
J_history[i] = compute_cost(X, y, theta)
return theta, J_history
# 测试代码
# 随机生成一些数据
x = 2 * np.random.rand(100, 1)
y = 4 + 3 * x + np.random.randn(100, 1)
# 在数据中添加x0=1
X_b = np.c_[np.ones((100, 1)), x]
# 初始化theta
theta = np.random.randn(2, 1)
# 定义迭代次数和学习率
iterations = 100
learning_rate = 0.1
# 运行梯度下降算法
theta, J_history = gradient_descent(X_b, y, theta, learning_rate, iterations)
# 输出最终结果
print("最终参数值:", theta)
print("损失函数值:", J_history[-1])
# 绘制代价函数曲线
plt.plot(J_history)
plt.xlabel("迭代次数")
plt.ylabel("代价函数")
plt.show()
欢迎指正