这个系列的公开课看了有段时间了,原来也想着一直记录下去,还是懒病的问题;中间停了这么长时间,最近又想着复习一下,就写点随笔。最前方关于Ng介绍机器学习的相关概念部分的几张截图,我用了张雨石大佬公开课笔记的截图(实在是懒的不想打字了),在这里我要感谢张雨石大佬!话不多说,直接进入正题!
附录证明
代码实现
批量梯度下降算法:
import matplotlib.pyplot as plt
import csv
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
# 从文件中取出x1,x2,y
file_name = "practise.csv"
with open(file_name) as f:
reader = csv.reader(f)
header_row = next(reader)
print(header_row)
x1 = []
x2 = []
y = []
for row in reader:
x1.append(int(row[1]))
x2.append(int(row[2]))
y.append(int(row[3]))
# 定义学习率
learning_rate = 0.00001
# 计算样本个数
m = len(y)
# 初始化参数theta
theta = [0]*3
print(theta)
# 误差
error0 = 0
error1 = 0
# 退出迭代的条件
epsilon = 0.0001
def h(x1,x2):
return theta[0]+theta[1]*x1+theta[2]*x2
# 开始迭代
cnt = 0
while True:
cnt += 1
diff = [0]*3
for i in range(m):
diff[0] += h(x1[i], x2[i]) - y[i]
diff[1] += (h(x1[i], x2[i]) - y[i])*x1[i]
diff[2] += (h(x1[i], x2[i]) - y[i])*x2[i]
theta[0] -= learning_rate /m * diff[0]
theta[1] -= learning_rate /m * diff[1]
theta[2] -= learning_rate /m * diff[2]
error1 = 0
for i in range(m):
error1 += (h(x1[i], x2[i])-y[i])**2
#print("error1: %d" %(error1))
if abs(error1 - error0)<epsilon:
print("cnt: %d" % cnt)
break
else:
error0 = error1
print(theta)
y_cal = []
for i in range(m):
y_cal.append(theta[0]+theta[1]*x1[i]+theta[2]*x2[i])
print(y)
print(y_cal)
使用正规方程组法求解参数:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# 房屋大小(13组数据)
x = [[100,74], [80,98], [120,50], [75,32], [60,22], [43,21], [140,77], [132,61], [63,30], [55,25], [74,37],
[44,20], [88,44]]
# 房屋价格
y = [[120], [92], [143], [87], [60], [50], [167], [147], [80], [60], [90], [57], [99]]
# 使用最小二乘法求解误差函数最优解
def least_square_regress(x,y):
# 在每组输入特征前面插入X0 = 1
for xs in x:
xs.insert(0, 1)
# 将原始数据转换为矩阵形式
x_array = np.array(x)
y_array = np.array(y)
x_mat = np.mat(x_array)
y_mat = np.mat(y_array)
print(x_mat.shape)
xT_x = np.matmul(x_mat.T, x_mat)
print(xT_x)
if(np.linalg.det(xT_x == 0)):
return ("无法求解")
para = xT_x.I * x_mat.T * y_mat
return para
# 得到线性回归模型的参数值,即输入特征前面的系数
para = least_square_regress(x,y)
print(para)
x_array = np.array(x)
x_mat = np.mat(x_array)
result = x_mat * para
print(result.T)
print(y)