常用语:
gradient descent 梯度下降
Closed Form Solution 近似解
Noise 噪声(误差)
Linear Regression 线性回归(一般只要预测值是连续的,都叫回归问题,比如年龄预测)
Logistic Regression 逻辑回归(用于预测属于某类的概率,线性回归+激活函数=逻辑回归)
示例:求解一个简单的二元一次方程 y = w * x + b
这个问题也就是给出n个数据 (x,y) ,求出 w 和 b 的最优值
也就是求出 loss = ( WX+b - y )^2 的最小值
具体代码:
import numpy as np
# loss function 损失函数 y = wx + b
def compute_error_for_line_given_points(b, w, points):
totalError = 0 # points 是x 、y 的组合
for i in range(0, len(points)):
x = points[i, 0] # 取得这个点的 x 的值
y = points[i, 1] # 取得这个点的 y 的值
totalError += (y - (w * x + b)) ** 2 # 预测值与真实值的 平方和 累加
return totalError / float(len(points)) # 求 average
# 梯度信息
def step_gradient(b_current, w_current, points, learningRate):
b_gradient = 0
w_gradient = 0
N = float(len(points)) # points 的总数目 N
for i in range(0, len(points)):
x = points[i, 0] # 取得这个点的 x 的值
y = points[i, 1] # 取得这个点的 y 的值
b_gradient += -(2 / N) * (y - ((w_current * x) + b_current)) # b 更新之后的梯度
w_gradient += -(2 / N) * x * (y - ((w_current * x) + b_current)) # w 更新之后的梯度
new_b = b_current - (learningRate * b_gradient) # 更新之后的值
new_w = w_current - (learningRate * w_gradient)
return [new_b, new_w]
# 循环迭代梯度信息
def gradient_descent_runner(points, current_b, current_w, # iterations表示迭代次数
learningRate, num_iterations):
b = current_b
w = current_w
for i in range(num_iterations):
[b, w] = step_gradient(b, w, np.array(points), learningRate) # np.array(points) 表示送进来的 x 和 y 的数组
return [b, w] # 返回最后一组的 b 和 w 就是最后得到的结果
# 执行函数
def run():
points = np.genfromtxt("E:\\untitled\\project1\\data.csv", delimiter=",")
learningRate = 0.0001 # 学习率
initial_b = 0 # b的初始值
initial_w = 0 # w的初始值
num_iterations = 1000 # 迭代次数
print("Staring gradient descent at b = {0} , w = {1}, error = {2} "
.format(initial_b, initial_w,
compute_error_for_line_given_points(initial_b, initial_w, points))
)
print("Running.....")
[b, w] = gradient_descent_runner(points, initial_b, initial_w, learningRate, num_iterations)
print("After {0} iterations b = {1}, w = {2}, error = {3}"
.format(num_iterations, b, w,
compute_error_for_line_given_points(b, w, points))
)
if __name__ == '__main__': # 主函数
run()
运行结果:
Staring gradient descent at b = 0 , w = 0, error = 2867.84536760405
Running…
After 1000 iterations b = -0.2618055223815977, w = 1.2088373733071605, error = 36.724168328835304