Path: blob/main/C1 - Supervised Machine Learning - Regression and Classification/week1/Optional Labs/lab_utils_common.py
2201 views
"""1lab_utils_common.py2functions common to all optional labs, Course 1, Week 23"""45import numpy as np6import matplotlib.pyplot as plt78plt.style.use('./deeplearning.mplstyle')9dlblue = '#0096ff'; dlorange = '#FF9300'; dldarkred='#C00000'; dlmagenta='#FF40FF'; dlpurple='#7030A0';10dlcolors = [dlblue, dlorange, dldarkred, dlmagenta, dlpurple]11dlc = dict(dlblue = '#0096ff', dlorange = '#FF9300', dldarkred='#C00000', dlmagenta='#FF40FF', dlpurple='#7030A0')121314##########################################################15# Regression Routines16##########################################################1718#Function to calculate the cost19def compute_cost_matrix(X, y, w, b, verbose=False):20"""21Computes the gradient for linear regression22Args:23X (ndarray (m,n)): Data, m examples with n features24y (ndarray (m,)) : target values25w (ndarray (n,)) : model parameters26b (scalar) : model parameter27verbose : (Boolean) If true, print out intermediate value f_wb28Returns29cost: (scalar)30"""31m = X.shape[0]3233# calculate f_wb for all examples.34f_wb = X @ w + b35# calculate cost36total_cost = (1/(2*m)) * np.sum((f_wb-y)**2)3738if verbose: print("f_wb:")39if verbose: print(f_wb)4041return total_cost4243def compute_gradient_matrix(X, y, w, b):44"""45Computes the gradient for linear regression4647Args:48X (ndarray (m,n)): Data, m examples with n features49y (ndarray (m,)) : target values50w (ndarray (n,)) : model parameters51b (scalar) : model parameter52Returns53dj_dw (ndarray (n,1)): The gradient of the cost w.r.t. the parameters w.54dj_db (scalar): The gradient of the cost w.r.t. the parameter b.5556"""57m,n = X.shape58f_wb = X @ w + b59e = f_wb - y60dj_dw = (1/m) * (X.T @ e)61dj_db = (1/m) * np.sum(e)6263return dj_db,dj_dw646566# Loop version of multi-variable compute_cost67def compute_cost(X, y, w, b):68"""69compute cost70Args:71X (ndarray (m,n)): Data, m examples with n features72y (ndarray (m,)) : target values73w (ndarray (n,)) : model parameters74b (scalar) : model parameter75Returns76cost (scalar) : cost77"""78m = X.shape[0]79cost = 0.080for i in range(m):81f_wb_i = np.dot(X[i],w) + b #(n,)(n,)=scalar82cost = cost + (f_wb_i - y[i])**283cost = cost/(2*m)84return cost8586def compute_gradient(X, y, w, b):87"""88Computes the gradient for linear regression89Args:90X (ndarray (m,n)): Data, m examples with n features91y (ndarray (m,)) : target values92w (ndarray (n,)) : model parameters93b (scalar) : model parameter94Returns95dj_dw (ndarray Shape (n,)): The gradient of the cost w.r.t. the parameters w.96dj_db (scalar): The gradient of the cost w.r.t. the parameter b.97"""98m,n = X.shape #(number of examples, number of features)99dj_dw = np.zeros((n,))100dj_db = 0.101102for i in range(m):103err = (np.dot(X[i], w) + b) - y[i]104for j in range(n):105dj_dw[j] = dj_dw[j] + err * X[i,j]106dj_db = dj_db + err107dj_dw = dj_dw/m108dj_db = dj_db/m109110return dj_db,dj_dw111112113114