一元回归
案例
求解
多元线性回归
梯度下降法
梯度下降
一元
# y = theta0+theta1*x
X = [4, 8, 5, 10, 12]
y = [20, 50, 30, 70, 60]
# 初始化参数
theta0 = theta1 = 0
# 学习率
alpha = 0.001
# 迭代次数
cnt = 0
# 误差
error0 = error1 = 0
# 指定一个阈值,用于检查两次误差的差,以便停止迭代
threshold = 0.000001
while True:
# 梯度,diff[0]是theta0的梯度,diff[1]是theta1的梯度
diff = [0, 0]
m = len(X)
for i in range(m):
diff[0] += y[i] - (theta0 + theta1 * X[i])
diff[1] += (y[i] - (theta0 + theta1 * X[i])) * X[i]
theta0 = theta0 + alpha/m * diff[0]
theta1 = theta1 + alpha/m * diff[1]
# 计算误差
for i in range(m):
error1 += (y[i] - (theta0 + theta1 * X[i])) ** 2
error1 /= m
if abs(error1 - error0) < threshold:
break
else:
error0 = error1
cnt += 1
print(theta0, theta1, cnt)
def predict(theta0, theta1, x_test):
return theta0 + theta1 * x_test
print(predict(theta0, theta1, 15))
多元
# y = theta0 * x0 + theta1 * x1 + theta2 * x2
x_train = [[1,0.,3],[1,1.,3],[1,2.,3],[1,3.,2],[1,4.,4]]
y_train = [95.364,97.217205,75.195834,60.105519,49.342380]
theta0,theta1,theta2 = 0,0,0
alpha = 0.0001
cnt = 0
error0,error1 = 0,0
threshold = 0.00001
m = len(y_train)
while True:
diff = [0,0,0]
for i in range(m):
diff[0] += (y_train[i] - (theta0 + theta1 * x_train[i][1] + theta2 * x_train[i][2])) * x_train[i][0]
diff[1] += (y_train[i] - (theta0 + theta1 * x_train[i][1] + theta2 * x_train[i][2])) * x_train[i][1]
diff[2] += (y_train[i] - (theta0 + theta1 * x_train[i][1] + theta2 * x_train[i][2])) * x_train[i][2]
theta0 = theta0 + alpha / m * diff[0]
theta1 = theta1 + alpha / m * diff[1]
theta2 = theta2 + alpha / m * diff[2]
for i in range(m):
error1 += (y_train[i] - theta0 + theta1 * x_train[i][1] + theta2 * x_train[i][2])**2
if abs(error1 - error0) < threshold:
break
else:
error0 = error1
cnt += 1
print(theta0,theta1,theta2,cnt)
随机梯度下降
# y = theta0+theta1*x
X = [4, 8, 5, 10, 12]
y = [20, 50, 30, 70, 60]
# 初始化参数
theta0 = theta1 = 0
# 学习率
alpha = 0.001
# 迭代次数
cnt = 0
# 误差
error0 = error1 = 0
# 指定一个阈值,用于检查两次误差的差,以便停止迭代
threshold = 0.000001
while True:
# 梯度,diff[0]是theta0的梯度,diff[1]是theta1的梯度
diff = [0, 0]
m = len(X)
for i in range(m):
diff[0] = y[i] - (theta0 + theta1 * X[i])
diff[1] = (y[i] - (theta0 + theta1 * X[i])) * X[i]
theta0 = theta0 + alpha * diff[0]
theta1 = theta1 + alpha * diff[1]
# 计算误差
for i in range(m):
error1 += (y[i] - (theta0 + theta1 * X[i])) ** 2
error1 /= m
if abs(error1 - error0) < threshold:
break
else:
error0 = error1
cnt += 1
print(theta0, theta1, cnt)
def predict(theta0, theta1, x_test):
return theta0 + theta1 * x_test
print(predict(theta0, theta1, 15))
小批量梯度下降
总结
sklearn线性回归
from sklearn.linear_model import LinearRegression
X = [[4], [8], [5], [10], [12]]
y = [20, 50, 30, 70, 60]
model = LinearRegression()
model.fit(X,y)
print(model.coef_)
print(model.intercept_)
print(model.score(X,y))
from sklearn.linear_model.stochastic_gradient import SGDRegressor
X = [[4], [8], [5], [10], [12]]
y = [20, 50, 30, 70, 60]
model = SGDRegressor(max_iter=10000)
model.fit(X,y)
print(model.coef_)
print(model.intercept_)
print(model.score(X,y))