240615
This commit is contained in:
@@ -9,7 +9,8 @@
|
||||
"<figure>\n",
|
||||
" <img src=\"./images/C1_W1_L3_S1_Lecture_b.png\" style=\"width:600px;height:200px;\">\n",
|
||||
"</figure>"
|
||||
]
|
||||
],
|
||||
"id": "d5947faf528d757"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -18,7 +19,8 @@
|
||||
"## Goals\n",
|
||||
"In this lab you will:\n",
|
||||
"- Learn to implement the model $f_{w,b}$ for linear regression with one variable"
|
||||
]
|
||||
],
|
||||
"id": "428fc5f273aad770"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -39,7 +41,8 @@
|
||||
"| $w$ | parameter: weight | `w` |\n",
|
||||
"| $b$ | parameter: bias | `b` | \n",
|
||||
"| $f_{w,b}(x^{(i)})$ | The result of the model evaluation at $x^{(i)}$ parameterized by $w,b$: $f_{w,b}(x^{(i)}) = wx^{(i)}+b$ | `f_wb` | \n"
|
||||
]
|
||||
],
|
||||
"id": "7b23d466e8a13579"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -49,7 +52,8 @@
|
||||
"In this lab you will make use of: \n",
|
||||
"- NumPy, a popular library for scientific computing\n",
|
||||
"- Matplotlib, a popular library for plotting data"
|
||||
]
|
||||
],
|
||||
"id": "d6756737db18e1c"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -60,7 +64,8 @@
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"plt.style.use('./deeplearning.mplstyle')"
|
||||
]
|
||||
],
|
||||
"id": "792fae4c5017098a"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -78,14 +83,16 @@
|
||||
"| 2.0 | 500 |\n",
|
||||
"\n",
|
||||
"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.\n"
|
||||
]
|
||||
],
|
||||
"id": "f8e92dd846374938"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Please run the following code cell to create your `x_train` and `y_train` variables. The data is stored in one-dimensional NumPy arrays."
|
||||
]
|
||||
],
|
||||
"id": "d877986e09640e0f"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -108,14 +115,16 @@
|
||||
"y_train = np.array([300.0, 500.0])\n",
|
||||
"print(f\"x_train = {x_train}\")\n",
|
||||
"print(f\"y_train = {y_train}\")"
|
||||
]
|
||||
],
|
||||
"id": "5306a3aeca0c549a"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
">**Note**: The course will frequently utilize the python 'f-string' output formatting described [here](https://docs.python.org/3/tutorial/inputoutput.html) when printing. The content between the curly braces is evaluated when producing the output."
|
||||
]
|
||||
],
|
||||
"id": "1f2af3b208024cc1"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -123,7 +132,8 @@
|
||||
"source": [
|
||||
"### Number of training examples `m`\n",
|
||||
"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."
|
||||
]
|
||||
],
|
||||
"id": "8e8c52a75e71157d"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -144,14 +154,16 @@
|
||||
"print(f\"x_train.shape: {x_train.shape}\")\n",
|
||||
"m = x_train.shape[0]\n",
|
||||
"print(f\"Number of training examples is: {m}\")"
|
||||
]
|
||||
],
|
||||
"id": "eca4f6257ac4de85"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"One can also use the Python `len()` function as shown below."
|
||||
]
|
||||
],
|
||||
"id": "a0f74fc14328cbb1"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -170,7 +182,8 @@
|
||||
"# m is the number of training examples\n",
|
||||
"m = len(x_train)\n",
|
||||
"print(f\"Number of training examples is: {m}\")"
|
||||
]
|
||||
],
|
||||
"id": "314bdb01cd4b140b"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -182,7 +195,8 @@
|
||||
"\n",
|
||||
"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]`.\n",
|
||||
"Run the next code block below to get the $i^{th}$ training example."
|
||||
]
|
||||
],
|
||||
"id": "7954c480ba221f81"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -203,14 +217,16 @@
|
||||
"x_i = x_train[i]\n",
|
||||
"y_i = y_train[i]\n",
|
||||
"print(f\"(x^({i}), y^({i})) = ({x_i}, {y_i})\")"
|
||||
]
|
||||
],
|
||||
"id": "b6a441b1c3984a36"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Plotting the data"
|
||||
]
|
||||
],
|
||||
"id": "34065d7bc1038bf0"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -220,7 +236,8 @@
|
||||
"- The function arguments `marker` and `c` show the points as red crosses (the default is blue dots).\n",
|
||||
"\n",
|
||||
"You can use other functions in the `matplotlib` library to set the title and labels to display"
|
||||
]
|
||||
],
|
||||
"id": "e89956448ccd316d"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -248,7 +265,8 @@
|
||||
"# Set the x-axis label\n",
|
||||
"plt.xlabel('Size (1000 sqft)')\n",
|
||||
"plt.show()"
|
||||
]
|
||||
],
|
||||
"id": "c1b08142e0243c78"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -265,7 +283,8 @@
|
||||
"Let's try to get a better intuition for this through the code blocks below. Let's start with $w = 100$ and $b = 100$. \n",
|
||||
"\n",
|
||||
"**Note: You can come back to this cell to adjust the model's w and b parameters**"
|
||||
]
|
||||
],
|
||||
"id": "3d4f63fe1b74df05"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -286,7 +305,8 @@
|
||||
"b = 100\n",
|
||||
"print(f\"w: {w}\")\n",
|
||||
"print(f\"b: {b}\")"
|
||||
]
|
||||
],
|
||||
"id": "6ef1590ead23e422"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -301,7 +321,8 @@
|
||||
"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.\n",
|
||||
"> **Note**: The argument description `(ndarray (m,))` describes a Numpy n-dimensional array of shape (m,). `(scalar)` describes an argument without dimensions, just a magnitude. \n",
|
||||
"> **Note**: `np.zero(n)` will return a one-dimensional numpy array with $n$ entries \n"
|
||||
]
|
||||
],
|
||||
"id": "2c9b74a9dafeb729"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -324,14 +345,16 @@
|
||||
" f_wb[i] = w * x[i] + b\n",
|
||||
" \n",
|
||||
" return f_wb"
|
||||
]
|
||||
],
|
||||
"id": "b6247cda89575683"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Now let's call the `compute_model_output` function and plot the output.."
|
||||
]
|
||||
],
|
||||
"id": "ab0afd05a817d94f"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -366,7 +389,8 @@
|
||||
"plt.xlabel('Size (1000 sqft)')\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
],
|
||||
"id": "95d34926475c6344"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -379,7 +403,8 @@
|
||||
"\n",
|
||||
"#### Tip:\n",
|
||||
"You can use your mouse to click on the green \"Hints\" below to reveal some hints for choosing b and w."
|
||||
]
|
||||
],
|
||||
"id": "a1b84ae24243cb1b"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -394,7 +419,8 @@
|
||||
" <li>Try $w = 200$ and $b = 100$ </li>\n",
|
||||
" </ul>\n",
|
||||
" </p>"
|
||||
]
|
||||
],
|
||||
"id": "ee76a723f8f5dbd5"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -402,7 +428,8 @@
|
||||
"source": [
|
||||
"### Prediction\n",
|
||||
"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 $x$ are in 1000's of sqft, $x$ is 1.2.\n"
|
||||
]
|
||||
],
|
||||
"id": "7f423cd19a7ba591"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -424,7 +451,8 @@
|
||||
"cost_1200sqft = w * x_i + b \n",
|
||||
"\n",
|
||||
"print(f\"${cost_1200sqft:.0f} thousand dollars\")"
|
||||
]
|
||||
],
|
||||
"id": "9cdc794cbcf34c22"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -436,14 +464,16 @@
|
||||
" - In the example above, the feature was house size and the target was house price\n",
|
||||
" - for simple linear regression, the model has two parameters $w$ and $b$ whose values are 'fit' using *training data*.\n",
|
||||
" - once a model's parameters have been determined, the model can be used to make predictions on novel data."
|
||||
]
|
||||
],
|
||||
"id": "4c8ad73f0d6f18f2"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
"source": [],
|
||||
"id": "b3eb2771a91f081b"
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
|
||||
Reference in New Issue
Block a user