梯度下降法--多元线性回归

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值