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 compute_cost 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
fub(x(i))=wx(i)+b
cost(0)=(fub-y(0))2
Return the total cost over all examples
J(w,b)=12mi=0m-1cost(i)
Here, m 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: compute_cost
def compute_cost(x,y,w, 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
total_cost (float): The cost of using w,b as the parameters for linear regression
to fit the data points in }x\mathrm{ and }
"n"m = x.shape [0]total_cost =0for i in range(m):
f_wb=w*x[i]+b
total_cost +=(f_wb - y[i])**2
total_cost =(1/(2 total_costGRADED FUNCTION: compute_cost
def compute_cost(x,y, 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
total_cost (float): The cost of using w,b as the parameters for linear regression
to fit the data points in }x\mathrm{ and y
m*nm = x.shape[0]total_cost =0for i in range(m):
f wb = w* x[i]+ b
total_cost +=(f_wb - y[i])***2
total cost =(1/(2 total_costreturn total_costreturn total_costClick for hints
You can check if your implementation was correct by running the following test code:initial_w =2
initial_b ==
cost = compute_cost(x_train, y_train, initial_w, initial_b)
print(type(cost))
print(f'Cost at initial w: {cost:.3f}')from public_tests import *
compute_cost_test(compute_cost)Expected Output:
Complete the compute _ cost below to: Iterate

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!