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": "7fae6127b91f846d"
|
||||
},
|
||||
{
|
||||
"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": "558caa26f894e501"
|
||||
},
|
||||
{
|
||||
"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": "387f93949917f1d2"
|
||||
},
|
||||
{
|
||||
"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": "26fe2d14cedb9a1a"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -60,7 +64,8 @@
|
||||
"import numpy as np\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"plt.style.use('./deeplearning.mplstyle')"
|
||||
]
|
||||
],
|
||||
"id": "e7a25e396c4b3d2d"
|
||||
},
|
||||
{
|
||||
"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": "a9cebb19cb18409e"
|
||||
},
|
||||
{
|
||||
"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": "514bee6257b3de45"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -99,14 +106,16 @@
|
||||
"y_train = np.array([300.0, 500.0])\n",
|
||||
"print(f\"x_train = {x_train}\")\n",
|
||||
"print(f\"y_train = {y_train}\")"
|
||||
]
|
||||
],
|
||||
"id": "a2b5841f412550ba"
|
||||
},
|
||||
{
|
||||
"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": "8845118d4044e7df"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -114,7 +123,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": "9435bc31d71a55a1"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -126,14 +136,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": "3042542073a8dee"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"One can also use the Python `len()` function as shown below."
|
||||
]
|
||||
],
|
||||
"id": "681e2c43a225a085"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -144,7 +156,8 @@
|
||||
"# m is the number of training examples\n",
|
||||
"m = len(x_train)\n",
|
||||
"print(f\"Number of training examples is: {m}\")"
|
||||
]
|
||||
],
|
||||
"id": "29b77e357217cb68"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -156,7 +169,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": "a09b9410eb2bdb4f"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -169,14 +183,16 @@
|
||||
"x_i = x_train[i]\n",
|
||||
"y_i = y_train[i]\n",
|
||||
"print(f\"(x^({i}), y^({i})) = ({x_i}, {y_i})\")"
|
||||
]
|
||||
],
|
||||
"id": "6c3c658d4f75db4"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Plotting the data"
|
||||
]
|
||||
],
|
||||
"id": "a49af67439b97d82"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -186,7 +202,8 @@
|
||||
"- The function arguments `marker` and `c` show the points as red crosses (the default is blue dots).\n",
|
||||
"\n",
|
||||
"You can also use other functions in the `matplotlib` library to display the title and labels for the axes."
|
||||
]
|
||||
],
|
||||
"id": "b15cbcf6b0ca004c"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -203,7 +220,8 @@
|
||||
"# Set the x-axis label\n",
|
||||
"plt.xlabel('Size (1000 sqft)')\n",
|
||||
"plt.show()"
|
||||
]
|
||||
],
|
||||
"id": "24298cc22c6eae4b"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -220,7 +238,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": "1650263e8dd4d997"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -232,7 +251,8 @@
|
||||
"b = 100\n",
|
||||
"print(f\"w: {w}\")\n",
|
||||
"print(f\"b: {b}\")"
|
||||
]
|
||||
],
|
||||
"id": "d14be934509bf334"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -247,7 +267,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": "7d6df6dd84468603"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -270,14 +291,16 @@
|
||||
" f_wb[i] = w * x[i] + b\n",
|
||||
" \n",
|
||||
" return f_wb"
|
||||
]
|
||||
],
|
||||
"id": "1fcf5af9a7d85129"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"Now let's call the `compute_model_output` function and plot the output.."
|
||||
]
|
||||
],
|
||||
"id": "47d041104e3c39d6"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -301,7 +324,8 @@
|
||||
"plt.xlabel('Size (1000 sqft)')\n",
|
||||
"plt.legend()\n",
|
||||
"plt.show()"
|
||||
]
|
||||
],
|
||||
"id": "736ec583c1cf5d67"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -314,7 +338,8 @@
|
||||
"\n",
|
||||
"#### Tip:\n",
|
||||
"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."
|
||||
]
|
||||
],
|
||||
"id": "7dec7d8a8efef44"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -329,7 +354,8 @@
|
||||
" <li>Try $w = 200$ and $b = 100$ </li>\n",
|
||||
" </ul>\n",
|
||||
" </p>"
|
||||
]
|
||||
],
|
||||
"id": "5ba15f56d2568e04"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -337,7 +363,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": "2ae9a0f157f44afb"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
@@ -351,7 +378,8 @@
|
||||
"cost_1200sqft = w * x_i + b \n",
|
||||
"\n",
|
||||
"print(f\"${cost_1200sqft:.0f} thousand dollars\")"
|
||||
]
|
||||
],
|
||||
"id": "7604756c838dc53a"
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
@@ -363,14 +391,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": "b9411842a79a5c"
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
"source": [],
|
||||
"id": "6854e2f089d03b0c"
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
|
||||
Reference in New Issue
Block a user