{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Topic 22: Practice problems\n",
    "\n",
    "Asiman Data School, Module 3: Comprehensive Machine Learning. Week 11, Thursday.\n",
    "\n",
    "Fifteen problems in three levels, on the same ideas and at the same level as the lesson notebook.\n",
    "\n",
    "How to work\n",
    "\n",
    "- Write your code in the cell under each problem and run it with Shift + Enter. Print exactly what the problem asks for.\n",
    "- A hint and one possible solution are under each problem. Open them only after you have tried.\n",
    "- The problems share the variables of the setup cell, so run that cell first. A problem that continues from another problem needs that problem's cell to be run before it.\n",
    "\n",
    "Tip: By hand: type your result into the answer boxes and press Check. Decimals are written with a point, such as `0.48`. In Python: only what your code prints is compared with the expected output, so any code that prints the same text is right. Print exactly what the task asks for, in the same order and with the same rounding."
   ],
   "id": "t22p-000"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Setup\n",
    "\n",
    "Run this cell first. Every problem below uses what it creates."
   ],
   "id": "t22p-001"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from sklearn.linear_model import LinearRegression\n",
    "from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score, root_mean_squared_error"
   ],
   "id": "t22p-002"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Easy"
   ],
   "id": "t22p-003"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 16. A table with one column\n",
    "\n",
    "Reshape `x` into a table `X` with one column. Print the shape of `X`, then its first row."
   ],
   "id": "t22p-004"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.array([3, 5, 7, 9])\n",
    "\n",
    "# Write your code here"
   ],
   "id": "t22p-005"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`X = x.reshape(-1, 1)`, then `print(X.shape)` and `print(X[0])`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "x = np.array([3, 5, 7, 9])\n",
    "\n",
    "X = x.reshape(-1, 1)\n",
    "\n",
    "print(X.shape)\n",
    "print(X[0])\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "(4, 1)\n",
    "[3]\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t22p-006"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 17. The first model\n",
    "\n",
    "Train a `LinearRegression` on `x` and `y`. Print the intercept and the slope on one line, each rounded to 2 decimals."
   ],
   "id": "t22p-007"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.array([3, 5, 7, 9])\n",
    "y = np.array([4, 7, 8, 13])\n",
    "\n",
    "# Write your code here"
   ],
   "id": "t22p-008"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`model.fit(x.reshape(-1, 1), y)`. The intercept is `model.intercept_`, the slope is `model.coef_[0]`: `print(round(model.intercept_, 2), round(model.coef_[0], 2))`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "x = np.array([3, 5, 7, 9])\n",
    "y = np.array([4, 7, 8, 13])\n",
    "\n",
    "model = LinearRegression()\n",
    "model.fit(x.reshape(-1, 1), y)\n",
    "\n",
    "print(round(model.intercept_, 2), round(model.coef_[0], 2))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "-0.4 1.4\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t22p-009"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 18. Two predictions\n",
    "\n",
    "Train a model on `x` and `y`. Print its predictions for `x = 6` and `x = 10` as one array, rounded to 2 decimals."
   ],
   "id": "t22p-010"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.array([3, 5, 7, 9])\n",
    "y = np.array([4, 7, 8, 13])\n",
    "\n",
    "# Write your code here"
   ],
   "id": "t22p-011"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "The new values must be a table with one column: `model.predict(np.array([[6], [10]])).round(2)`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "x = np.array([3, 5, 7, 9])\n",
    "y = np.array([4, 7, 8, 13])\n",
    "\n",
    "model = LinearRegression()\n",
    "model.fit(x.reshape(-1, 1), y)\n",
    "\n",
    "print(model.predict(np.array([[6], [10]])).round(2))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "[ 8.  13.6]\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t22p-012"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 19. Predictions and residuals\n",
    "\n",
    "Train a model on `x` and `y`. Print the predictions for the four points, and on a second line the residuals, both rounded to 2 decimals."
   ],
   "id": "t22p-013"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.array([2, 4, 6, 8])\n",
    "y = np.array([3, 7, 8, 12])\n",
    "\n",
    "# Write your code here"
   ],
   "id": "t22p-014"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`predicted = model.predict(X)`, and the residuals are `y - predicted`. Print each array with `.round(2)`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "x = np.array([2, 4, 6, 8])\n",
    "y = np.array([3, 7, 8, 12])\n",
    "\n",
    "X = x.reshape(-1, 1)\n",
    "model = LinearRegression()\n",
    "model.fit(X, y)\n",
    "\n",
    "predicted = model.predict(X)\n",
    "print(predicted.round(2))\n",
    "print((y - predicted).round(2))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "[ 3.3  6.1  8.9 11.7]\n",
    "[-0.3  0.9 -0.9  0.3]\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t22p-015"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 20. MSE and RMSE with NumPy\n",
    "\n",
    "Train a model on `x` and `y`. Calculate the MSE and the RMSE with NumPy, without `sklearn.metrics`. Print both on one line, rounded to 4 decimals."
   ],
   "id": "t22p-016"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.array([1, 2, 3, 4, 5])\n",
    "y = np.array([2, 4, 5, 4, 5])\n",
    "\n",
    "# Write your code here"
   ],
   "id": "t22p-017"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "The MSE is the mean of the squared residuals: `mse = (residuals ** 2).mean()`. The RMSE is `np.sqrt(mse)`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "x = np.array([1, 2, 3, 4, 5])\n",
    "y = np.array([2, 4, 5, 4, 5])\n",
    "\n",
    "X = x.reshape(-1, 1)\n",
    "model = LinearRegression()\n",
    "model.fit(X, y)\n",
    "\n",
    "residuals = y - model.predict(X)\n",
    "mse = (residuals ** 2).mean()\n",
    "\n",
    "print(round(mse, 4), round(np.sqrt(mse), 4))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "0.48 0.6928\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t22p-018"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Medium"
   ],
   "id": "t22p-019"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 21. MAE and R² with scikit-learn\n",
    "\n",
    "Train a model on `x` and `y`. Print the MAE, and on a second line R², each rounded to 4 decimals. Use `mean_absolute_error` and `r2_score`."
   ],
   "id": "t22p-020"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.array([10, 20, 30, 40, 50])\n",
    "y = np.array([25, 45, 50, 70, 85])\n",
    "\n",
    "# Write your code here"
   ],
   "id": "t22p-021"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Both functions take the true values first: `mean_absolute_error(y, predicted)` and `r2_score(y, predicted)`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "x = np.array([10, 20, 30, 40, 50])\n",
    "y = np.array([25, 45, 50, 70, 85])\n",
    "\n",
    "X = x.reshape(-1, 1)\n",
    "model = LinearRegression()\n",
    "model.fit(X, y)\n",
    "predicted = model.predict(X)\n",
    "\n",
    "print(round(mean_absolute_error(y, predicted), 4))\n",
    "print(round(r2_score(y, predicted), 4))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "2.4\n",
    "0.9779\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t22p-022"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 22. Two ways to R²\n",
    "\n",
    "Train a model on `x` and `y`. Print `model.score(X, y)` and `r2_score(y, predicted)` on one line, each rounded to 4 decimals."
   ],
   "id": "t22p-023"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.array([0, 2, 4, 6])\n",
    "y = np.array([10, 8, 5, 1])\n",
    "\n",
    "# Write your code here"
   ],
   "id": "t22p-024"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`score` gets `X` and `y`. `r2_score` gets `y` and the predictions `model.predict(X)`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "x = np.array([0, 2, 4, 6])\n",
    "y = np.array([10, 8, 5, 1])\n",
    "\n",
    "X = x.reshape(-1, 1)\n",
    "model = LinearRegression()\n",
    "model.fit(X, y)\n",
    "\n",
    "print(round(model.score(X, y), 4), round(r2_score(y, model.predict(X)), 4))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "0.9783 0.9783\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t22p-025"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 23. The formulas in NumPy\n",
    "\n",
    "Calculate the slope and the intercept with the formulas of Part 5, without scikit-learn. Print the slope and the intercept on one line, each rounded to 2 decimals."
   ],
   "id": "t22p-026"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.array([10, 20, 30, 40, 50])\n",
    "y = np.array([25, 45, 50, 70, 85])\n",
    "\n",
    "# Write your code here"
   ],
   "id": "t22p-027"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`x_dev = x - x.mean()` and `y_dev = y - y.mean()`. The slope is `(x_dev * y_dev).sum() / (x_dev ** 2).sum()`, the intercept `y.mean() - w1 * x.mean()`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "x = np.array([10, 20, 30, 40, 50])\n",
    "y = np.array([25, 45, 50, 70, 85])\n",
    "\n",
    "x_dev = x - x.mean()\n",
    "y_dev = y - y.mean()\n",
    "w1 = (x_dev * y_dev).sum() / (x_dev ** 2).sum()\n",
    "w0 = y.mean() - w1 * x.mean()\n",
    "\n",
    "print(round(w1, 2), round(w0, 2))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "1.45 11.5\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t22p-028"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 24. A function for the loss\n",
    "\n",
    "Write a function `sse(w0, w1)` that returns the sum of the squared residuals of the line `w0 + w1 * x`. The two print lines are already there."
   ],
   "id": "t22p-029"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.array([2, 4, 6, 8])\n",
    "y = np.array([3, 7, 8, 12])\n",
    "\n",
    "# Write the function sse(w0, w1) here\n",
    "\n",
    "print(round(sse(1, 1.3), 2))\n",
    "print(round(sse(0.5, 1.4), 2))"
   ],
   "id": "t22p-030"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "Inside the function: `predicted = w0 + w1 * x`, then `return ((y - predicted) ** 2).sum()`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "x = np.array([2, 4, 6, 8])\n",
    "y = np.array([3, 7, 8, 12])\n",
    "\n",
    "def sse(w0, w1):\n",
    "    predicted = w0 + w1 * x\n",
    "    return ((y - predicted) ** 2).sum()\n",
    "\n",
    "print(round(sse(1, 1.3), 2))\n",
    "print(round(sse(0.5, 1.4), 2))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "2.0\n",
    "1.8\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t22p-031"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 25. Ten steps of gradient descent\n",
    "\n",
    "The model is `w * x`. Start at `w = 0.0` and take 10 steps of gradient descent with the learning rate 0.1. Print `w`, rounded to 4 decimals."
   ],
   "id": "t22p-032"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.array([1, 2])\n",
    "y = np.array([4, 8])\n",
    "\n",
    "# Write your code here"
   ],
   "id": "t22p-033"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "In every step: `error = w * x - y`, `slope = 2 * (error * x).mean()`, `w = w - 0.1 * slope`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "x = np.array([1, 2])\n",
    "y = np.array([4, 8])\n",
    "\n",
    "w = 0.0\n",
    "rate = 0.1\n",
    "for step in range(10):\n",
    "    error = w * x - y\n",
    "    slope = 2 * (error * x).mean()\n",
    "    w = w - rate * slope\n",
    "\n",
    "print(round(w, 4))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "3.9961\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t22p-034"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Medium-Hard"
   ],
   "id": "t22p-035"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 26. Gradient descent for a line\n",
    "\n",
    "Find the line `w0 + w1 * x` by gradient descent: start at `w0 = 0.0`, `w1 = 0.0`, learning rate 0.05, 5000 steps. Print `w0` and `w1` on one line, each rounded to 2 decimals."
   ],
   "id": "t22p-036"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.array([1, 2, 3, 4, 5])\n",
    "y = np.array([2, 4, 5, 4, 5])\n",
    "\n",
    "# Write your code here"
   ],
   "id": "t22p-037"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "In every step: `error = (w0 + w1 * x) - y`, `slope0 = 2 * error.mean()`, `slope1 = 2 * (error * x).mean()`. Calculate both slopes before you change `w0` and `w1`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "x = np.array([1, 2, 3, 4, 5])\n",
    "y = np.array([2, 4, 5, 4, 5])\n",
    "\n",
    "w0, w1 = 0.0, 0.0\n",
    "rate = 0.05\n",
    "for step in range(5000):\n",
    "    error = (w0 + w1 * x) - y\n",
    "    slope0 = 2 * error.mean()\n",
    "    slope1 = 2 * (error * x).mean()\n",
    "    w0 = w0 - rate * slope0\n",
    "    w1 = w1 - rate * slope1\n",
    "\n",
    "print(round(w0, 2), round(w1, 2))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "2.2 0.6\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t22p-038"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 27. Two features\n",
    "\n",
    "Every row of `houses` is an area and a number of rooms. Train a model. Print the two weights (rounded to 2 decimals), then the intercept (2 decimals), then the prediction for 100 m² and 3 rooms (1 decimal), on three lines."
   ],
   "id": "t22p-039"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "houses = np.array([[50, 2], [65, 2], [80, 3], [95, 3], [110, 4], [140, 5]])\n",
    "price = np.array([110, 135, 172, 196, 235, 300])\n",
    "\n",
    "# Write your code here"
   ],
   "id": "t22p-040"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`houses` already has two dimensions: no reshape. `print(model.coef_.round(2))`, `print(round(model.intercept_, 2))`, `print(model.predict(np.array([[100, 3]])).round(1))`.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "houses = np.array([[50, 2], [65, 2], [80, 3], [95, 3], [110, 4], [140, 5]])\n",
    "price = np.array([110, 135, 172, 196, 235, 300])\n",
    "\n",
    "model = LinearRegression()\n",
    "model.fit(houses, price)\n",
    "\n",
    "print(model.coef_.round(2))\n",
    "print(round(model.intercept_, 2))\n",
    "print(model.predict(np.array([[100, 3]])).round(1))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "[ 1.62 14.41]\n",
    "-0.34\n",
    "[205.2]\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t22p-041"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 28. One feature or two?\n",
    "\n",
    "Train one model on the area alone and one on both features. Print the R² of both on one line, each rounded to 4 decimals."
   ],
   "id": "t22p-042"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "houses = np.array([[50, 2], [65, 2], [80, 3], [95, 3], [110, 4], [140, 5]])\n",
    "price = np.array([110, 135, 172, 196, 235, 300])\n",
    "\n",
    "# Write your code here"
   ],
   "id": "t22p-043"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`houses[:, [0]]` is the first column as a table. Each model is scored on the features it was trained on. The R² with two features is at least as high: on the training data an extra feature can never lower it.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "houses = np.array([[50, 2], [65, 2], [80, 3], [95, 3], [110, 4], [140, 5]])\n",
    "price = np.array([110, 135, 172, 196, 235, 300])\n",
    "\n",
    "area = houses[:, [0]]\n",
    "one = LinearRegression().fit(area, price)\n",
    "two = LinearRegression().fit(houses, price)\n",
    "\n",
    "print(round(one.score(area, price), 4), round(two.score(houses, price), 4))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "0.9971 0.9998\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t22p-044"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 29. One more point\n",
    "\n",
    "Add the point `x = 60`, `y = 20` to the data. Print the slope without the extra point, and on a second line the slope with it, each rounded to 2 decimals."
   ],
   "id": "t22p-045"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = np.array([10, 20, 30, 40, 50])\n",
    "y = np.array([25, 45, 50, 70, 85])\n",
    "\n",
    "# Write your code here"
   ],
   "id": "t22p-046"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`np.append(x, 60)` gives a new array with one more value. Train two models and print `round(model.coef_[0], 2)` of each.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "x = np.array([10, 20, 30, 40, 50])\n",
    "y = np.array([25, 45, 50, 70, 85])\n",
    "\n",
    "x_more = np.append(x, 60)\n",
    "y_more = np.append(y, 20)\n",
    "\n",
    "first = LinearRegression().fit(x.reshape(-1, 1), y)\n",
    "second = LinearRegression().fit(x_more.reshape(-1, 1), y_more)\n",
    "\n",
    "print(round(first.coef_[0], 2))\n",
    "print(round(second.coef_[0], 2))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "1.45\n",
    "0.33\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t22p-047"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Problem 30. Fifty points with noise\n",
    "\n",
    "The 50 points were made from the line `2 * x + 5` plus random noise. Train a model on them. Print the slope and the intercept on one line (each rounded to 2 decimals), then R² (3 decimals)."
   ],
   "id": "t22p-048"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rng = np.random.default_rng(7)\n",
    "x = rng.uniform(0, 10, 50).round(1)\n",
    "y = (2 * x + 5 + rng.normal(0, 2, 50)).round(1)\n",
    "\n",
    "# Write your code here"
   ],
   "id": "t22p-049"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<details>\n",
    "<summary><b>Show a hint</b></summary>\n",
    "\n",
    "`X = x.reshape(-1, 1)`. Then `print(round(model.coef_[0], 2), round(model.intercept_, 2))` and `print(round(model.score(X, y), 3))`. The model finds numbers close to 2 and 5, not exactly: the noise hides the true line a little.\n",
    "\n",
    "</details>\n",
    "\n",
    "<details>\n",
    "<summary><b>Show the solution</b></summary>\n",
    "\n",
    "```python\n",
    "rng = np.random.default_rng(7)\n",
    "x = rng.uniform(0, 10, 50).round(1)\n",
    "y = (2 * x + 5 + rng.normal(0, 2, 50)).round(1)\n",
    "\n",
    "X = x.reshape(-1, 1)\n",
    "model = LinearRegression()\n",
    "model.fit(X, y)\n",
    "\n",
    "print(round(model.coef_[0], 2), round(model.intercept_, 2))\n",
    "print(round(model.score(X, y), 3))\n",
    "```\n",
    "\n",
    "**Output**\n",
    "\n",
    "```\n",
    "1.92 5.47\n",
    "0.917\n",
    "```\n",
    "\n",
    "</details>"
   ],
   "id": "t22p-050"
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When you have finished, compare your solutions with the hidden ones. A different solution that prints the same numbers is just as good."
   ],
   "id": "t22p-051"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
