Question: Complete the compute _ cost below to: Iterate over the training examples, and for each example, compute: The prediction of the model for that example
Complete the computecost below to:
Iterate over the training examples, and for each example, compute:
The prediction of the model for that example
The cost for that example
Return the total cost over all examples
Here, is the number of training examples and is the summation operator
If you get stuck, you can check out the hints presented after the cell below to help you with the implementation.GRADED FUNCTION: computecost
def computecostxyw b:
Computes the cost function for linear regression,
Args:
x ndarray: Shape m Input to the model Population of cities
y ndarray: Shape m Label Actual profits for the cities
w b scalar: Parameters of the model
Returns
totalcost float: The cost of using wb as the parameters for linear regression
to fit the data points in xmathrm and
nm xshape totalcost for i in rangem:
fwbwxib
totalcost fwb yi
totalcost totalcostGRADED FUNCTION: computecost
def computecostxy w b:
m
Computes the cost function for linear regression.
Args:
x ndarray: Shape m Input to the model Population of cities
y ndarray: Shape m Label Actual profits for the cities
w b scalar: Parameters of the model
Returns
totalcost float: The cost of using wb as the parameters for linear regression
to fit the data points in xmathrm and y
mnm xshapetotalcost for i in rangem:
f wb w xi b
totalcost fwb yi
total cost totalcostreturn totalcostreturn totalcostClick for hints
You can check if your implementation was correct by running the following test code:initialw
initialb
cost computecostxtrain, ytrain, initialw initialb
printtypecost
printfCost at initial w: cost:ffrom publictests import
computecosttestcomputecostExpected Output:
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
