import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D #画3d图像
#读入数据、切分数据
data = np.genfromtxt('Delivery.csv',delimiter=',')
x_data = data[:,:-1] #只有最后一列不取
y_data = data[:,-1] #只取最后一列
lr = 0.0001
theta0 = 0
theta1 = 0
theta2 = 0
epochs=1000
#最小二乘法
def compute_error(theta0,theta1,theta2,x_data,y_data):
total_error = 0
for i in range(0,len(x_data)):
total_error += (y_data[i]-(theta0+theta1*x_data[i,0]+theta2*x_data[i,1]))**2
return total_error/float(len(x_data))
#梯度下降
def gradient_descent_runner(x_data,y_data,theta0,theta1,theta2,lr,epochs):
#计算总数据量
m = float(len(x_data))
for i in range(epochs):
theta0_grad = 0
theta1_grad = 0
theta2_grad = 0
#计算梯度的总和再求平均(公式)
&n
梯度下降法--多元线性回归
最新推荐文章于 2024-06-13 17:27:04 发布