C1 W1 Lab03 Cost Function Soln
C1 W1 Lab03 Cost Function Soln
Goals
In this lab you will:
• you will implement and explore the cost function for linear regression with one variable.
Tools
In this lab we will make use of:
Problem Statement
You would like a model which can predict housing prices given the size of the house.
Let's use the same two data points as before the previous lab- a house with 1000 square feet
sold for \$300,000 and a house with 2000 square feet sold for \$500,000.
Computing Cost
The term 'cost' in this assignment might be a little confusing since the data is housing cost.
Here, cost is a measure how well our model is predicting the target price of the house. The term
'price' is used for housing data.
where
f w ,b ( x (i )) =w x (i )+ b
( f w, b ( x (i) ) − y ( i) )
2
• is the squared difference between the target value and the prediction.
• These differences are summed over all the m examples and divided by 2m to produce
the cost, J ( w , b ).
Note, in lecture summation ranges are typically from 1 to m, while code will be from
0 to m-1.
The code below calculates cost by looping over each example. In each loop:
Args:
x (ndarray (m,)): Data, m examples
y (ndarray (m,)): target values
w,b (scalar) : model parameters
Returns
total_cost (float): The cost of using w,b as the parameters
for linear regression
to fit the data points in x and y
"""
# number of training examples
m = x.shape[0]
cost_sum = 0
for i in range(m):
f_wb = w * x[i] + b
cost = (f_wb - y[i]) ** 2
cost_sum = cost_sum + cost
total_cost = (1 / (2 * m)) * cost_sum
return total_cost
Cost Function Intuition
Your goal is to find a model f w ,b ( x )=w x +b , with parameters w ,b , which will accurately predict
house values given an input x . The cost is a measure of how accurate the model is on the training
data.
The cost equation (1) above shows that if w and b can be selected such that the predictions
f w ,b ( x ) match the target data y , the $(f_{w,b}(x^{(i)}) - y^{(i)})^2 $ term will be zero and the cost
minimized. In this simple two point example, you can achieve this!
In the previous lab, you determined that b=100 provided an optimal solution so let's set b to
100 and focus on w .
Below, use the slider control to select the value of w that minimizes cost. It can take a few
seconds for the plot to update.
plt_intuition(x_train,y_train)
{"model_id":"fe8d97d412b849ee884e134677e8823a","version_major":2,"vers
ion_minor":0}
• cost is minimized when w=200 , which matches results from the previous lab
• Because the difference between the target and pediction is squared in the cost equation,
the cost increases rapidly when w is either too large or too small.
• Using the w and b selected by minimizing cost results in a line which is a perfect fit to the
data.
In the contour plot, click on a point to select w and b to achieve the lowest cost. Use the contours
to guide your selections. Note, it can take a few seconds to update the graph.
plt.close('all')
fig, ax, dyn_items = plt_stationary(x_train, y_train)
updater = plt_update_onclick(fig, ax, x_train, y_train, dyn_items)
Above, note the dashed lines in the left plot. These represent the portion of the cost contributed
by each example in your training set. In this case, values of approximately w=209 and b=2.4
provide low cost. Note that, because our training examples are not on a line, the minimum cost
is not zero.
Congratulations!
You have learned the following:
• The cost equation provides a measure of how well your predictions match your training
data.
• Minimizing the cost can provide optimal values of w , b .