deflinearModelLossRSS(b,X,y):# Make predictionspredY=linearModelPredict(b,X)# Compute residualsres=y-predY# Compute the residual sum of squaresresidual_sum_of_squares=sum(res**2)# Compute the gradient of the lossgradient=-2*np.dot(res,X)return(residual_sum_of_squares,gradient)
importscipy.optimizeassodeflinearModelFit(X,y,lossfcn):nrows,ncols=X.shapebetas=np.zeros((ncols,1))# Optimize the lossRES=so.minimize(lossfcn,betas,args=(X,y),jac=True,# hess = 2 # isn't it just 2)# Obtain estimates from the optimizerestimated_betas=RES.x# Compute goodness of fitres=y-np.mean(y)TSS=sum(res**2)RSS,deriv=linearModelLossRSS(estimated_betas,X,y)# L2 loss and RSS are the same thingR2=1-RSS/TSSreturn(estimated_betas,R2)