批量梯度下降算法实现
import numpy as np
# 设置数据集的行数和维数
r = 20
c = 10
# 生成数据集合label
x = np.random.randint(0, 10, r * c).reshape(r, c) # 20行,10列
x = np.hstack((x, np.ones(r).reshape(r, 1))) # 20行,11列
y = np.random.randint(0, 1, r).reshape(r, 1) # 20行,1列
# 代价函数
def cost(x, y, theta, m):
diff = np.dot(x, theta) - y
return (1 / (2 * m)) * np.dot(np.transpose(diff), diff)
# 梯度函数
def grad(x, y, theta, m):
diff = np.dot(x, theta) - y
return 1 / m * np.dot(np.transpose(x), diff) # 结果是一列
def gradient_descent(x, y, num, error):
m = x.shape[0] # 20行
theta = np.random.randint(0, 10, c + 1).reshape(c + 1, 1) # 20行一列
time = 0
while cost(x, y, theta, m) > error and time < num:
theta = theta - 0.001 * grad(x, y, theta, m)
time += 1
print(cost(x, y, theta, m))
return theta
ans = gradient_descent(x, y, 10000, 0.01)
print(ans)
随机梯度下降算法实现
import numpy as np
import time
# 设置数据集的行数和维数
r = 20
c = 10
# 生成数据集合label
x = np.random.randint(0, 100, r * c).reshape(r, c) # 20行,10列
x = np.hstack((x, np.ones(r).reshape(r, 1))) # 20行,11列
# print(x)
y = np.random.randint(0, 100, r).reshape(r, 1) # 20行,1列
# print(y)
# 损失函数
def cost(x, y, theta, m):
diff = np.dot(x, theta) - y
return (1 / (2 * m)) * np.dot(np.transpose(diff), diff)
# 梯度函数,给样本(xi,yi)求第j个theta的偏导数
def grad(xi, yi, theta, j, m): # 明确选定的theta的第几个值
return 1. / m *(np.dot(xi, theta) - yi) * xi[j]
def gradient_descent(x, y, num, error):
m = x.shape[0] # 20行
theta = np.random.random(c + 1).reshape(c + 1, 1) # 这里不能用随机整数,无法更新
time = 0
while cost(x, y, theta, m) > error and time < num:
index = np.random.randint(m)
xi = x[index]
yi = y[index]
for i in range(11): # 依次更新theta
gr = grad(xi, yi, theta, i , 20)
theta[i] = theta[i] - 0.0001*gr
time += 1
print(cost(x, y, theta, m))
return theta
ans = gradient_descent(x, y, 1000, 0.01)
print(np.dot(x, ans) - y)
print(ans)