梯度下降法与单变量、多变量线性回归

这篇博客探讨了梯度下降法在单变量和多变量线性回归中的应用。首先讲解了单变量线性回归如何利用最小化代价函数进行房价预测,接着深入到多变量线性回归的理论和实践。

梯度下降法求解线性回归

单变量线性回归

这一次首先从单变量线性回归开始,仍以房价预测作为例子。首先回顾理论基础——最小化代价函数,如下。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

# Author: Daniel Geng
# In this code, let's change the method to Gradient descent. We start from single feature.
import csv
import numpy as np
import matplotlib.pyplot as pl
import random

# Define function to calculate gradient, return new theta vector, where Theta, X are numpy arrays.
# alpha is to control the learning rate
def GradientDescent(Theta, X, Y, m, n, alpha):
    tempTheta = np.zeros((1, n)).reshape(n, )
    for i in range(n):
        error = 0.0
        gradient = 0.0
        for j in range(m):
            error = np.dot(Theta, np.transpose(X[j])) - Y[j]
            gradient += error * X[j][i]

        gradient *= alpha / m
        tempTheta[i] = gradient # temp record new theta value

    # update theta value one time
    for i in range(n):
        Theta[i] -= tempTheta[i]
    print('Theta = ', Theta)
    return Theta


# Define function to calculate J(theta0, theta1)
def CostFunc(Theta, X, Y, m):
    error = 0.0
    cost = 0.0

    for i in range(m):
        error += (np.dot(Theta, np.transpose(X[i])) - Y[i]) ** 2

    cost = error * (1 / (2 * m))
    print('cost = ', cost)

    return cost


#Get data
def ImportData(filePath):
    X = []  # 2D arrays [[1, x11, x12, ...], [], ...[]], each item is a [] which contains columns of each row
    Y = []  # 1D vecotr Stands for vector of prices, Y = X * theta
    f = open(filePath)
    r = csv.reader(f, delimiter=',')
    r.__next__()  # Skip header row

    for row in r:
        rowX = [1.0]
        rowX.append(float(row[2])) # only fetch lotsize
        X.append(rowX)
        Y.append(float(row[1]))

    # random the order of raw data
    for i, j in zip(X, Y):
        i.append(j)
    random.shuffle(X)
    Y = []     # clear Y, get Y item from X
    for i in X:
        Y.append(i[
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值