def compute_cost(x, y, w, b):m = x.shape[0] cost = 0for i in range(m):f_wb = w * x[i] + bcost = cost + (f_wb - y[i])**2total_cost = 1 / (2 * m) * costreturn total_cost
def compute_gradient(x, y, w, b): m = x.shape[0] dj_dw = 0dj_db = 0for i in range(m): f_wb = w * x[i] + b dj_dw_i = (f_wb - y[i]) * x[i] dj_db_i = f_wb - y[i] dj_db += dj_db_idj_dw += dj_dw_i dj_dw = dj_dw / m dj_db = dj_db / m return dj_dw, dj_db
def gradient_descent(x, y, w_in, b_in, alpha, num_iters, cost_function, gradient_function): '''x:输入向量,numpy.ndarrayy:输出向量,numpy.ndarrayw_in:初始wb_in:初始balpha:学习率num_iters:迭代次数cost_function:代价函数gradient_function:计算梯度函数'''J_history = [] p_history = [] b = b_inw = w_infor i in range(num_iters):dj_dw, dj_db = gradient_function(x, y, w , b) b = b - alpha * dj_db w = w - alpha * dj_dw J_history.append( cost_function(x, y, w , b))p_history.append([w,b])if i% math.ceil(num_iters/10) == 0:print(f"Iteration {i}: Cost {J_history[-1]} ",f"dj_dw: {dj_dw}, dj_db: {dj_db} ",f"w: {w}, b:{b}")print{f'final w:{w},b:{b}'}y_hat=w*x+b for i in range(x.shape[0]):print(f'target value:{y[i]},predicted value:{y_hat[i]},error:{y[i]-y_hat[i]}')return w, b, J_history, p_history