单变量线性回归
Python代码:
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# cost function
def cost_function(X, Y, theta):
hyposis = np.dot(np.transpose(theta), X) - np.transpose(Y)
cost = np.dot(hyposis, hyposis) / (2 * np.size(Y))
return cost
# gradient descend
def gradient_descend(X, Y, theta, alpha, iter_num):
m = Y.shape[0]
for num in range(0, iter_num):
hyp = np.dot(np.transpose(theta), X)
theta = theta - alpha * np.dot((hyp - Y), np.transpose(X)) / m
return theta
# load the data
filepath = r'ex1data1.txt'
dataset = np.loadtxt(filepath, delimiter=',', usecols=(0, 1))
input = dataset[:, 0]
output = dataset[:, 1]
# scatter plot
plt.plot(input, output, 'r+')
X = np.c_[np.ones(np.size(output)), input]
X = np.transpose(X)
Y = output
theta = np.transpose(np.array([0, 0]))
alpha = 0.01
iter_num = 10000
theta = gradient_descend(X, Y, theta, alpha, iter_num)
# fitted curve
plt.plot(np.transpose(input), np.dot(np.transpose(theta), X), 'b-')
plt.show()
多变量线性回归
Python代码
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
# normalization
def normalization(data):
mean = np.mean(data)
std = np.std(data)
return (data - mean)/std
# cost function
def cost_function(X, Y, theta):
hyposis = np.dot(np.transpose(theta), X) - np.transpose(Y)
cost = np.dot(hyposis, hyposis) / (2 * np.size(Y))
return cost
# gradient descend
def gradient_descend(X, Y, theta, alpha, iter_num):
m = Y.shape[0]
for num in range(0, iter_num):
hyp = np.dot(np.transpose(theta), X)
theta = theta - alpha * np.dot((hyp - Y), np.transpose(X)) / m
return theta
# load the data
filepath = r'ex1data2.txt'
dataset = np.loadtxt(filepath, delimiter=',', usecols=(0, 1))
input = dataset[:, 0:2]
output = dataset[:, 1]
input = normalization(input)
output = normalization(output)
X = np.c_[np.ones(np.size(output)), input]
X = np.transpose(X)
Y = output
theta = np.transpose(np.array([0, 0, 0]))
alpha = 0.01
iter_num = 10000
theta = gradient_descend(X, Y, theta, alpha, iter_num)
print(theta)