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



# 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[

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

1029

被折叠的 条评论
为什么被折叠?



