Gradient descent for linear regression
import math, copy import numpy as np import matplotlib.pyplot as plt plt.style.use('./deeplearning.mplstyle') from lab_utils_uni import plt_house_x, plt_contour_wgrad, plt_divergence, plt_gradients # Load our data set x_train = np.array([1.0, 2.0]) #features y_train = np.array([300.0, 500.0]) #target value #Function to calculate the cost def compute_cost(x, y, w, b): m = x.shape[0] cost = 0 for i in range(m): f_wb = w * x[i] + b cost = cost + (f_wb - y[i])**2 total_cost = 1 / (2 * m) * cost return total_cost def compute_gradient(x, y, w, b): """ Computes the gradient for linear regression Args: x (ndarray (m,)): Data, m examples y (ndarray (m,)): target values w,b (scalar)...


Yorumlar
Yorum Gönder