{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Example Linear Regression\n", "\n", "In this example (generalized) linear regression, as introduced [in the previous section](LinReg) is implemented and applied for estimating a function $f()$ that maps the speed of long distance runners to their heartrate. \n", "\n", "$$\n", "heartrate = f(speed)\n", "$$\n", "\n", "For training the model, a set of 30 samples is applied, each containing the speed (in m/s) of a runner and the heartrate measured at this speed. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "> Note that in this example input data consists of the single feature *speed*, i.e. it is 1-dimensional (d=1). All functions implemented below are tailored to this one-dimensional case." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Required Modules:" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [], "source": [ "#%matplotlib inline\n", "import numpy as np\n", "import pandas as pd\n", "import math\n", "import matplotlib.pyplot as plt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Read data from file. The first column contains an ID, the second column is the speed in m/s and the third column is the heartrate in beats/s." ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | speed | \n", "heartrate | \n", "
---|---|---|
1 | \n", "4.50 | \n", "155.15 | \n", "
2 | \n", "5.00 | \n", "166.68 | \n", "
3 | \n", "4.50 | \n", "164.37 | \n", "
4 | \n", "5.25 | \n", "160.82 | \n", "
5 | \n", "4.50 | \n", "148.51 | \n", "
6 | \n", "4.75 | \n", "169.83 | \n", "
7 | \n", "5.00 | \n", "188.01 | \n", "
8 | \n", "5.50 | \n", "187.90 | \n", "
9 | \n", "4.50 | \n", "157.96 | \n", "
10 | \n", "5.25 | \n", "178.29 | \n", "
11 | \n", "5.25 | \n", "179.55 | \n", "
12 | \n", "5.50 | \n", "203.81 | \n", "
13 | \n", "4.00 | \n", "150.74 | \n", "
14 | \n", "4.50 | \n", "171.78 | \n", "
15 | \n", "5.00 | \n", "172.52 | \n", "
16 | \n", "4.25 | \n", "148.92 | \n", "
17 | \n", "4.25 | \n", "160.02 | \n", "
18 | \n", "5.00 | \n", "183.83 | \n", "
19 | \n", "5.00 | \n", "156.67 | \n", "
20 | \n", "5.00 | \n", "162.40 | \n", "
21 | \n", "5.00 | \n", "171.39 | \n", "
22 | \n", "4.00 | \n", "156.10 | \n", "
23 | \n", "4.50 | \n", "153.15 | \n", "
24 | \n", "4.75 | \n", "161.35 | \n", "
25 | \n", "4.25 | \n", "163.16 | \n", "
26 | \n", "5.25 | \n", "165.42 | \n", "
27 | \n", "5.25 | \n", "189.25 | \n", "
28 | \n", "4.25 | \n", "165.56 | \n", "
29 | \n", "5.25 | \n", "172.35 | \n", "
30 | \n", "4.00 | \n", "158.07 | \n", "