Path: blob/main/C1 - Supervised Machine Learning - Regression and Classification/week1/Optional Labs/C1_W1_Lab03_Model_Representation_Soln.ipynb
2201 views
Optional Lab: Model Representation

Goals
In this lab you will:
Learn to implement the model for linear regression with one variable
Notation
Here is a summary of some of the notation you will encounter.
|General
Notation | Description
| Python (if applicable) | |: ------------|: ------------------------------------------------------------|| | | scalar, non bold || | | vector, bold || | Regression | | | | | | Training Example feature values (in this lab - Size (1000 sqft)) |
x_train
|
| | Training Example targets (in this lab Price (1000s of dollars)). | y_train
| , | Training Example | x_i
, y_i
| | m | Number of training examples | m
| | | parameter: weight, | w
| | | parameter: bias | b
|
| | The result of the model evaluation at parameterized by : | f_wb
|
Tools
In this lab you will make use of:
NumPy, a popular library for scientific computing
Matplotlib, a popular library for plotting data
Problem Statement
As in the lecture, you will use the motivating example of housing price prediction. This lab will use a simple data set with only two data points - a house with 1000 square feet(sqft) sold for $300,000 and a house with 2000 square feet sold for $500,000. These two points will constitute our data or training set. In this lab, the units of size are 1000 sqft and the units of price are 1000s of dollars.
Size (1000 sqft) | Price (1000s of dollars) |
---|---|
1.0 | 300 |
2.0 | 500 |
You would like to fit a linear regression model (shown above as the blue straight line) through these two points, so you can then predict price for other houses - say, a house with 1200 sqft.
Please run the following code cell to create your x_train
and y_train
variables. The data is stored in one-dimensional NumPy arrays.
Note: The course will frequently utilize the python 'f-string' output formatting described here when printing. The content between the curly braces is evaluated when producing the output.
Number of training examples m
You will use m
to denote the number of training examples. Numpy arrays have a .shape
parameter. x_train.shape
returns a python tuple with an entry for each dimension. x_train.shape[0]
is the length of the array and number of examples as shown below.
One can also use the Python len()
function as shown below.
Training example x_i, y_i
You will use (x, y) to denote the training example. Since Python is zero indexed, (x, y) is (1.0, 300.0) and (x, y) is (2.0, 500.0).
To access a value in a Numpy array, one indexes the array with the desired offset. For example the syntax to access location zero of x_train
is x_train[0]
. Run the next code block below to get the training example.
Plotting the data
You can plot these two points using the scatter()
function in the matplotlib
library, as shown in the cell below.
The function arguments
marker
andc
show the points as red crosses (the default is blue dots).
You can use other functions in the matplotlib
library to set the title and labels to display
Model function
As described in lecture, the model function for linear regression (which is a function that maps from
x
to y
) is represented as
The formula above is how you can represent straight lines - different values of and give you different straight lines on the plot.
Let's try to get a better intuition for this through the code blocks below. Let's start with and .
Note: You can come back to this cell to adjust the model's w and b parameters
Now, let's compute the value of for your two data points. You can explicitly write this out for each data point as -
for , f_wb = w * x[0] + b
for , f_wb = w * x[1] + b
For a large number of data points, this can get unwieldy and repetitive. So instead, you can calculate the function output in a for
loop as shown in the compute_model_output
function below.
Note: The argument description
(ndarray (m,))
describes a Numpy n-dimensional array of shape (m,).(scalar)
describes an argument without dimensions, just a magnitude. Note:np.zero(n)
will return a one-dimensional numpy array with entries
Now let's call the compute_model_output
function and plot the output..
As you can see, setting and does not result in a line that fits our data.
Challenge
Try experimenting with different values of and . What should the values be for a line that fits our data?
Tip:
You can use your mouse to click on the triangle to the left of the green "Hints" below to reveal some hints for choosing b and w.
- Try and
Prediction
Now that we have a model, we can use it to make our original prediction. Let's predict the price of a house with 1200 sqft. Since the units of are in 1000's of sqft, is 1.2.
Congratulations!
In this lab you have learned:
Linear regression builds a model which establishes a relationship between features and targets
In the example above, the feature was house size and the target was house price
for simple linear regression, the model has two parameters and whose values are 'fit' using training data.
once a model's parameters have been determined, the model can be used to make predictions on novel data.