<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Sebastian Höfer&apos;s Blog</title>
    <description>A blog about how to understand AI, robotics and machine learning intuitively, including random posts about all sorts of different topics.
</description>
    <link>http://shoefer.github.io/</link>
    <atom:link href="http://shoefer.github.io/feed.xml" rel="self" type="application/rss+xml"/>
    <pubDate>Thu, 19 Dec 2024 15:39:08 +0000</pubDate>
    <lastBuildDate>Thu, 19 Dec 2024 15:39:08 +0000</lastBuildDate>
    <generator>Jekyll v3.10.0</generator>
    
      <item>
        <title>Yet Another Machine Learning 101</title>
        <description>&lt;p&gt;This post is a somewhat short recap of machine learning in general. I wrote it
as lecture notes for a tutorial that I gave in one of the &lt;a href=&quot;http://www.robotics.tu-berlin.de&quot;&gt;robotics courses at
TU Berlin&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It will probably be difficult to understand it in detail if you are a beginner.
For beginners, I recommend reading &lt;a href=&quot;http://www.intuitivemi.de&quot;&gt;my blog on intuitive machine
intelligence&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The post is also available as an &lt;a href=&quot;https://github.com/shoefer/shoefer.github.io/blob/master/notebooks/Yet%20Another%20Machine%20Learning%20101.ipynb&quot;&gt;ipython notebook&lt;/a&gt; which allows you to play around with the python examples.&lt;/p&gt;

&lt;h2 id=&quot;general-setting&quot;&gt;General setting&lt;/h2&gt;

&lt;p&gt;In general, we distinguish three general paradigms in ML&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Supervised Learning&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Unsupervised Learning&lt;/strong&gt;&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Reinforcement Learning&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Additionally, there is a variety of other paradigms, which are not going to be
covered here, such as &lt;a href=&quot;https://en.wikipedia.org/wiki/Semi-supervised_learning&quot;&gt;&lt;strong&gt;semi-supervised
learning&lt;/strong&gt;&lt;/a&gt; and
&lt;a href=&quot;http://arxiv.org/abs/1511.06429&quot;&gt;&lt;strong&gt;learning from side information&lt;/strong&gt;&lt;/a&gt; which will
not be covered here.&lt;/p&gt;

&lt;p&gt;In all types of machine learning the goal is to learn a function to predict some
output from some input data:&lt;/p&gt;

\[f: X \rightarrow Y\]

&lt;p&gt;The different paradigms differ in&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;the goal, i.e. what kind of data $X$ and $Y$ are&lt;/li&gt;
  &lt;li&gt;the training data available to learn $f$&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We will now review the different paradigms and give examples, however, mostly
focusing on &lt;em&gt;supervised learning&lt;/em&gt;.&lt;/p&gt;

&lt;h1 id=&quot;supervised-learning&quot;&gt;Supervised Learning&lt;/h1&gt;

&lt;p&gt;Supervised learning is by far the most popular paradigm. In this setting, we are
given examples from $X$ and $Y$, i.e. $N$ pairs ${ x^{(i)}, y^{(i)}
}_{i=1\ldots N }$. The data $x^{(i)}$ are called the &lt;em&gt;input data&lt;/em&gt; and $y^{(i)}$
the &lt;em&gt;labels&lt;/em&gt;.
Together, they are called the &lt;em&gt;training data&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Any ML method usually consists of (at least) three ingredients.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;A representation of $f$, i.e. whether it is a linear function, polynomial,
neural network, or non-parametric, etc.&lt;/li&gt;
  &lt;li&gt;An appropriate &lt;em&gt;loss function&lt;/em&gt; $\mathcal{L}$,&lt;/li&gt;
  &lt;li&gt;which is minimized using an &lt;em&gt;optimization method&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The loss function for supervised learning looks as follows:&lt;/p&gt;

\[f = \textrm{argmin}_{f}\ \mathcal{L} ( f, \{ x^{(i)}, y^{(i)} \}_{i=1\ldots N
})\]

&lt;p&gt;A &lt;em&gt;loss function&lt;/em&gt; can be thought of as an assessment of how good $f$ fits the
training data. If its value is high, it means that $f(x^{(i)})$, i.e. our
prediction of the label for $x^{(i)}$ given current function $f$, is very
different from the known true value $y^{(i)}$. If $\mathcal{L} = 0$, it means
the $f$ perfectly fits the data.&lt;/p&gt;

&lt;p&gt;An &lt;em&gt;optimization&lt;/em&gt; method can be thought of as a method that is given data and a
loss function and automatically tries to find the best function minimizing the
loss. There is a wide variety of optimization methods, and they mostly differ by
the properties of $f$ they exploit (e.g. if $f$ is differentiable, we can
compute its derivative, set it to zero, and go step by step in the direction of
steepest descent; this is called &lt;em&gt;gradient descent optimization&lt;/em&gt;, see below). We
only look at some very basic optimization methods, but it is of course the
success in learning a function greatly depends on the optimization used.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;representation&lt;/em&gt; of $f$ also drastically influences its learnability. The
easiest and best-understood representations are linear functions, but also
neural networks (which are some sort of nonlinear functions) are very common
nowadays.&lt;/p&gt;

&lt;p&gt;Usually, as a practitioner, you don’t have to worry about making choices these
three things.
An ML method usually determines  the loss, optimization and function
representation, and is tuned in such a way that all of them work together
nicely.&lt;/p&gt;

&lt;h3 id=&quot;overfitting&quot;&gt;Overfitting&lt;/h3&gt;

&lt;p&gt;We said that the goal of supervised learning is to learn a function $f$ from
training data, using the loss $\mathcal{L}$. However, it is important that a
perfect loss, $\mathcal{L}=0$, for your training does not necessarily solve the
problem – the actual task of supervised learning is to learn an $f$ that
&lt;em&gt;generalizes&lt;/em&gt; to &lt;em&gt;unseen examples&lt;/em&gt; $x^{(j)}$. And actually, fitting the training
data is very easy: just memorize all of them by heart! This will trivially give
us $\mathcal{L}=0$ for our training data. But as we will see, that might mean
that for unseen data we will actually make blatantly wrong predictions.&lt;/p&gt;

&lt;p&gt;To repeat, the core problem of any machine learning method is to learn an $f$
that generalizes to unseen data. If $f$ only works well on the training data,
but not on unseen data, we say $f$ &lt;em&gt;overfits&lt;/em&gt; the data.&lt;/p&gt;

&lt;p&gt;To ensure that our function does not overfit, we divide the data into three
sets:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;em&gt;Training data&lt;/em&gt;&lt;br /&gt;The data used for actually learning/fitting the function&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;Validation data&lt;/em&gt;&lt;br /&gt;Additional data, not used for learning the function, but
only used to compute the loss for our current estimate of the function. It is
also used to tune the hyperparameters of your method, or decide which ML method
to use.&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;Test data&lt;/em&gt; &lt;br /&gt;Another additional data set, neither used for training nor
for validation. You can think of it as being locked in some secret drawer, and
it is only released and used to evaluate $f$ once you are sure that you have
properly learned $f$.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;example-linear-regression&quot;&gt;Example: Linear regression&lt;/h3&gt;

&lt;p&gt;Let us consider a simple example for supervised learning: &lt;em&gt;linear regression&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Assume we want to predict the price of apartments in Berlin, given their size of
square meters of the apartment. Here is some fictitious data:&lt;/p&gt;

&lt;table style=&quot;border 1px solid black;&quot;&gt;
&lt;tr&gt;
&lt;th&gt; $X$ (square meters) &lt;/th&gt;
&lt;th&gt; $Y$ (monthly rent in Euro)&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;40&lt;/td&gt;&lt;td&gt;500&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;65&lt;/td&gt;&lt;td&gt;620&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;80&lt;/td&gt;&lt;td&gt;855&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;81&lt;/td&gt;&lt;td&gt;910&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;100&lt;/td&gt;&lt;td&gt;1100&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;120&lt;/td&gt;&lt;td&gt;1250&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;

&lt;p&gt;Let’s plot the data&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In [1]:&lt;/strong&gt;&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;o&quot;&gt;%&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pylab&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inline&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;set_printoptions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;suppress&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;X&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;65&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;80&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;81&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;120&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;Y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;500&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;620&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;855&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;910&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1250&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;scatter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;Populating the interactive namespace from numpy and matplotlib
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;http://shoefer.github.io/notebooks/yet-another-machine-learning-101_files/yet-another-machine-learning-101_2_1.png&quot; alt=&quot;png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The idea is linear regression is to fit a linear function, i.e. a line of the
form $f(x) = w_1 x + w_0$ to the data. To learn such a line we define as loss
the &lt;em&gt;mean-squared error&lt;/em&gt; which penalizes large deviations of $f(x)$ from the
known $y$:&lt;/p&gt;

\[\mathcal{L}_\textrm{MSE}
= \frac{1}{2N}  \sum_{i=0}^N ( f(x^{(i)}) - y^{(i)})^2\\
= \frac{1}{2N} \sum_{i=0}^N ( w_1 x^{(i)} + w_0 - y^{(i)})^2\]

&lt;p&gt;Maybe you are a bit confused about the fraction $\frac{1}{2N}$. The reason we
use it, because if we compute the derivative $\mathcal{L}_\textrm{MSE}$, the
square within the sum cancels out this fraction; and the $\frac{1}{N}$
normalizes for the number of training examples.&lt;/p&gt;

&lt;p&gt;Note that usually $x$ is multi-dimensional, i.e. a vector $\mathbf{x}$. Then
$w_1$ becomes a weight vector $\mathbf{w}$.
We can do another trick to get rid of $w_0$ (called the &lt;em&gt;bias&lt;/em&gt; or &lt;em&gt;intercept&lt;/em&gt;
term) by appending a $1$ to $\mathbf{x}$, and extending $\mathbf{w}$ by one
dimension. This facilitates notation a bit, as it allows us to write the loss
using vector notation:&lt;/p&gt;

\[\mathcal{L}_\textrm{MSE} = \frac{1}{2N} \sum_{i=0}^N ( \mathbf{w}^T
\mathbf{x}^{(i)} - y^{(i)})^2\]

&lt;h3 id=&quot;gradient-descent&quot;&gt;Gradient descent&lt;/h3&gt;

&lt;p&gt;How to optimize it? One way is to compute the partial derivative wrt. each
$j$-th element of the weight (the so-called &lt;em&gt;gradient&lt;/em&gt;), and change the updates:&lt;/p&gt;

\[\nabla \mathcal{L}_\textrm{MSE} =
\frac{\partial \mathcal{L}_\textrm{MSE}}{\partial {w}_j}
= \frac{1}{2N} \sum_{i=0}^N \frac{\partial}{\partial {w}_j}
( \mathbf{w}^T \mathbf{x}^{(i)} - y^{(i)})^2 \\
= \frac{1}{2N} \sum_{i=0}^N 2 w_j^{(i)}  (w_j x_j^{(i)} - y^{(j)})\\
= \frac{1}{N} \sum_{i=0}^N w_j (f(x_j^{(i)}) - y^{(j)})\]

&lt;p&gt;The &lt;a href=&quot;https://en.wikipedia.org/wiki/Gradient_descent&quot;&gt;gradient descent&lt;/a&gt; algorithm
(for linear regression with MSE) then goes as follows:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Initialize the weights $\mathbf{w}$ randomly&lt;/li&gt;
  &lt;li&gt;Update each $j$-th entry in the weight vector by following the negative
gradient:&lt;br /&gt;$w_j^\textrm{new} = w_j - \alpha \frac{1}{N} \sum_{i=0}^N w_j
(f(x_j^{(i)}) - y^{(j)})$&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here, $\alpha$ denotes the step size. It has to small enough, otherwise we might
overshoot and miss the minimum.&lt;/p&gt;

&lt;p&gt;If we image the loss function to be a “bowl” and every $\mathbf{w}$ to be a
point in this bowl, the gradient points towards the bottom of this bowl, and
thus the minimum of the loss. The update rule then takes small steps towards
this minimum.
This is a cool visualization which is also accompanied by a &lt;a href=&quot;http://www.mathworks.com/matlabcentral/fileexchange/35389-gradient-
descent-visualization&quot;&gt;Matlab
script&lt;/a&gt;:
&lt;img src=&quot;http://www.mathworks.com/matlabcentral/mlc-downloads/downloads/submissions/35389/versions/1/screenshot.png&quot; style=&quot;width:30%&quot; alt=&quot;Obtained from http://www.mathworks.com/matlabcentral/fileexchange/35389-gradient-descent- visualization&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Gradient descent is a very important technique, very popular especially for
training neural networks (see below).&lt;/p&gt;

&lt;h3 id=&quot;ordinary-least-squares&quot;&gt;Ordinary least squares&lt;/h3&gt;

&lt;p&gt;In the linear regression case, however, there is a more direct solution. If we
consider $\mathbf{w}$ to be a matrix rather than a vector, we can write the loss
in the following way:&lt;/p&gt;

\[\mathcal{L}_\textrm{MSE} = \frac{1}{2} (\mathbf{X}\mathbf{w} - \mathbf{y})^T
(\mathbf{X}\mathbf{w} - \mathbf{y})\]

&lt;p&gt;where $\mathbf{X}$ contains in each $i$-th &lt;em&gt;row&lt;/em&gt; on training example
$\mathbf{x}^{(i)}$, and $\mathbf{y}$ in each $i$-th row a label $y^{(i)}$.&lt;/p&gt;

&lt;p&gt;Now we can compute the derivative of $\mathcal{L}_\textrm{MSE}$, set it to 0,
and solve for $\mathbf{w}$. As you can check yourself, the derivative is given
by:
\(\frac{d \mathcal{L}_\textrm{MSE}}{d \mathbf{w}} = \mathbf{X}\mathbf{w} -
\mathbf{y} \\
\mathbf{X}\mathbf{w} - \mathbf{y}  = 0\\
\mathbf{X}\mathbf{w}  = \mathbf{y}\)&lt;/p&gt;

&lt;p&gt;To now solve it for $\mathbf{w}$, we need to invert $\mathbf{X}$ – which is
usually not possible because it is not square in the general case. But we can
apply a trick, namely use the &lt;em&gt;pseudo-inverse&lt;/em&gt;:&lt;/p&gt;

\[\mathbf{X}\mathbf{w} = \mathbf{y}\\
\mathbf{X}^T\mathbf{X}\mathbf{w} = \mathbf{X}^T\mathbf{y}\\
\mathbf{w} = (\mathbf{X}^T\mathbf{X})^{-1} \mathbf{X}^T\mathbf{y}\\\]

&lt;p&gt;where the &lt;em&gt;pseudo-inverse&lt;/em&gt; is given by $(\mathbf{X}^T\mathbf{X})^{-1}
\mathbf{X}^T$.&lt;/p&gt;

&lt;h3 id=&quot;computational-example&quot;&gt;Computational example&lt;/h3&gt;

&lt;p&gt;Let’s now implement this in python.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In [2]:&lt;/strong&gt;&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;c1&quot;&gt;# transpose training data and append 1
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Xhat&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hstack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ones&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;shape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;1.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))])&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;w&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;linalg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;inv&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Xhat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Xhat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Xhat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;w&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;array([ 10.04581152,  58.78926702])
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;In [3]:&lt;/strong&gt;&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;n&quot;&gt;scatter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Xhat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;img src=&quot;http://shoefer.github.io/notebooks/yet-another-machine-learning-101_files/yet-another-machine-learning-101_5_0.png&quot; alt=&quot;png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Luckily, there are libraries that do all that for us. One of the most popular ML
libraries in python is &lt;em&gt;scikit learn&lt;/em&gt;.
We can easily verify that it computes the same function:&lt;/p&gt;

&lt;p&gt;(we see that sklearn automatically adds a bias, stored in the variable
“intercept_”)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In [4]:&lt;/strong&gt;&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sklearn.linear_model&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LinearRegression&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;lr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LinearRegression&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;lr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;X_rng&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;linspace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;40&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;120&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;scatter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X_rng&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;predict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X_rng&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;lr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;coef_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;intercept_&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;(array([ 10.04581152]), 58.789267015706741)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;http://shoefer.github.io/notebooks/yet-another-machine-learning-101_files/yet-another-machine-learning-101_7_1.png&quot; alt=&quot;png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;We see that the weights and our prediction (the blue line) are identical. And
you see that it fits the data Ok, but not perfectly. Still, it looks like a
reasonable guess. Most importantly, it also makes a prediction for inputs $x$
for which we did not have any training data.&lt;/p&gt;

&lt;h2 id=&quot;overfitting-1&quot;&gt;Overfitting&lt;/h2&gt;

&lt;p&gt;What if we don’t want to fit a line, but some more complex model, e.g. a
polynomial? This is easily done by &lt;em&gt;augmenting our input&lt;/em&gt; (also called &lt;em&gt;feature
expansion&lt;/em&gt;) by different powers of the input:&lt;/p&gt;

&lt;p&gt;$f(\tilde{\mathbf{x}}) = [\mathbf{x}, \mathbf{x}^2, \mathbf{x}^3, … ]$&lt;/p&gt;

&lt;p&gt;Let’s try that out:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In [5]:&lt;/strong&gt;&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;polynomial_feature_expansion&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hstack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;**&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,])&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;X_rng&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;np&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;linspace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;140&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reshape&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;lr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;LinearRegression&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;lr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;polynomial_feature_expansion&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;scatter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X_rng&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;predict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;polynomial_feature_expansion&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X_rng&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)));&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ylim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;lr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;coef_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;intercept_&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;(array([ 14413.72962393,   -394.63014472,      5.20499616,     -0.03319069,
             0.00008213]), -201202.688457913)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;http://shoefer.github.io/notebooks/yet-another-machine-learning-101_files/yet-another-machine-learning-101_9_1.png&quot; alt=&quot;png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;We see that the training data is fitted almost perfectly; but the function
hallucinates weird values inbetween and before/after the training data! Also,
the weights are actually very high. This is a classical example of overfitting:
we used a function that is too “powerful”, as it has many more parameters than
the linear model. There are different ways to remedy this problem:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Big_data&quot;&gt;More training data&lt;/a&gt;!&lt;/li&gt;
  &lt;li&gt;Restricting the function to a simpler one (e.g. less parameters)&lt;br /&gt;(The
danger can be &lt;em&gt;underfitting&lt;/em&gt;, i.e. choosing a too simple model)&lt;/li&gt;
  &lt;li&gt;Model selection, e.g. using &lt;a href=&quot;https://en.wikipedia.org/wiki
/Cross-validation_%28statistics%29&quot;&gt;cross-validation&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Incorporating prior knowledge about the problem, e.g. by &lt;a href=&quot;https://en.wikipedia.org/wiki/Feature_engineering&quot;&gt;feature
engineering&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Regularization&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At this point we will only talk about
&lt;a href=&quot;https://en.wikipedia.org/wiki/Regularization_%28mathematics%29&quot;&gt;regularization&lt;/a&gt;
which a special way to force a powerful function not to overfit. The idea is to
put additional terms into the loss function. A popular regularization is &lt;em&gt;L2&lt;/em&gt;
which puts a &lt;a href=&quot;http://mathworld.wolfram.com/L2-Norm.html&quot;&gt;L2-norm&lt;/a&gt; penalty on the
weights, i.e. $||\mathbf{w}||^2$ into the loss. The optimizer then has to make
sure not only to fulfill the initial loss, e.g. the mean-squared error, but also
the regularization.&lt;/p&gt;

&lt;p&gt;A linear regression with L2 regularization is called &lt;em&gt;ridge regression&lt;/em&gt;. The
loss then becomes:
\(\mathcal{L}_\textrm{Ridge} = \frac{1}{2} (\mathbf{X}\mathbf{w} -
\mathbf{y})^T (\mathbf{X}\mathbf{w} - \mathbf{y}) + \alpha ||\mathbf{w}||^2\)&lt;/p&gt;

&lt;p&gt;where $\alpha$ weights the influence of the regularizer.&lt;/p&gt;

&lt;p&gt;Ridge regression is also implemented in scikit learn:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;In [6]:&lt;/strong&gt;&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-python&quot; data-lang=&quot;python&quot;&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;sklearn.linear_model&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Ridge&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;lr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Ridge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;alpha&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mf&quot;&gt;20.&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;lr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;polynomial_feature_expansion&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;scatter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;plot&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X_rng&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;predict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;polynomial_feature_expansion&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;X_rng&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)));&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ylim&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;200&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;lr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;coef_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;intercept_&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;(array([-0.05208327, -2.0080791 ,  0.05283245, -0.00047523,  0.00000145]),
 1401.9265643035942)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;&lt;img src=&quot;http://shoefer.github.io/notebooks/yet-another-machine-learning-101_files/yet-another-machine-learning-101_11_1.png&quot; alt=&quot;png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;We see, that the weights are lower and the values inbetween are much smoother;
but still for values $&amp;gt;120$ and $&amp;lt;40$ the linear model reflects our intuition
about the real $f$ better. Therefore, we might choose to increase the
regularization, collect more data or choose a different function
parametrization, e.g. a polynomial with lower degree.&lt;/p&gt;

&lt;h2 id=&quot;regression-vs-classification&quot;&gt;Regression vs. Classification&lt;/h2&gt;

&lt;p&gt;Before we talk about more sophisticated supervised learning methods, we should
clarify the terms &lt;em&gt;regression&lt;/em&gt; and &lt;em&gt;classification&lt;/em&gt;. The only difference between
these two concepts is whether $y$ is &lt;em&gt;discrete&lt;/em&gt; or &lt;em&gt;continuous&lt;/em&gt;. In the previous
example we used regression, i.e. we treated the price as a continuous variable.
In classification, we are usually given a discrete, finite set of &lt;em&gt;classes&lt;/em&gt;, and
we are only interested in predicting the class of a new input. The only thing
that changes because of this is the loss. We won’t bother about these loss
functions now, but in case you are interested, common choices are the
&lt;a href=&quot;https://en.wikipedia.org/wiki/Cross_entropy#Cross-
entropy_error_function_and_logistic_regression&quot;&gt;categorical cross-entropy&lt;/a&gt; loss or the &lt;a href=&quot;https://en.wikipedia.org/wiki/Hinge_loss&quot;&gt;hinge
loss&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;But watch out, the terminology is not always fully consistent: training a linear
model with a categorical cross-entropy loss is called &lt;em&gt;logistic regression&lt;/em&gt; –
although it is actually a &lt;em&gt;classification&lt;/em&gt; method!&lt;/p&gt;

&lt;h2 id=&quot;deep-neural-networks&quot;&gt;(Deep) Neural Networks&lt;/h2&gt;

&lt;p&gt;Currently, they are probably the most popular approach in supervised learning.
The idea is to compose the function $f$ of small slightly nonlinear functions
(neurons) and connect them. This small nonlinear functions are called &lt;em&gt;neurons&lt;/em&gt;,
and together they form a neural network that can be visualized as follows:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://upload.wikimedia.org/wikipedia/commons/e/e4/Artificial_neural_network.svg&quot; alt=&quot;Artificial neural network (https://commons.wikimedia.org/wiki/File:Artificial_neural_network.svg)&quot; style=&quot;width: 20%;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The picture (image taken from &lt;a href=&quot;https://commons.wikimedia.org/wiki/Fil
e:Artificial_neural_network.svg&quot;&gt;wikipedia&lt;/a&gt;) depicts a network with an input layer
($=\mathbf{x}$), an output (should be equal to our labels $y$) and a hidden
layer. This hidden layer can learn some representation of $\mathbf{x}$ that is
suitable for predicting $y$. For the record, this network structure is also
sometimes called &lt;em&gt;multi-layer perceptron&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;What does a (non-input and non-output) neuron look like? In fact, a neuron
basically multiplies a linear weight vector with its input (sounds exactly like
linear regression, right?) and then applies some nonlinearity on the output of
this operation (that’s different from linear regression). Let’s make this
formal; a neuron $h_i$ (in the hidden layer), given input $\mathbf{z}$ (in the
network above $\mathbf{z} = \mathbf{x}$), computes its output as follows:&lt;/p&gt;

\[h_i(\mathbf{z}) = \sigma(\mathbf{w}_{h_i}^T\mathbf{z})\]

&lt;p&gt;where $\sigma$ denotes some nonlinear &lt;em&gt;activation function&lt;/em&gt;, often something
like the &lt;em&gt;sigmoid&lt;/em&gt;-function:&lt;/p&gt;

&lt;p&gt;\(\sigma(t) = \frac{1}{1 + e^{-t}}\),&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://upload.wikimedia.org/wikipedia/commons/5/53/Sigmoid-function-2.svg&quot; alt=&quot;Signmoid function (https://commons.wikimedia.org/wiki/File :Sigmoid-function-2.svg)&quot; style=&quot;width: 40%;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;although the &lt;a href=&quot;http://mathworld.wolfram.com/HyperbolicTangent.html&quot;&gt;hyperbolic
tangent&lt;/a&gt; and
&lt;a href=&quot;https://en.wikipedia.org/wiki/Rectifier_%28neural_networks%29&quot;&gt;rectifiers&lt;/a&gt; are
more en vogue in state-of-the art neural networks.&lt;/p&gt;

&lt;p&gt;A hidden layer $f_h$ composed of $H$ neurons then computes its output as
follows:&lt;/p&gt;

\[f_h(\mathbf{z}) = \sigma(\mathbf{W}_{h} \mathbf{z}),\]

&lt;p&gt;where ${\mathbf{W} _ h}$ is a $\mathrm{dim}(\mathbf{z}) \times k$ matrix
composed of the stacked (transposed) weight vectors $\mathbf{w}_{h_i}^T, i=1
\ldots H$, and the activation function $\sigma$ is applied separately to each
output dimension of $\mathbf{W}_H$.&lt;/p&gt;

&lt;p&gt;So what are &lt;em&gt;deep&lt;/em&gt; networks? The idea is to stack multiple hidden layers – the
more hidden layers there are, the “deeper” the network is. Mathematically, it is
just a repeated application of multiplying a linear weight with the output of
the previous layer, then computing the activation, passing it to the next layer,
and so on. The advantage of these networks is, that they can learn more
sophisticated representations of the input data, and thus solve more challenging
learning problems.&lt;/p&gt;

&lt;p&gt;Finally, we have to say how to train neural networks. We can use the same loss
functions as for linear regression (or classification, of course), and apply
gradient descent. However, if we have multiple layers, we need to apply some
tricks to adapt gradient descent. The first trick is &lt;em&gt;backpropagation&lt;/em&gt;; it
basically says that to train multiple layers, we apply gradient descent to each
layer separately, and pass the loss backwards through the network layer by layer
until we reach the beginning. For this to work well, we must apply also some
additional tricks, e.g. setting the initial values of all weights appropriately
and so on. But in principle, training a neural network is not much different
from performing a linear or logistic regression.&lt;/p&gt;

&lt;p&gt;Of course there is a lot more to know abouot deep learning, and if you want a
more in-depth treatment of this topic, check out &lt;a href=&quot;http://www.deeplearningbook.org/&quot;&gt;this recent
book&lt;/a&gt;.&lt;/p&gt;

&lt;h1 id=&quot;unsupervised-learning&quot;&gt;Unsupervised Learning&lt;/h1&gt;

&lt;p&gt;Unsupervised differs from supervised learning that we are only given
${x^{(i)}}_{i=1 \ldots N}$, an no labels. This obviously means that the loss
functions we’ve seen so far will not work. Instead the loss functions impose
some “statistical” constraints on $y$. A good example is &lt;em&gt;Principal component
analysis&lt;/em&gt; (PCA): here we want to learn a low-dimensional variant of $x$ –
however, which still contains roughly the same “information” as the original
$x$. The question is how to quantify “information”. This very complicated and
highly depends on the task; but PCA takes a pragmatic stance by defining
information as &lt;em&gt;high variance&lt;/em&gt; (in a statistical sense). Therefore, the loss for
PCA is roughly equivalent to:&lt;/p&gt;

\[{\mathcal{L}}_\textrm{PCA} \approx -\textrm{Var}(f(\mathbf{x}))\]

&lt;p&gt;where $f$ is indeed a linear function as used above.
The $\approx$ sign reflects that formally the loss is a bit different - we need
some additional constraints to make this problem well-defined, i.e. not find
infinitely large weights. I will not go into details here, but you should
understand that one can formulate learning objectives without any supervised
signal, just by formulating some desired properties of the result of $f$ in the
loss function.&lt;/p&gt;

&lt;p&gt;Note that PCA is somewhat the “regression” variant of unsupervised learning.
There are also methods that map data into discrete representations, e.g. in
clustering. The most popular and yet simplest method is probably
&lt;a href=&quot;https://en.wikipedia.org/wiki/K-means_clustering&quot;&gt;k-means&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Also note that unsupervised learning has somewhat different applications than
supervised learning. Often it is used for pre-processing the input data, in
order to then feed its output to a supervised learning method. Another important
application is exploratory data analysis, i.e. studying and finding patterns in
your data.&lt;/p&gt;

&lt;h1 id=&quot;reinforcement-learning&quot;&gt;Reinforcement Learning&lt;/h1&gt;

&lt;p&gt;In reinforcement learning our outputs $Y$ are &lt;em&gt;actions&lt;/em&gt; that an agent should
take, and our input &lt;em&gt;X&lt;/em&gt; is the state. Therefore, we also rename $X$ and $Y$:
the state is denoted by $\mathbf{s}$, the actions by $\mathbf{a}$, and the
function we want to learn is called &lt;em&gt;policy&lt;/em&gt; $\pi$ (instead of $f$):&lt;/p&gt;

\[\pi(\mathbf{s}) = \mathbf{a}\]

&lt;p&gt;A crucial difference to supervised learning is that we do not know the correct
actions $\mathbf{a}$. Rather, we only get a &lt;em&gt;reward signal&lt;/em&gt; $r(\mathbf{s},
\mathbf{a})$ for every action we take (in a certain state). The reward is a real
number that is high if the action was good, and low if it was bad.&lt;/p&gt;

&lt;p&gt;Obviously, this problem is much harder as learning becomes indirect – you need
to figure out the right action only from getting a reward. The biggest problem
is that rewards are usually sparse and &lt;em&gt;delayed&lt;/em&gt;; that means the agent might
receive a positive reward only after it has executed a set of actions. However,
it might be that actually the first action in this sequence was the most
important one.&lt;/p&gt;

&lt;p&gt;There is a wide variety of different techniques, such as &lt;em&gt;policy search&lt;/em&gt;,
&lt;em&gt;value-based methods&lt;/em&gt; and &lt;em&gt;model-based reinforcement learning&lt;/em&gt; to tackle this
problem. We will look at these techniques at this point, but it is important
that you at least understand the general setting of reinforcement learning, and
its difference to supervised learning (and unsupervised learning).&lt;/p&gt;
</description>
        <pubDate>Thu, 12 May 2016 00:00:00 +0000</pubDate>
        <link>http://shoefer.github.io/2016/05/12/yet-another-machine-learning-101.html</link>
        <guid isPermaLink="true">http://shoefer.github.io/2016/05/12/yet-another-machine-learning-101.html</guid>
        
        <category>python</category>
        
        <category>notebook</category>
        
        
      </item>
    
      <item>
        <title>The Curse of Dimensionality</title>
        <description>&lt;p&gt;In the last post we have looked at one of the big problems of machine learning: when we want to learn &lt;a href=&quot;/intuitivemi/2015/12/28/functions.html&quot;&gt;functions&lt;/a&gt; from data, we have to fight &lt;a href=&quot;/intuitivemi/2015/08/07/overfitting.html&quot;&gt;overfitting&lt;/a&gt;. In this post we will look at another archenemy of learning: dimensionality.&lt;/p&gt;

&lt;h3 id=&quot;parameters&quot;&gt;Parameters&lt;/h3&gt;

&lt;p&gt;Let’s briefly recap our stock price prediction example. In an &lt;a href=&quot;/intuitivemi/2015/12/30/learning-functions.html&quot;&gt;earlier post&lt;/a&gt; we used random search to find the parameters of a line that explains the training data examples well.
The algorithm learned two numbers, namely the &lt;em&gt;parameters&lt;/em&gt; p&lt;sub&gt;1&lt;/sub&gt; and p&lt;sub&gt;2&lt;/sub&gt;:&lt;/p&gt;

&lt;div class=&quot;pseudoformula&quot;&gt;
&lt;b&gt;Stock price&lt;/b&gt; = f(&lt;b&gt;Revenue&lt;/b&gt;) = p&lt;sub&gt;1&lt;/sub&gt; * &lt;b&gt;Revenue&lt;/b&gt; + p&lt;sub&gt;2&lt;/sub&gt;,
&lt;/div&gt;

&lt;p&gt;where visually, p&lt;sub&gt;1&lt;/sub&gt; changes the slope of the line and p&lt;sub&gt;2&lt;/sub&gt; the shift along the y-axis of a line.&lt;/p&gt;

&lt;p&gt;The random search learned the numbers p&lt;sub&gt;1&lt;/sub&gt;=0.00015 and p&lt;sub&gt;2&lt;/sub&gt;=58:&lt;/p&gt;

&lt;div class=&quot;imgcenter imgcap&quot;&gt;




&lt;img src=&quot;/intuitivemi/images/2015-12-29-learning-random_guess.png&quot; class=&quot;&quot; width=&quot;65%&quot; height=&quot;&quot; /&gt;




&lt;/div&gt;

&lt;p&gt;This line is not very far away from the parameters of the “true” function  p&lt;sub&gt;1&lt;/sub&gt;&lt;sup&gt;true&lt;/sup&gt;=0.00013 and p&lt;sub&gt;2&lt;/sub&gt;&lt;sup&gt;true&lt;/sup&gt;=70. Interestingly, for the learned function the slope p&lt;sub&gt;1&lt;/sub&gt; is a bit higher, but this is “compensated” by a lower shift p&lt;sub&gt;2&lt;/sub&gt;. Intuitively, this makes sense: the higher the slope, the more we need to shift the line downwards in order to approximately hit the training examples. We will see in a second why this observation is important.&lt;/p&gt;

&lt;h3 id=&quot;adding-dimensions&quot;&gt;Adding dimensions&lt;/h3&gt;

&lt;p&gt;Now, I would like to reason about the influence of the number of parameters of a function on the difficulty of learning that function. You can think about the parameters of the line, but in fact I will formulate it in a general way.&lt;/p&gt;

&lt;p&gt;For the sake of the argument let’s assume that we do not consider all possible numbers as possible parameter values, but that we restrict ourselves a fixed list of numbers (in mathematics that’s called &lt;em&gt;discretization&lt;/em&gt;), and we make this list finite. To make it really simple, we will only use the numbers from 1 to 10.&lt;/p&gt;

&lt;p&gt;Assume that we have a function that has only &lt;em&gt;one&lt;/em&gt; parameter (for example, only the slope of a line), we immediately see that there are 10 possible values that the parameter can take. We visualize each value of the parameter by a blue box:&lt;/p&gt;

&lt;table border=&quot;0&quot; style=&quot;border-collapse: collapse; margin: 0 0 15px 25px;&quot;&gt;
&lt;tr&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
1
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
2
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
3
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
4
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
5
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
6
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
7
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
8
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
9
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
10
&lt;/td&gt;

&lt;/tr&gt;
&lt;/table&gt;

&lt;p&gt;What happens if we add a second parameter that can take 10 values? Well, you might think that results in 10 values for parameter one and another 10 for parameter two, which makes 20. But unfortunately that’s wrong: we need to consider all possible combinations of the parameter values! The reason is that the parameters are usually dependent as we have seen in the line example: when changing parameter one (here the slope), we can still improve how well the line fits the data by changing parameter two (shift). Let’s visualize all possible combinations:&lt;/p&gt;

&lt;table border=&quot;0&quot; style=&quot;border-collapse: collapse; margin: 0 0 15px 25px;&quot;&gt;

&lt;tr&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(1, 1)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(1, 2)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(1, 3)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(1, 4)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(1, 5)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(1, 6)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(1, 7)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(1, 8)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(1, 9)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(1, 10)
&lt;/td&gt;

&lt;/tr&gt;

&lt;tr&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(2, 1)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(2, 2)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(2, 3)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(2, 4)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(2, 5)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(2, 6)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(2, 7)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(2, 8)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(2, 9)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(2, 10)
&lt;/td&gt;

&lt;/tr&gt;

&lt;tr&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(3, 1)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(3, 2)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(3, 3)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(3, 4)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(3, 5)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(3, 6)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(3, 7)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(3, 8)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(3, 9)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(3, 10)
&lt;/td&gt;

&lt;/tr&gt;

&lt;tr&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(4, 1)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(4, 2)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(4, 3)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(4, 4)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(4, 5)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(4, 6)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(4, 7)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(4, 8)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(4, 9)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(4, 10)
&lt;/td&gt;

&lt;/tr&gt;

&lt;tr&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(5, 1)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(5, 2)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(5, 3)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(5, 4)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(5, 5)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(5, 6)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(5, 7)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(5, 8)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(5, 9)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(5, 10)
&lt;/td&gt;

&lt;/tr&gt;

&lt;tr&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(6, 1)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(6, 2)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(6, 3)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(6, 4)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(6, 5)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(6, 6)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(6, 7)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(6, 8)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(6, 9)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(6, 10)
&lt;/td&gt;

&lt;/tr&gt;

&lt;tr&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(7, 1)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(7, 2)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(7, 3)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(7, 4)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(7, 5)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(7, 6)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(7, 7)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(7, 8)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(7, 9)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(7, 10)
&lt;/td&gt;

&lt;/tr&gt;

&lt;tr&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(8, 1)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(8, 2)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(8, 3)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(8, 4)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(8, 5)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(8, 6)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(8, 7)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(8, 8)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(8, 9)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(8, 10)
&lt;/td&gt;

&lt;/tr&gt;

&lt;tr&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(9, 1)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(9, 2)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(9, 3)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(9, 4)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(9, 5)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(9, 6)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(9, 7)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(9, 8)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(9, 9)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(9, 10)
&lt;/td&gt;

&lt;/tr&gt;

&lt;tr&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(10, 1)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(10, 2)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(10, 3)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(10, 4)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(10, 5)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(10, 6)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(10, 7)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(10, 8)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(10, 9)
&lt;/td&gt;

&lt;td style=&quot;width:40px; height:30px; border:1px solid blue; font-size: 8pt; color: blue; &quot; align=&quot;center&quot;&gt;
(10, 10)
&lt;/td&gt;

&lt;/tr&gt;

&lt;/table&gt;

&lt;p&gt;We see that we have 10 &lt;em&gt;times&lt;/em&gt; 10, i.e. 100 possible parameter pairs. What happens if we add a third parameter? We get a cube with 10 times 10 times 10 equals 1000 parameters! And with 10 parameters each taking 10 values, we get 10.000.000.000 which are 10 billion different combinations of parameter values!&lt;/p&gt;

&lt;p&gt;You probably see the formula behind our reasoning: If we have &lt;em&gt;n&lt;/em&gt; values a parameter can take, and &lt;em&gt;m&lt;/em&gt; parameters, we end up with &lt;em&gt;n&lt;/em&gt;&lt;sup&gt;&lt;em&gt;m&lt;/em&gt;&lt;/sup&gt; possible parameter value assignments. We say that the number of parameter values grows &lt;em&gt;exponentially&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Now how big of a problem is it? Well, it is very big indeed, which is why this problem is called &lt;em&gt;the curse of dimensionality&lt;/em&gt;. The problem is that data are usually high-dimensional, and that each parameter usually has significantly more than 10 possible values.&lt;/p&gt;

&lt;p&gt;For example, the tiny pictures we played around with in an &lt;a href=&quot;/intuitivemi/2015/07/25/vector-spaces.html&quot;&gt;earlier post&lt;/a&gt; had 27x35, that is 945 pixels. If we were to learn a function that has a parameter for every pixel and again every parameter can only take 1 out of 10 values we would still end up with 10&lt;sup&gt;945&lt;/sup&gt; parameter values - this is a number consisting of a 1 with 945 trailing zeros, and it is several orders of magnitudes higher than the &lt;a href=&quot;http://www.quora.com/How-many-particles-are-there-in-the-universe&quot;&gt;number of particles in the entire universe&lt;/a&gt;! We will never be able to try out even a tiny fraction of all possible parameter values.&lt;/p&gt;

&lt;p&gt;So we see that the curse of dimensionality forces us to find smarter ways of finding the parameters of functions. We will save this for later articles.&lt;/p&gt;

&lt;h3 id=&quot;hughes-effect&quot;&gt;Hughes effect&lt;/h3&gt;

&lt;p&gt;Irrespective of the way how smart we go about searching for parameters, high dimensionality has another very problematic (and somewhat unintuitive) implication, namely for classification. This problem is also known as the &lt;em&gt;Hughes effect&lt;/em&gt;.
I will only sketch the idea very briefly but you find a more elaborate explanation with nice illustrative figures &lt;a href=&quot;http://www.visiondummy.com/2014/04/curse-dimensionality-affect-classification/&quot;&gt;in this blog article&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The problem is stated as follows: recall that in classification, we aim to find a function that discriminates between two or more categories of input data. We presented a way of doing so, namely by finding &lt;a href=&quot;/intuitivemi/2015/07/25/vector-spaces.html&quot;&gt;hyperplanes in space&lt;/a&gt; that separate the categories. So how much easier or simpler does it get to find a hyperplane if the dimensionality of the data is higher?&lt;/p&gt;

&lt;p&gt;Here comes the problem: the more dimensions the data has, the &lt;em&gt;easier&lt;/em&gt; it is to find a hyperplane separating categories in the &lt;em&gt;training data&lt;/em&gt;; but at the same time, the &lt;em&gt;harder&lt;/em&gt; it gets to also perform well on the &lt;em&gt;unseen data&lt;/em&gt; (for example, &lt;em&gt;test data&lt;/em&gt;). The reason is that because we have more dimensions that we can choose from to lay the hyperplane through, we are much more prone to &lt;a href=&quot;/intuitivemi/2015/08/07/overfitting.html&quot;&gt;overfitting&lt;/a&gt;. Thus, having higher-dimensional data has the same effect as allowing our function to have more “wrinkles”. And the less training data we have, the less sure we are that we have identified the dimensions that really matter for discriminating between the categories.&lt;/p&gt;

&lt;!--
&lt;div class=&quot;pseudoformula&quot;&gt;
f(&lt;b&gt;Image&lt;/b&gt;) = 1	&amp;nbsp;&amp;nbsp;&amp;nbsp; if &lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;Image&lt;/b&gt;&lt;sub&gt;(1,1)&lt;/sub&gt; * 10  + &lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;Image&lt;/b&gt;&lt;sub&gt;(1,2)&lt;/sub&gt; * 1.1  + &lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; ... &lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;b&gt;Image&lt;/b&gt;&lt;sub&gt;(27,35)&lt;/sub&gt; * 2.5 &lt;br/&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;gt; 0
f(&lt;b&gt;Input&lt;/b&gt;) = 0	&amp;nbsp;&amp;nbsp;&amp;nbsp; otherwise
&lt;/div&gt;

### Real dimensionality of data
--&gt;

&lt;h3 id=&quot;summary&quot;&gt;Summary&lt;/h3&gt;

&lt;p&gt;In this post, we got to know another nemesis of data: high dimensionality. When looking at learning from the perspective of searching for the right parameters, each additional dimensionality means that we must search an exponential number of more parameter values. And in classification, every additional dimension makes it harder to find the right hyperplane to discriminate between the categories.&lt;/p&gt;

&lt;p&gt;But another curse is already on its way; and it has to do with (no) free lunch.&lt;/p&gt;

&lt;h3 id=&quot;tldr&quot;&gt;&lt;a href=&quot;http://de.urbandictionary.com/define.php?term=tl%3Bdr&quot;&gt;TL;DR&lt;/a&gt;:&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Curse of dimensionality refers to the dimensionality of the data and parameters to be learned&lt;/li&gt;
  &lt;li&gt;Searching for the right parameter values becomes exponentially harder with every dimension&lt;/li&gt;
  &lt;li&gt;Similarly, in classification every dimension makes us more prone to overfitting by choosing the wrong dimension as discriminatory&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;further-reading&quot;&gt;&lt;a name=&quot;further&quot;&gt;&lt;/a&gt;Further reading:&lt;/h3&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;a name=&quot;[1]&quot;&gt;&lt;/a&gt;Excellent &lt;a href=&quot;http://www.visiondummy.com/2014/04/curse-dimensionality-affect-classification/&quot;&gt;blog article&lt;/a&gt; by Vincent Spruyt on the curse of dimensionality in classification, aka the Hughes effect.&lt;/li&gt;
&lt;/ol&gt;
</description>
        <pubDate>Sun, 03 Jan 2016 13:45:00 +0000</pubDate>
        <link>http://shoefer.github.io/intuitivemi/2016/01/03/dimensionality.html</link>
        <guid isPermaLink="true">http://shoefer.github.io/intuitivemi/2016/01/03/dimensionality.html</guid>
        
        
        <category>intuitivemi</category>
        
      </item>
    
      <item>
        <title>Learning Functions from Data: A Primer</title>
        <description>&lt;p&gt;In the introductory articles we have learned that &lt;a href=&quot;/intuitivemi/2015/07/19/data-numbers-representations.html&quot;&gt;data is a bunch of numbers&lt;/a&gt; encoding some information, and that data can be multi-dimensional which makes them live in &lt;a href=&quot;/intuitivemi/2015/07/25/vector-spaces.html&quot;&gt;vector spaces&lt;/a&gt;. 
We have also looked at the core competence of machine intelligence: applying   &lt;a href=&quot;/intuitivemi/2015/12/28/functions.html&quot;&gt;functions&lt;/a&gt; to data. In this and the following posts we will look at the most powerful tool of machine intelligence: &lt;em&gt;learning functions from data&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The roadmap is as follows. In this article, we will understand why learning functions from data is in principle rather straightforward. Indeed, at the end of this article we will have a developed a very simple learning method.&lt;/p&gt;

&lt;p&gt;The next few posts will then bring us back down to earth and explain some fundamental problems that learning from data has, and discuss solutions to these problems. This will endow you with a powerful intuition of how learning from data works and what its limitations are.&lt;/p&gt;

&lt;!--
QUESTION: better explain by intuitive example, e.g. correlating the hypothesis that it is raining to the 
  hmm, but isn&apos;t that more about priors?
  --&gt;

&lt;h4 id=&quot;learning-functions-from-data&quot;&gt;Learning Functions from Data&lt;/h4&gt;

&lt;p&gt;In order to understand how learning from data works, let’s use as our running example the stock price prediction problem introduced &lt;a href=&quot;/intuitivemi/2015/12/28/functions.html&quot;&gt;earlier&lt;/a&gt;: given the annual revenue of a company, we want to predict the company’s stock price. We have learned that such a prediction is represented by a function. We discussed two representations of functions; first, tabular functions:&lt;/p&gt;

&lt;table class=&quot;data-table&quot;&gt;
&lt;tr&gt;
&lt;th&gt;&lt;i&gt;input&lt;/i&gt;: Annual Revenue in January (Euro)&lt;/th&gt;
&lt;th&gt;&lt;i&gt;output&lt;/i&gt;: Price (Euro)&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;40.000&lt;/td&gt;
&lt;td&gt;122&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;50.000&lt;/td&gt;
&lt;td&gt;135&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;60.000&lt;/td&gt;
&lt;td&gt;148&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;80.000&lt;/td&gt;
&lt;td&gt;174&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;100.000&lt;/td&gt;
&lt;td&gt;200&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;

&lt;p&gt;These functions had the drawback that we could not extrapolate from them, that is we do not know from this table what the stock price would be if, for example, the company’s revenue was 120.000 Euros per year.&lt;/p&gt;

&lt;p&gt;Therefore, we were interested in finding a different function representation, and I suggested to use linear functions, namely this one:&lt;/p&gt;

&lt;div class=&quot;pseudoformula&quot;&gt;
&lt;b&gt;Stock price&lt;/b&gt; = f(&lt;b&gt;Revenue&lt;/b&gt;) = 0.00013 * &lt;b&gt;Revenue&lt;/b&gt; + 70
&lt;/div&gt;

&lt;p&gt;I have told you that since the function is a &lt;em&gt;linear&lt;/em&gt; we can visualize it with a line:&lt;/p&gt;

&lt;div class=&quot;imgcenter imgcap&quot;&gt;




&lt;img src=&quot;/intuitivemi/images/2015-07-20-functions_sizeprice.png&quot; class=&quot;&quot; width=&quot;65%&quot; height=&quot;&quot; /&gt;




&lt;/div&gt;

&lt;p&gt;More importantly, the regularity enabled us to predict the stock price given any annual revenue. Intuitively, when the the revenue gets higher, the stock price moderately increases.&lt;/p&gt;

&lt;h4 id=&quot;how-to-extrapolate-from-data&quot;&gt;How to extrapolate from data?&lt;/h4&gt;

&lt;p&gt;By drawing the line through the bunch of dots, I actually did the task that the computer is supposed to do: I looked at the data, and I found out that there is a linear relationship between stock price and annual revenue. But what I really would like to do is to give the machine the data from the table above and let it figure out &lt;em&gt;on its own&lt;/em&gt; how to make stock price predictions into the future! How do we do that?&lt;/p&gt;

&lt;p&gt;Automating exactly this process is the core of &lt;em&gt;machine learning&lt;/em&gt;. In machine learning, you are given a set of &lt;em&gt;training examples&lt;/em&gt; - which correspond to the table given above (and are visualized by the blue &lt;em&gt;dots&lt;/em&gt;). 
These training examples are a bunch of data that need to be in the form &lt;em&gt;(input, output)&lt;/em&gt; where output is the true, known output for the given input. The computer should then spit out an appropriate function, namely the blue line.
Luckily, the stock price training data are exactly in the form (input, output). So how to learn from them?&lt;/p&gt;

&lt;p&gt;I will now show you the simplest learning procedure I could come with. And it is indeed &lt;em&gt;very&lt;/em&gt; simple! Intuitively, the computer will do the following: we tell the compute to use some line which should relate input and output, and the computer will then fiddle around with this line until it comes as close as possible to as many training examples as possible. That’s it!&lt;/p&gt;

&lt;p&gt;Problem solved?&lt;/p&gt;

&lt;h3 id=&quot;defining-the-learning-problem&quot;&gt;Defining the learning problem&lt;/h3&gt;

&lt;p&gt;Well, almost. If we would ask our computer to do that, it would reply with some of the following questions:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;What function should I find? How is it represented?&lt;/li&gt;
  &lt;li&gt;What does “as close as possible to as many training examples as possible” mean?&lt;/li&gt;
  &lt;li&gt;What does “fiddle around” mean?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So let’s give our eager computer some answers:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Start off with a single line. A line is represented by two basic &lt;em&gt;parameters&lt;/em&gt;: how much it is shifted up or down (the 70 in the example above), and its slope (the 0.00013).&lt;/li&gt;
  &lt;li&gt;Calculate the difference of every training example to the value predicted by the function the computer guesses. When we sum all of these differences up we obtain a single number which we call the &lt;em&gt;training error&lt;/em&gt;. The training error allows us to calculate how well any line fits the training examples.&lt;/li&gt;
  &lt;li&gt;Do random guessing; we randomly guess a line (more precisely, its &lt;em&gt;parameters&lt;/em&gt;) and see how well it fulfils the criterion defined in 2.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That sounds like a plan, doesn’t it?&lt;/p&gt;

&lt;p&gt;Let’s first get an intuition what the &lt;em&gt;training error&lt;/em&gt; looks like for some randomly guessed function:&lt;/p&gt;
&lt;div class=&quot;imgcenter imgcap&quot;&gt;




&lt;img src=&quot;/intuitivemi/images/2015-12-29-learning-random_guess_training_error.png&quot; class=&quot;&quot; width=&quot;65%&quot; height=&quot;&quot; /&gt;




&lt;/div&gt;

&lt;p&gt;Here, the blue dots are the training examples, the red curve is the random guess, the dotted lines indicate the discrepancy between the guessed line and the training data, and the numbers next to the lines indicate how big the discrepancy is. By summing over all  these differences, we obtain the red number at the bottom. This number is the training error - we want to get it as low as possible in order to find a highly predictive function.&lt;/p&gt;

&lt;p&gt;So how does random guessing perform? The following video shows what this looks like for 100 random guesses.  The green line is the one with the lowest training error so far. We see that it takes some guesses, but eventually we get a close match of the green line and the blue dots, the guess and the training data.&lt;/p&gt;

&lt;div class=&quot;imgcenter imgcap&quot;&gt;


&lt;img src=&quot;/intuitivemi/images/2015-12-29-learning-random_guess_animation.png&quot; id=&quot;learning-random-guess-animation&quot; class=&quot;&quot; width=&quot;500&quot; height=&quot;375&quot; /&gt;


&lt;/div&gt;

&lt;script&gt;
    $(&apos;#learning-random-guess-animation&apos;).gifplayer({label:&apos;play&apos;});
&lt;/script&gt;

&lt;div&gt;
&lt;img style=&quot;display:none; width:0px; height:0px; visibility:hidden; border:0; margin:0; padding:0;&quot; src=&quot;/intuitivemi/images/2015-12-29-learning-random_guess_animation.gif&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;Congratulations: you have witnessed your first machine learning algorithm &lt;a href=&quot;#[1]&quot;&gt;[1]&lt;/a&gt;!&lt;/p&gt;

&lt;h4 id=&quot;caveats&quot;&gt;Caveats&lt;/h4&gt;

&lt;p&gt;You might be a bit disappointed now. Random guessing? That’s machine intelligence? That doesn’t seem intelligent at all! Fair enough, that is the simplest thing you can do. 
And in practise, it won’t work very well. Yet, some of the most powerful algorithms do apply random guessing (which is officially called &lt;em&gt;random search&lt;/em&gt;) but combine it with some “tricks” to make the guessing smarter. So random search is an important building block for creating learning algorithms, although it’s not very powerful when used on its own.&lt;/p&gt;

&lt;p&gt;At this point, we can already think about why random search does not work so well on its own. Here are a couple of thoughts.&lt;/p&gt;

&lt;p&gt;First of all, you don’t now how big you should go. Should you shift the line only between -1 to +1, or from -1000 to +1000? Should you try out both 1.1 and 1.10001? That means, in which granularity should you vary the parameters? In fact, in the animation above, I have set the bounds of the parameters very tightly around the real (but in the usual case unknown) parameters. Otherwise, the search would have taken much longer.&lt;/p&gt;

&lt;p&gt;Second, we don’t learn from our mistakes. If I have guessed line 1 that got a training error of 1000. Next, I guessed line 2 with a training error of 800. Intuitively, I should try to find out what might line 2 better than line 1, and progress into that direction. Instead, random search will just look for a new line which might be much worse than both line 1 and line 2.&lt;/p&gt;

&lt;p&gt;Can you think of more issues? &lt;a href=&quot;#[2]&quot;&gt;[2]&lt;/a&gt;&lt;/p&gt;

&lt;h4 id=&quot;terminology&quot;&gt;Terminology&lt;/h4&gt;

&lt;p&gt;Before we close, a note on terminology. Some paragraphs ago, we had to specify three things for our learning algorithm to work. I want to quickly state how these things are called in machine intelligence.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;b&gt;Model&lt;/b&gt;, &lt;b&gt;function representation&lt;/b&gt; (sometimes also &lt;b&gt;hypothesis space&lt;/b&gt;): What kind of functions do I look for? &lt;br /&gt;(In the example: single straight lines)&lt;/li&gt;
  &lt;li&gt;&lt;b&gt;Objective&lt;/b&gt; or &lt;b&gt;loss function&lt;/b&gt; &lt;a href=&quot;#[3]&quot;&gt;[3]&lt;/a&gt;: Criterion for evaluating whether the learned function is good. Often this is the &lt;b&gt;training error&lt;/b&gt;, a measure of how well the learned function accounts for the training data. &lt;br /&gt;(In the example: distance between samples and function)&lt;/li&gt;
  &lt;li&gt;&lt;b&gt;Learning algorithm&lt;/b&gt;: In which way to come up with functions (or parameters) in order to get a good value for the loss function.&lt;br /&gt;(In the example: random search)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Moreover, if you learn from training data of the form (input, output), the approach is called &lt;em&gt;supervised learning&lt;/em&gt;. The picture is that the pupil comes up with different inputs, and a supervisor gives the pupil a hint by telling her the right output, and the pupil tries to figure out the regularity (aka function) from these hints.&lt;/p&gt;

&lt;p&gt;Finally, there are two general types of supervised learning, which depend on the type of function you want to learn. If you learn to map an input to a continuous number (as done in the stock price example), the learning task is called a &lt;em&gt;regression task&lt;/em&gt;; in the last post we called this fitting a line &lt;em&gt;through&lt;/em&gt; the data. In contrast, if you map the input to a category (as in the &lt;a href=&quot;/intuitivemi/2015/07/19/data-numbers-representations.html&quot;&gt;image classification example&lt;/a&gt;), it is called a &lt;em&gt;classification task&lt;/em&gt;. We solve this class by finding a (hyper)plane or line that &lt;em&gt;separates&lt;/em&gt; the data.&lt;/p&gt;

&lt;p&gt;In the next post, we will critically assess what we have done, and find out that there is a fundamental issue in learning from data which we have neglected so far. It is coarsely related to the question: why did we actually use a straight line, and not something with the shape of a curve or a snake?&lt;/p&gt;

&lt;h3 id=&quot;tldr&quot;&gt;&lt;a href=&quot;http://de.urbandictionary.com/define.php?term=tl%3Bdr&quot;&gt;TL;DR&lt;/a&gt;:&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Machine intelligence learns functions from data&lt;/li&gt;
  &lt;li&gt;Data are given as training examples, that is input-output pairs&lt;/li&gt;
  &lt;li&gt;This type of learning is called supervised learning&lt;/li&gt;
  &lt;li&gt;To allow a compute to learn, we have to define model, objective and a learning algorithm&lt;/li&gt;
  &lt;li&gt;Random guessing is a learning algorithm that works for very simple problems&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;footnotes&quot;&gt;&lt;a name=&quot;further&quot;&gt;&lt;/a&gt;Footnotes:&lt;/h3&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;a name=&quot;[1]&quot;&gt;&lt;/a&gt;An &lt;a href=&quot;https://en.wikipedia.org/wiki/Algorithm&quot;&gt;algorithm&lt;/a&gt; is the machine intelligence people name for “recipe”. It is a list of steps that you have to execute in a certain order; it has to be detailed enough so that the machine has no doubt about what it has to do.&lt;/li&gt;
  &lt;li&gt;&lt;a name=&quot;[2]&quot;&gt;&lt;/a&gt;Think, for example, what happens if your function has more parameters, let’s say twice as many. How much harder does the learning problem then get?&lt;/li&gt;
  &lt;li&gt;&lt;a name=&quot;[3]&quot;&gt;&lt;/a&gt;Note the term loss &lt;em&gt;function&lt;/em&gt;. The criterion itself is again formulated as a function - but this is &lt;em&gt;not&lt;/em&gt; the function want to learn! It is an auxiliary function that takes as input three arguments: a set of (1) inputs and (2) outputs of all the training data and (3) the corresponding outputs of the learned function. It outputs a single number, assessing the quality of the learned function for the training data. In contrast, the learned function takes only one input, namely the one it requires to predict the output.&lt;/li&gt;
&lt;/ol&gt;
</description>
        <pubDate>Tue, 29 Dec 2015 23:00:00 +0000</pubDate>
        <link>http://shoefer.github.io/intuitivemi/2015/12/29/learning-functions.html</link>
        <guid isPermaLink="true">http://shoefer.github.io/intuitivemi/2015/12/29/learning-functions.html</guid>
        
        <category>machine</category>
        
        <category>learning,</category>
        
        <category>statistical</category>
        
        <category>learning</category>
        
        
        <category>intuitivemi</category>
        
      </item>
    
      <item>
        <title>Functions as Data Translators</title>
        <description>&lt;p&gt;If you have read the previous posts carefully you should now be familiar with high-dimensional data. In this last introductory article we are now going to look at what machine intelligence people mean when they think about manipulating data: they apply &lt;em&gt;functions&lt;/em&gt; to data.&lt;/p&gt;

&lt;p&gt;We will learn what functions are and look at different examples. Once we know what they are, we can in the next post see how to learn them - which is essentially what learning machines do.&lt;/p&gt;

&lt;h2 id=&quot;mathematical-functions&quot;&gt;Mathematical Functions&lt;/h2&gt;

&lt;p&gt;Mathematicians use the term function rather differently from the common sense definition. In everyday language we talk about the function of things in the sense “what is the purpose of the thing” or “how does this thing work”. A function in mathematical sense is rather different. It can be best thought of as a translation from one type of data to another type. Or in other words, you give some input (data) to the function, and get some output (data):&lt;/p&gt;

&lt;div class=&quot;imgcenter imgcap&quot;&gt;




&lt;img src=&quot;https://upload.wikimedia.org/wikipedia/commons/3/3b/Function_machine2.svg&quot; class=&quot;&quot; width=&quot;35%&quot; height=&quot;&quot; /&gt;




&lt;/div&gt;

&lt;p&gt;(This figure and the first example are taken from the highly recommended &lt;a href=&quot;https://en.wikipedia.org/wiki/Function_(mathematics)&quot;&gt;Wikipedia page on functions&lt;/a&gt;)&lt;/p&gt;

&lt;p&gt;Let me give you a couple of examples of these things called functions.&lt;/p&gt;

&lt;h4 id=&quot;object-description-functions&quot;&gt;Object description functions&lt;/h4&gt;

&lt;p&gt;Let’s assume a couple of primitive shapes, such as triangles, squares, etc. all of different color. We can now define a function that, given a shape, outputs the color of the function:&lt;/p&gt;

&lt;div class=&quot;imgcenter imgcap&quot;&gt;




&lt;img src=&quot;/intuitivemi/images/2015-07-20-functions-color_example_wp.svg&quot; class=&quot;&quot; width=&quot;35%&quot; height=&quot;&quot; /&gt;




&lt;/div&gt;

&lt;p&gt;This visualization shows how to map &lt;em&gt;input data&lt;/em&gt; X (the shape) to some &lt;em&gt;output&lt;/em&gt;, the shape’s color Y. All the arrows between X and Y are the defining elements of the function.&lt;/p&gt;

&lt;p&gt;Note that for each object in X there is only one arrow pointing away from it. This is indeed a requirement because we want a function to be a unique mapping: for each input we get exactly one output. The inverse is not required, though: the square and the triangle have the same color, so the red color in Y has two arrows pointing to it. You also notice that some colors, namely blue and purple have no arrows pointing to it. All of these things are very common in functions.&lt;/p&gt;

&lt;!--
We can also come up with a whole bunch of other functions, defined on shapes: for example, we could define functions that counts the edges of each shape; or a function that calculates the area of the shape, and so on.
--&gt;

&lt;!-- You see, many concepts and things can be cast as function. However, 
--&gt;
&lt;p&gt;So far we have only talked about functions conceptually but we have not stated how a function &lt;em&gt;can automatically compute&lt;/em&gt; the output from the input. Let us therefore look at a simpler example to shed some light on this.&lt;/p&gt;

&lt;h4 id=&quot;stock-price-prediction-function&quot;&gt;Stock price prediction function&lt;/h4&gt;

&lt;p&gt;Let us now re-introduce our simple example from the &lt;a href=&quot;/intuitivemi/2015/07/28/datascience-showoff.html&quot;&gt;introductory post&lt;/a&gt;. We want to assess the stock price of some company. For the sake of simplicity, we will only use information about the annual revenue of the company (in Euro) and try to predict the stock price (also in Euro). We can describe this relationship in a big table:&lt;/p&gt;

&lt;table class=&quot;data-table&quot;&gt;
&lt;tr&gt;
&lt;th style=&quot;color: gray&quot;&gt;Year&lt;/th&gt;
&lt;th&gt;Annual Revenue in January (Euro)&lt;/th&gt;
&lt;th&gt;Price (Euro)&lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;color: gray&quot;&gt;2010&lt;/td&gt;
&lt;td&gt;40.000&lt;/td&gt;
&lt;td&gt;122&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;color: gray&quot;&gt;2011&lt;/td&gt;
&lt;td&gt;50.000&lt;/td&gt;
&lt;td&gt;135&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;color: gray&quot;&gt;2012&lt;/td&gt;
&lt;td&gt;60.000&lt;/td&gt;
&lt;td&gt;148&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;color: gray&quot;&gt;2013&lt;/td&gt;
&lt;td&gt;80.000&lt;/td&gt;
&lt;td&gt;174&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td style=&quot;color: gray&quot;&gt;2014&lt;/td&gt;
&lt;td&gt;100.000&lt;/td&gt;
&lt;td&gt;200&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;

&lt;p&gt;This table can be considered a function in at least three ways: either we use the year as input, we use the annual revenue as input, or we use the stock price as input. We choose the annual revenue as the input and the stock price as the output because we believe that the annual revenue is more predictive for the stock price than the year - if the company was founded 10 years earlier, we would still expect the relationship of annual revenue and stock price to be similar (unless there was something like a global crisis, but we ignore that for now).&lt;/p&gt;

&lt;p&gt;So the input of our function is &lt;em&gt;annual revenue&lt;/em&gt; and the output &lt;em&gt;stock price&lt;/em&gt;. And “computing” this function is very simple: given a number for the annual revenue, we look up the row in the table containing this number and return the corresponding stock price.&lt;/p&gt;

&lt;p&gt;Such tabular functions are common but they have a severe drawback: we cannot &lt;em&gt;extrapolate&lt;/em&gt; - from this table alone we do not know how to get the value for the stock price for an annual revenue of 90.000 Euros or 200.000 Euros! 
However, the example data I have given exhibits a regularity (surprise, surprise!), namely that the stock price values in the right column are exactly 0.00013 times the annual revenue plus 70 (check this for yourself). This gives us a much more concise way of describing this function:&lt;/p&gt;

&lt;div class=&quot;pseudoformula&quot;&gt;
&lt;b&gt;Stock price&lt;/b&gt; = 0.00013 * &lt;b&gt;Revenue&lt;/b&gt; + 70
&lt;/div&gt;

&lt;p&gt;You see that the function gets as input the revenue, and gives as output the stock price. To make this even clearer, we usually denote the function by the symbol &lt;em&gt;f&lt;/em&gt; and write:&lt;/p&gt;

&lt;div class=&quot;pseudoformula&quot;&gt;
f(&lt;b&gt;Revenue&lt;/b&gt;) = 0.00013 * &lt;b&gt;Revenue&lt;/b&gt; + 70
&lt;/div&gt;

&lt;p&gt;The parentheses behind the &lt;em&gt;f&lt;/em&gt; contain the inputs of the function, sometimes called &lt;em&gt;arguments&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Let us visualize this function by plotting a graph which has on one axis (the &lt;em&gt;x-axis&lt;/em&gt;) the annual revenue and on the other (the &lt;em&gt;y-axis&lt;/em&gt;) the stock price:&lt;/p&gt;

&lt;div class=&quot;imgcenter imgcap&quot;&gt;




&lt;img src=&quot;/intuitivemi/images/2015-07-20-functions_sizeprice.png&quot; class=&quot;&quot; width=&quot;65%&quot; height=&quot;&quot; /&gt;




&lt;/div&gt;

&lt;p&gt;We see two interesting things here: first, we can now &lt;em&gt;predict&lt;/em&gt; the stock price value from the annual revenue, no matter which revenue. Secondly, the relationship between revenue and stock price in our example turns out to be a line. The fact that such relationships / functions can be drawn by a line results in them being called &lt;em&gt;linear functions&lt;/em&gt;. Linear functions are amongst the most important types of relationships in mathematics - and actually they are one of the few that mathematics can really deal with &lt;a href=&quot;#[2]&quot;&gt;[2]&lt;/a&gt;. Therefore, the majority of methods in machine intelligence are based on linear functions as the one given here. 
&lt;!-- We will talk about them in more detail in the next article. --&gt;&lt;/p&gt;

&lt;!--
##### Truthfulness of a function

In the previous paragraph, I have come up with a function to predict the stock price, by postulating a linear function that relates annual revenue to stock price. Now the question is: Who guarantees this function is actually &quot;true&quot; in the real world? 

To be honest:  *no one* can guarantee you that! I have come up with this function because I saw the regularity in the data; but  I might be totally off. Maybe the stock price drops at 1100000 Euros annual revenue? 
--&gt;

&lt;p&gt;In this example, we have talked about functions that takes a single number as input, and outputs another single number. Let’s now look how functions can be applied to vectors.&lt;/p&gt;

&lt;h4 id=&quot;vectorial-functions&quot;&gt;Vectorial functions&lt;/h4&gt;

&lt;p&gt;In the &lt;a href=&quot;/intuitivemi/2015/07/25/vector-spaces.html&quot;&gt;previous post&lt;/a&gt; we have dealt with the question of representing high-dimensional images as vectors. Of course, we can also define functions on these vectors. Recall that a vector is merely a list of numbers of fixed size, e.g. a 3-dimensional vector looking like this:&lt;/p&gt;

&lt;table class=&quot;data-table&quot;&gt;
&lt;tr&gt;
&lt;td style=&quot;background-color: #000; opacity: 0.909; width: 30px&quot;&gt;0.909&lt;/td&gt;
&lt;td style=&quot;background-color: #000; opacity: 1.0; width: 30px&quot;&gt;1.000&lt;/td&gt;
&lt;td style=&quot;background-color: #000; opacity: 0.860; width: 30px&quot;&gt;0.860&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;

&lt;p&gt;So let’s define a &lt;em&gt;linear&lt;/em&gt; function on 3-dimensional &lt;em&gt;vectors&lt;/em&gt;:&lt;/p&gt;

&lt;div class=&quot;pseudoformula&quot;&gt;
f&lt;sub&gt;a&lt;/sub&gt;(&lt;b&gt;Image&lt;/b&gt;) = f(&lt;b&gt;Image&lt;/b&gt;&lt;sub&gt;1&lt;/sub&gt;, &lt;b&gt;Image&lt;/b&gt;&lt;sub&gt;2&lt;/sub&gt;, &lt;b&gt;Image&lt;/b&gt;&lt;sub&gt;3&lt;/sub&gt;) = 2*&lt;b&gt;Image&lt;/b&gt;&lt;sub&gt;1&lt;/sub&gt; + 5*&lt;b&gt;Image&lt;/b&gt;&lt;sub&gt;2&lt;/sub&gt; - 1* &lt;b&gt;Image&lt;/b&gt;&lt;sub&gt;3&lt;/sub&gt;
&lt;/div&gt;

&lt;p&gt;What does this function do? With the little subscript we denote the individual dimensions of the input image. The function therefore computes the sum of the individual dimensions of the input vector, each dimension multiplied with some number. These numbers (here 2, 5 and -1, which I have chosen arbitrarily in this example) are called the &lt;em&gt;parameters&lt;/em&gt; of a function. The result for this function applied to the example vector above is:&lt;/p&gt;

&lt;div class=&quot;pseudoformula&quot;&gt;
f&lt;sub&gt;a&lt;/sub&gt;(&lt;b&gt;Image&lt;/b&gt;) = 2*0.909 + 5*1.0 - 1*0.86 = 5.958
&lt;/div&gt;

&lt;p&gt;At first sight this function does not really seem to make much sense. Why should we sum up pixel values of an image? For example, it allows us draw some conclusions on whether the image is rather dark (low value of f&lt;sub&gt;a&lt;/sub&gt;) or light (high value); and the different parameters of f&lt;sub&gt;a&lt;/sub&gt; allow us to emphasize certain regions of the image more than others. 
We will see later that this is actually very useful for recognizing things in images.
&lt;!--
But wait a second: we have as many parameters (2, 5 and 1) as input dimensions. This means, if the input data is an image, the parameters are in principle also an image! If you now think of input that are real 945-dimensional images, the parameters of the function can be used to &quot;weigh&quot; certain areas of the images higher. If you don&apos;t quite see this now, don&apos;t worry, we will talk about this later.
--&gt;&lt;/p&gt;

&lt;p&gt;You might have noticed that the previous function mapped a vectorial input to a single number. But we can also define functions that map vectors to vectors:&lt;/p&gt;

&lt;div class=&quot;pseudoformula&quot;&gt;
f&lt;sub&gt;b&lt;/sub&gt;(&lt;b&gt;Image&lt;/b&gt;)&amp;lt;/b&amp;gt; = [ &amp;nbsp;&amp;nbsp;&amp;nbsp; 2*&lt;b&gt;Image&lt;/b&gt;&lt;sub&gt;1&lt;/sub&gt; + 4*&lt;b&gt;Image&lt;/b&gt;&lt;sub&gt;2&lt;/sub&gt;,
	&amp;nbsp;&amp;nbsp;&amp;nbsp; 3*&lt;b&gt;Image&lt;/b&gt;&lt;sub&gt;2&lt;/sub&gt; + 1*&lt;b&gt;Image&lt;/b&gt;&lt;sub&gt;3&lt;/sub&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;]
&lt;/div&gt;

&lt;p&gt;This function maps the 3-dimensional input vector to a &lt;em&gt;2-dimensional&lt;/em&gt; output vector by summing over subparts of the input (the brackets indicate that the output is a vector, and the comma separates the two output dimensions). These types of functions will be very useful as they allow us to transform data into different representations, for example lower-dimensional ones.&lt;/p&gt;

&lt;h4 id=&quot;image-classification-function&quot;&gt;Image classification function&lt;/h4&gt;

&lt;p&gt;I will now introduce a last type of functions which I call decision functions or &lt;em&gt;classification functions&lt;/em&gt;. In a nutshell, these functions look like that:&lt;/p&gt;

&lt;div class=&quot;pseudoformula&quot;&gt;
f&lt;sub&gt;c&lt;/sub&gt;(&lt;b&gt;Input&lt;/b&gt;) = 1	&amp;nbsp;&amp;nbsp;&amp;nbsp; If &lt;b&gt;Input&lt;/b&gt; &amp;gt; 10  &lt;br /&gt;
f&lt;sub&gt;c&lt;/sub&gt;(&lt;b&gt;Input&lt;/b&gt;) = 0	&amp;nbsp;&amp;nbsp;&amp;nbsp; otherwise
&lt;/div&gt;

&lt;p&gt;Classification functions map input data to 2 (or more) categories which we simply enumerate from 0 to the number of categories (minus one).&lt;/p&gt;

&lt;p&gt;If we put together classification functions with our knowledge about vectorial functions, we can reconsider the example in the &lt;a href=&quot;/intuitivemi/2015/07/25/vector-spaces.html&quot;&gt;previous post&lt;/a&gt; we have seen that we can draw (hyper)planes to separate two categories of objects, namely blobfish and Sebastians:&lt;/p&gt;

&lt;div class=&quot;imgcenter imgcap&quot;&gt;


&lt;img src=&quot;/intuitivemi/images/2015-07-21-vector_spaces-arrow-plane.png&quot; id=&quot;vector-spaces-arrow-plane&quot; class=&quot;&quot; width=&quot;500&quot; height=&quot;375&quot; /&gt;


&lt;/div&gt;

&lt;script&gt;
    $(&apos;#vector-spaces-arrow-plane&apos;).gifplayer({label:&apos;play&apos;});
&lt;/script&gt;

&lt;div&gt;
&lt;img style=&quot;display:none; width:0px; height:0px; visibility:hidden; border:0; margin:0; padding:0;&quot; src=&quot;/intuitivemi/images/2015-07-21-vector_spaces-arrow-plane.gif&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;We would now assign the category Sebastian to 0 and blobfish to 1, and make the if-else-part of f&lt;sub&gt;c&lt;/sub&gt; such that it takes into account whether an input sample lies on one or the other side of the line. I will not write that out explicitly, but in fact the left-of-or-right-of-line can also be cast as a multiplication of the input with a bunch of numbers. So classification functions are exactly what we want if we want to solve classification tasks - surprised?&lt;/p&gt;

&lt;h4 id=&quot;relationship-between-functions&quot;&gt;Relationship between functions&lt;/h4&gt;

&lt;p&gt;Before closing this article, I would like to point out the relationship between the stock price and the image classification function.&lt;/p&gt;

&lt;p&gt;For image classification, the trick for visualizing the hyperplane that separated Sebastians from blobfish was to shrink the 27x35 images to a 3x1 image which allowed us to treat images as points (vectors) in 3D space.
What does this look like if we shrink the images even further, namely to a 2x1 image? We can then visualize the images as vectors in 2D. Similar as before we can now ask how to separate the shrinked Sebastians and blobfish. The answer is that we have to find a “2D hyperplane” - which turns out to be just a line!&lt;/p&gt;

&lt;div class=&quot;imgcenter imgcap&quot;&gt;




&lt;img src=&quot;/intuitivemi/images/2015-07-20-functions_image_class_2d.png&quot; id=&quot;2015-07-20-functions_image_class_2d&quot; class=&quot;&quot; width=&quot;65%&quot; height=&quot;&quot; /&gt;




&lt;/div&gt;

&lt;p&gt;Interestingly, this looks very much like the stock price prediction above. The main difference is that we do not draw the line &lt;em&gt;through&lt;/em&gt; the data but in such a way to &lt;em&gt;separate&lt;/em&gt; the data. Still, in both applications we use a &lt;em&gt;linear&lt;/em&gt; function.&lt;/p&gt;

&lt;!--
Therefore, we can down write the Sebastian-vs.-Blobfish discriminator in the following way:
&lt;div class=&quot;pseudoformula&quot;&gt;
&lt;b&gt;Category = &lt;i&gt;Sebastian&lt;/i&gt;&lt;/b&gt; &amp;nbsp; &lt;b&gt;IF&lt;/b&gt; { -0.9 * &lt;b&gt;gray value 1&lt;/b&gt; + 0.00013 * &lt;b&gt;gray value 2&lt;/b&gt; + 1 } &lt;b&gt; &amp;gt; x &lt;/b&gt;&lt;br/&gt;
&lt;b&gt;Category = &lt;i&gt;Blobfish&lt;/i&gt;&lt;/b&gt; &amp;nbsp; &lt;b&gt;IF&lt;/b&gt; { -0.1 * &lt;b&gt;gray value 1&lt;/b&gt; + 0.00013 * &lt;b&gt;gray value 2&lt;/b&gt; + 1 } &lt;b&gt; &amp;lt; x &lt;/b&gt;&lt;br/&gt;
&lt;/div&gt;
The expression in 
The two expressions only differ with respect to the &amp;gt; or &amp;lt; after the linear expression.
--&gt;

&lt;p&gt;Maybe it’s now a bit clearer how these linear functions translate into higher-dimensional spaces: the plane is a generalization of the line to 3D, and the hyperplane a generalization of the line/plane to higher dimensions! So in the future if we think about discriminating hyperplanes, we will usually visualize this as a line or a plane separating two sets of 2D or 3D points. And as before the mathematical field of &lt;a href=&quot;https://en.wikipedia.org/wiki/Linear_algebra&quot;&gt;linear algebra&lt;/a&gt; describes how to deal with these lines, planes and hyperplanes.&lt;/p&gt;

&lt;h4 id=&quot;summary&quot;&gt;Summary&lt;/h4&gt;

&lt;p&gt;For now, you should have gotten a feeling for what functions do: translating input to output data. Functions can take different inputs and return different outputs, such as numbers and vectors. (In fact, they can even take other functions as inputs as well! But we won’t bother with these insane cases now.)&lt;/p&gt;

&lt;p&gt;In the next post, we will get to the real meat: how to learn functions automatically.&lt;/p&gt;

&lt;h3 id=&quot;tldr&quot;&gt;&lt;a href=&quot;http://de.urbandictionary.com/define.php?term=tl%3Bdr&quot;&gt;TL;DR&lt;/a&gt;:&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Functions in the mathematical sense translate an input to an output&lt;/li&gt;
  &lt;li&gt;The simplest and best understood functions are linear functions&lt;/li&gt;
  &lt;li&gt;Functions map between numbers, or more general, vectors.&lt;/li&gt;
  &lt;li&gt;Functions can map to single numbers, but also to vector&lt;/li&gt;
  &lt;li&gt;If functions map onto a limited set of categories/classes, we call them classification functions&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;footnotes&quot;&gt;&lt;a name=&quot;further&quot;&gt;&lt;/a&gt;Footnotes:&lt;/h3&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;a name=&quot;[1]&quot;&gt;&lt;/a&gt;Of course this is largely oversimplified. The actual price would also depend on the size of the company at the time, economic health of the country, stock market crashes, etc. But for the sake of the example let’s assume none of these factors plays a role.&lt;/li&gt;
  &lt;li&gt;&lt;a name=&quot;[2]&quot;&gt;&lt;/a&gt;To cite the &lt;a href=&quot;http://books.google.de/books?id=dUhMAQAAQBAJ&amp;amp;pg=PA182&amp;amp;lpg=PA182&amp;amp;dq=Classical+mathematics+concentrated+on+linear+equations+for+a+sound+pragmatic+reason:+it+could+not+solve+anything+else.&amp;amp;source=bl&amp;amp;ots=PuRT666z3D&amp;amp;sig=YBZtoUP_y0siL0RUXfC14keMGe4&amp;amp;hl=de&amp;amp;sa=X&amp;amp;ei=upteVPDfBIysPJChgZgE&amp;amp;ved=0CCsQ6AEwAQ#v=onepage&amp;amp;q=Classical%20mathematics%20concentrated%20on%20linear%20equations%20for%20a%20sound%20pragmatic%20reason%3A%20it%20could%20not%20solve%20anything%20else.&amp;amp;f=false&quot;&gt;mathematician Ian Stewart&lt;/a&gt;: “Classical mathematics concentrated on linear equations for a sound pragmatic reason: it could not solve anything else.”&lt;/li&gt;
&lt;/ol&gt;

&lt;!--
3. &lt;a name=&quot;[3]&quot;&gt;&lt;/a&gt;You might remember that in the post on [vector spaces](intuitivemi/2015/07/25/vector-spaces) we mentioned that *linear algebra* is the mathematical discipline taking care of moving or rotating vectors. It is not a coincidence that the term *linear* appears in the title of this field: every possible movement or rotation of objects can be formalized by exactly the same kind of linear functions that we have seen above! Only that they are defined on vectors not on numbers. 
4. &lt;a name=&quot;[4]&quot;&gt;&lt;/a&gt; Indeed, finding a function for the image captioning problem is a very difficult task. Only recently, a [very complicated type of functions](http://karpathy.github.io/2015/05/21/rnn-effectiveness/) has been found which shows promising performance at this task. These functions - called recurrent neural networks - are actually not that different from the linear functions we have just learned above, and we will hopefully cover them at a later point.
--&gt;
</description>
        <pubDate>Mon, 28 Dec 2015 18:10:00 +0000</pubDate>
        <link>http://shoefer.github.io/intuitivemi/2015/12/28/functions.html</link>
        <guid isPermaLink="true">http://shoefer.github.io/intuitivemi/2015/12/28/functions.html</guid>
        
        
        <category>intuitivemi</category>
        
      </item>
    
      <item>
        <title>Stop Teaching Math in School?</title>
        <description>&lt;p&gt;Today, I watched the &lt;a href=&quot;https://www.youtube.com/watch?v=xyowJZxrtbg&quot;&gt;TEDx talk by John Bennett&lt;/a&gt; who advocates that we should stop teaching math to middle and high school students. 
I find it great if people make controversial claims and defend them argumentatively. And indeed, there seems to be a problem with how math is taught in school, given the infamous “math anxiety” that Bennett is alluding to. Yet, I strongly disagree with Bennett’s conclusion.&lt;/p&gt;

&lt;p&gt;In his talk, he tells us that at some point he realized that 99% of the American population don’t need math. Therefore, you shouldn’t teach math to them. He also rejects the argument that you need math in school at least to get a high SAT score - which is important for getting a good job. He aims to disprove this argument by citing Alfie Kohn with the following quote:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;The SAT is a measure of resources more than of reasoning. Year after year, College Board’s own statics depict a virtually linear correlation between SAT scores and family income.”&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Whether or not you buy the argument, my question is: how are you going to understand this argument and this quote, if you don’t know what a &lt;em&gt;linear correlation&lt;/em&gt; is - which you only get with a basic education in higher math?
Even worse, how are you going to understand other claims based on correlations unless you understand the relationship between correlation and causation? Are you going to understand that any argument based on a &lt;a href=&quot;http://www.tylervigen.com/spurious-correlations&quot;&gt;single correlation&lt;/a&gt; is prone to be false?&lt;/p&gt;

&lt;p&gt;There is a deeper point that I want to make here. In my opinion, Bennett misconceives education as &lt;em&gt;preparation for professional life&lt;/em&gt;. Of course, if you are not becoming an engineer, you might never see an integral sign again in your life. However, education is a &lt;em&gt;preparation for being a member of the society&lt;/em&gt; - you should be able to critically read the newspaper, question claims and lines of argumentation, which are nowadays often backed  up by some sort of statistics.
And sound argumentation, sharp thinking and - obviously - statistics are at the very core of math. All of this is required because you have the right to vote for your leaders - if  you are well-informed and able to make up your own opinion on valid logical ground, you are less likely to fall for populists and propaganda.&lt;/p&gt;

&lt;p&gt;I do agree that my math curriculum did not do a great job at teaching me all of the this - but it should. We most definitely should change the way math is taught, and there are various ideas of doing so (I have &lt;a href=&quot;/intuitivemi/2015/07/19/why-no-formulas.html&quot;&gt;some thoughts&lt;/a&gt; about what is going wrong in university courses, and &lt;a href=&quot;https://www.youtube.com/watch?v=fu-gFkuls_c&quot;&gt;some people&lt;/a&gt; have ideas on how to fix them). However, dropping maths from the curriculum does not sound like a viable solution to me. 
The number of people supporting &lt;a href=&quot;http://www.der-postillon.com/2016/11/erster-clown.html&quot;&gt;clowns&lt;/a&gt; like Donald Trump shows that we have an education problem. Let’s try to make education better, and let’s make teaching better math part of this endeavour.&lt;/p&gt;

</description>
        <pubDate>Mon, 28 Dec 2015 13:43:00 +0000</pubDate>
        <link>http://shoefer.github.io/education/2015/12/28/stop-teaching-math.html</link>
        <guid isPermaLink="true">http://shoefer.github.io/education/2015/12/28/stop-teaching-math.html</guid>
        
        <category>education,</category>
        
        <category>math</category>
        
        
        <category>education</category>
        
      </item>
    
      <item>
        <title>The Curse of Overfitting</title>
        <description>&lt;p&gt;In the last post we obtained an understanding of how to &lt;a href=&quot;/intuitivemi/2015/12/30/learning-functions.html&quot;&gt;learn functions from data&lt;/a&gt;, and we developed our first learning method. In this post, we will start building an understanding of why learning from data is actually pretty hard. The first problem we are facing is &lt;em&gt;the curse of overfitting&lt;/em&gt;. Let’s see what that is.&lt;/p&gt;

&lt;!--
QUESTION: better explain by intuitive example, e.g. correlating the hypothesis that it is raining to the 
  hmm, but isn&apos;t that more about priors?
  --&gt;

&lt;p&gt;Recap again the  &lt;a href=&quot;/intuitivemi/2015/12/28/functions.html&quot;&gt;stock price prediction problem&lt;/a&gt;: given the annual revenue of a company, we want to predict the company’s stock price. I have given you set of example data and we have found the following &lt;em&gt;linear&lt;/em&gt; regularity in the data:&lt;/p&gt;

&lt;div class=&quot;imgcenter imgcap&quot;&gt;




&lt;img src=&quot;/intuitivemi/images/2015-07-20-functions_sizeprice.png&quot; class=&quot;&quot; width=&quot;65%&quot; height=&quot;&quot; /&gt;




&lt;/div&gt;

&lt;p&gt;Alhough I told you that these &lt;em&gt;linear&lt;/em&gt; functions are really cool and mathematics can cope with them quite well you have probably already thought of many cases where linear functions do not suffice and we need different ones. And indeed, even in the stock price problem, there seems to be no reason for disregarding different functions, for example this one (red curve):&lt;/p&gt;

&lt;div class=&quot;imgcenter imgcap&quot;&gt;




&lt;img src=&quot;/intuitivemi/images/2015-08-07-overfitting-sizeprice.png&quot; class=&quot;&quot; width=&quot;65%&quot; height=&quot;&quot; /&gt;




&lt;/div&gt;

&lt;p&gt;It is not as nice as the line, but it perfectly passes through all of the data points. And
from an economic perspective this function looks much better as it predicts that if annual revenues exceed 1000000 Euro the stock price will go up much more quickly! So better go and buy some shares now!&lt;/p&gt;

&lt;h3 id=&quot;occams-razor&quot;&gt;Occam’s Razor&lt;/h3&gt;

&lt;p&gt;Let me give you a reason in defense of the linear function: the function’s simplicity. Although both functions perfectly fit the data (both the blue line and the red curve perfectly coincide with the blue dots) the line has much less wrinkles than the curve - namely zero rather than six! For the line we do not have to choose whether the wrinkles go up or down, how high/low they should go etc.&lt;/p&gt;

&lt;p&gt;In fact, this type of reasoning is so common that it has already been brought up over 700 years ago by William of Ockham. Therefore, this principle is called &lt;a href=&quot;https://en.wikipedia.org/wiki/Occam%27s_razor&quot;&gt;Occam’s razor&lt;/a&gt; (although Ockham wasn’t the first to bring it up, nor was he known for being particularly passionate about shaving). It states that “among competing hypotheses that predict equally well, the one with the fewest assumptions should be selected”.&lt;/p&gt;

&lt;h3 id=&quot;overfitting&quot;&gt;Overfitting&lt;/h3&gt;

&lt;p&gt;Although this explanation of Occam’s Razor sounds reasonable, there is an even better way of arguing why we should prefer the line in this stock price example. Occam’s razor rejects the wrinkled function because we have to make too many choices about the direction and the size of the wrinkles. In fact, there are infinitely many wrinkled functions going through all of the points, for instance this one (green line):&lt;/p&gt;

&lt;div class=&quot;imgcenter imgcap&quot;&gt;




&lt;img src=&quot;/intuitivemi/images/2015-08-07-overfitting-sizeprice2.png&quot; class=&quot;&quot; width=&quot;65%&quot; height=&quot;&quot; /&gt;




&lt;/div&gt;

&lt;p&gt;The problem with that is that the chance of picking the &lt;em&gt;wrong&lt;/em&gt; wrinkled function is much higher than picking the wrong straight line - because (at least in our example) there is only &lt;em&gt;one line&lt;/em&gt; that goes exactly through all of the points. I cannot rotate or shift the line up without it losing contact with one or more of the points. Therefore, we should prefer the line. So in a way, we have given a justification of Occam’s razor by realizing that using a more complex function for predictions forces us to take more arbitrary decisions, which makes us more prone to making mistakes.&lt;/p&gt;

&lt;h3 id=&quot;random-fluctuations&quot;&gt;Random fluctuations&lt;/h3&gt;

&lt;p&gt;In the case where the data lies exactly on a line the answer was clear that the line is the best choice. However, in reality, data is not perfect but almost always undergoes random fluctuations and noise. So let us make the stock prediction data a bit more realistic and then look at how well line and wrinkled function explain the data:&lt;/p&gt;

&lt;div class=&quot;imgcenter imgcap&quot;&gt;




&lt;img src=&quot;/intuitivemi/images/2015-08-07-overfitting-sizeprice_noise.png&quot; class=&quot;&quot; width=&quot;65%&quot; height=&quot;&quot; /&gt;




&lt;/div&gt;

&lt;p&gt;We see that no line will be able to pass directly through all the data points, but we have easily found a wrinkled function doing so. Still the line shown here seems to capture the main trend quite well. Which of the two functions do you prefer for making predictions about future stock prices?&lt;/p&gt;

&lt;p&gt;Since I have told you that the fluctuations are random, you will probably prefer the line - even though it does not fit the data exactly. Although the wrinkled function explains the individual data points well, it does not explain the more moderate trend of the revenue-stock price relationship well; it focuses too much on explaining the random fluctations in the data. Data scientists call this general effect &lt;em&gt;overfitting&lt;/em&gt;, and we say that the wrinkled function &lt;em&gt;overfits&lt;/em&gt; the data.&lt;/p&gt;

&lt;h3 id=&quot;battling-overfitting&quot;&gt;Battling Overfitting&lt;/h3&gt;

&lt;p&gt;Out there in the real world, discovering whether your function overfits or not is really hard. Probably, it is &lt;em&gt;the&lt;/em&gt; problem of machine learning. 
It is so prominent that two machine learning professors have even written a song about it. Seriously:&lt;/p&gt;

&lt;div class=&quot;imgcenter&quot;&gt;
&lt;iframe width=&quot;560&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/DQWI1kvmwRg&quot; frameborder=&quot;0&quot; allowfullscreen=&quot;&quot;&gt;&lt;/iframe&gt;
&lt;/div&gt;

&lt;p&gt;Luckily, there are a few things you can do to counteract overfitting apart from singing about it. I want to give you a few examples.&lt;/p&gt;

&lt;p&gt;First, you can retain some preference for simpler explanations, by making a similar argument as before: even for the randomly fluctuating data there are still only a few lines that are  not too far away from any of the points. But since there are infinitely many wrinkled functions passing directly through all of the points, the chance of picking the wrong wrinkled function is very high. So using a simpler function is better.&lt;/p&gt;

&lt;p&gt;Secondly, you can hold back some of your data and not use it for finding the function - you only use it &lt;em&gt;after&lt;/em&gt; having computed the function to test how your function copes with that unseen data. If your function really sucks at explaining the data you have held out (called &lt;em&gt;test data&lt;/em&gt;) you should consider using a different type of functions. In fact, using test data is a standard procedure in machine learning and everyone has to do it because fitting well your training data does not tell you at all whether you learned the right thing.&lt;/p&gt;

&lt;p&gt;Third, you can use &lt;em&gt;more data&lt;/em&gt;. In fact, this is why everyone is so excited about &lt;em&gt;big data&lt;/em&gt; which heavily relies on lots of data being available. It does work well, but we will see in the next post, that there are some limitations about how much data you can get.&lt;/p&gt;

&lt;p&gt;Forth, you can get more knowledge about which shape of the functions you actually expect. If for example you know that the stock price varies in cycles, that is goes up, slightly down, then up again etc., you can try to find a function that reflects this knowledge.&lt;/p&gt;

&lt;p&gt;Although all of these things are good ideas and they definitely help to counteract overfitting, in the end you still have to keep your fingers crossed and hope your function predicts the right thing. There is no guarantee that your function doesn’t overfit although all of the things suggested make it less likely.&lt;/p&gt;

&lt;p&gt;I hope that by looking at this very simple example, and by understanding how overfitting can occur here, you have already come to think more critically about decision making - both in machines and in humans.&lt;/p&gt;

&lt;h3 id=&quot;underfitting&quot;&gt;Underfitting&lt;/h3&gt;

&lt;p&gt;Last but not least, let me say that of course also the opposite thing can happen: &lt;em&gt;underfitting&lt;/em&gt;. If we choose a function that is too simple to explain a relationship we will not get a good prediction, either. I leave it as an exercise for you to think about a good example of underfitting.&lt;/p&gt;

&lt;!--  Quadratic function --&gt;
&lt;p&gt;That’s basically it - you have now understood the biggest problem of learning from data.
And in case you want to brag in front of your friends, here is some more terminology: the topic of this post, that a function learned from data always has to balance under- and overfitting is also called the &lt;em&gt;bias-variance trade-off&lt;/em&gt;. &lt;em&gt;Variance&lt;/em&gt; relates to how much all possible different wrinkled functions vary - the more wrinkles you allow, the higher is the variance of your learning method and hence the more prone the learner is to overfitting. &lt;em&gt;Bias&lt;/em&gt; refers to underfitting, stating that a too simple function adds a systematic bias to the prediction which cannot be overcome by the learner, not even when given more data. The bias-variance trade-off is a smart way of stating that if don’t consider the simplest yet not too simple hypothesis, your hypothesized function is more likely to be wrong – too simple and wrong or too complicated and wrong.&lt;/p&gt;

&lt;p&gt;This post elucidated the first big problem of machine learning: overfitting. Next, we will look at &lt;em&gt;the curse of dimensionality&lt;/em&gt;.&lt;/p&gt;

&lt;!--In the next post we will look at the problem of overfitting in the more complex image classification scenario, and we will see how the dimensionality aggrevates the problem of learning and overfitting even more. --&gt;

&lt;h3 id=&quot;tldr&quot;&gt;&lt;a href=&quot;http://de.urbandictionary.com/define.php?term=tl%3Bdr&quot;&gt;TL;DR&lt;/a&gt;:&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Finding the right predictive function for data is hard&lt;/li&gt;
  &lt;li&gt;Even if the function perfectly fits the data it might be totally wrong: an effect called overfitting&lt;/li&gt;
  &lt;li&gt;Occam’s razor states that one should prefer simpler solutions&lt;/li&gt;
  &lt;li&gt;A justification for this is: when using simpler functions we are less likely to pick the wrong one&lt;/li&gt;
  &lt;li&gt;Underfitting occurs when choosing too simple functions&lt;/li&gt;
  &lt;li&gt;Singing about data science is fun&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;further-reading&quot;&gt;&lt;a name=&quot;further&quot;&gt;&lt;/a&gt;Further Reading:&lt;/h3&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;a name=&quot;[1]&quot;&gt;&lt;/a&gt;&lt;a href=&quot;http://courses.cs.washington.edu/courses/cse546/12wi/slides/cse546wi12LinearRegression.pdf&quot;&gt;Lecture notes&lt;/a&gt; by Luke Zettlemoyer from University of Washington on overfitting.&lt;/li&gt;
  &lt;li&gt;&lt;a name=&quot;[2]&quot;&gt;&lt;/a&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Occam%27s_razor&quot;&gt;Wikipedia article on Occam’s razor&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a name=&quot;[2]&quot;&gt;&lt;/a&gt;&lt;a href=&quot;http://mste.illinois.edu/exner/java.f/leastsquares/&quot;&gt;Simple Java applet&lt;/a&gt; for playing around with data fitting. The switch “degree of polynomial” corresponds to the “wrinkles” in the function.&lt;/li&gt;
&lt;/ol&gt;
</description>
        <pubDate>Fri, 07 Aug 2015 15:00:00 +0000</pubDate>
        <link>http://shoefer.github.io/intuitivemi/2015/08/07/overfitting.html</link>
        <guid isPermaLink="true">http://shoefer.github.io/intuitivemi/2015/08/07/overfitting.html</guid>
        
        
        <category>intuitivemi</category>
        
      </item>
    
      <item>
        <title>The Power of Machine Intelligence</title>
        <description>&lt;p&gt;Thinking machines have revolutionalized our everyday life, maybe even more than industrialization or the invention of the car. But although everyone of has an intuition about how a car works thinking machines remain obscure and magical to most of us.&lt;/p&gt;

&lt;p&gt;I believe that everyone should have at least a “car intuition” about thinking machines. Unfortunately, common introductions to artificial intelligence and machine learning require quite some knowledge about programming and math and scare laymen off. Luckily, none of these skills is required to grasp the core ideas behind thinking machines. And these core ideas are what I would like to tell you about in the forthcoming series of articles in this &lt;em&gt;introduction to machine intelligence&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;To whet your appetite I would like to present four prototypical applications of machine intelligence. We will get back to these applications later and get a more detailed understanding of how machine intelligence approaches each of these problems.&lt;/p&gt;

&lt;h2 id=&quot;image-understanding&quot;&gt;Image Understanding&lt;/h2&gt;

&lt;p&gt;For decades, researchers have been trying to make machines see the world we do. In 1966, one of the most famous artificial intelligence researchers, Marvin Minsky, hired an undergrad to solve the following problem as a summer project: connect a TV camera to a computer and make the computer describe what it sees. Now, 50 years later, we still haven’t fully solved the problem, but at least we have made some progress. In the &lt;em&gt;image classification&lt;/em&gt; task computers are now able to &lt;a href=&quot;https://gigaom.com/2015/02/13/microsoft-says-its-new-computer-vision-system-can-outperform-humans/&quot;&gt;outperform humans&lt;/a&gt; in certain regards. In this task, the computer has to decide for any given image what kind of object is depicted on that image. As manually programming the computer to do so has failed, machine intelligence tries to solve the problem by learning from data. 
To learn successfully huge amounts of &lt;em&gt;labeled data&lt;/em&gt; (images together with descriptions of what object category is depicted on each image) are required which is why the community  created a &lt;a href=&quot;http://www.image-net.org/&quot;&gt;database of over 15 million images&lt;/a&gt; containing 21841 object types. By learning from these images, using a method called &lt;em&gt;deep neural networks&lt;/em&gt;, machine intelligence is now able to predict the object category correctly in over 95% of all cases - which is above human performance. These neural networks will one of the topics covered later.&lt;/p&gt;

&lt;div class=&quot;imgcenter imgcap&quot;&gt;



&lt;a href=&quot;/images/image-classification-msrdl1.jpg&quot; target=&quot;_blank&quot;&gt;


&lt;img src=&quot;/images/image-classification-msrdl1.jpg&quot; id=&quot;image-understanding&quot; class=&quot;&quot; width=&quot;300&quot; height=&quot;&quot; /&gt;


&lt;/a&gt;



&lt;div class=&quot;thecap&quot;&gt;Top 5 predictions by an image classification approach. Credit: Microsoft Research&lt;/div&gt;

&lt;/div&gt;

&lt;p&gt;Note however that the general problem of seeing machines hasn’t been solved yet since the image classification task is limited to pictures where only one object is present. Describing entire scenes is much more complicated, but recently &lt;a href=&quot;http://karpathy.github.io/2015/05/21/rnn-effectiveness/&quot;&gt;progress has been made there&lt;/a&gt;, too.&lt;/p&gt;

&lt;h2 id=&quot;algorithmic-trading-stock-price-prediction&quot;&gt;Algorithmic Trading: Stock Price Prediction&lt;/h2&gt;

&lt;p&gt;Machines are slowly taking over the stock markets using &lt;a href=&quot;https://en.wikipedia.org/wiki/Algorithmic_trading&quot;&gt;algorithmic trading&lt;/a&gt;. One important thing these machine traders have to do is predicting stock prices. How would machine intelligence approach this problem? In fact, the task is very similar to the image classification problem, only that, instead of an object type, the computer should predict a continuous valued number: the stock price. But what data should the stock price predictor use for learning?
In the image classification task this was obvious: the image itself. Here, it is not clear what influences the stock price. It could be last months’s revenue of the company, the size of the company, the stock price from last month, last year, and many more. 
A data scientist will approach this problem by collecting as many of these pieces of information, so-called &lt;em&gt;features&lt;/em&gt;, and try to weigh the features in the best way to predict the stock price. We will look later in more detail how the computer can automatically weigh features to make predictions, and take a look at why the task of “weighing in the best way” is much more difficult then it sounds.&lt;/p&gt;

&lt;h2 id=&quot;spam-classification&quot;&gt;Spam Classification&lt;/h2&gt;

&lt;p&gt;Everyone knows the problem of &lt;a href=&quot;http://www.stevenburgess.net/wp-content/uploads/2014/12/Spam-Can.jpg&quot;&gt;spam&lt;/a&gt;. It tastes weird and, even worse, it fills your mailbox with juicy offers for &lt;a href=&quot;http://www.mensjournal.com/health-fitness/health/the-hard-truth-about-penis-enlargement-20141027&quot;&gt;penis enlargement&lt;/a&gt; or earning money from &lt;a href=&quot;http://www.419eater.com/img/news.pdf&quot;&gt;fake Nigerian royalty&lt;/a&gt;. But thanks to machine intelligence, nowadays most spam automatically finds its way into your junk folder. This application of machine intelligence is called &lt;em&gt;spam classification&lt;/em&gt; and - in its simplest form - works as follows: your mail program receives an email and extracts features (as in the example before) from the email, such as what words the email  contains, whether you know the author of the email, and many more. It then weighs each of these features and makes a binary decision: spam or no spam.&lt;/p&gt;

&lt;p&gt;We see a pattern emerging here. Spam classification is very similar to image classification and stock price prediction: data scientists collects many spam emails, extract features from these emails, and use fancy tools to find the best weighting of all the features. The main difference is that not a continuous number (stock price), or an object category, but a binary output (spam / no spam) is computed.&lt;/p&gt;

&lt;h2 id=&quot;robot-skill-learning&quot;&gt;Robot Skill Learning&lt;/h2&gt;

&lt;p&gt;The last and maybe most exciting application of machine intelligence is robotics. Interestingly, you can (to some extent) apply exactly the same techniques - as before - to make a robot do cool things. The following video shows how to teach a robot to play table tennis.&lt;/p&gt;

&lt;div class=&quot;imgcenter&quot;&gt;
&lt;iframe width=&quot;280&quot; height=&quot;155&quot; src=&quot;https://www.youtube.com/embed/SH3bADiB7uQ&quot; frameborder=&quot;0&quot; allowfullscreen=&quot;&quot;&gt;&lt;/iframe&gt;
&lt;/div&gt;

&lt;p&gt;The type of learning slightly differs from the approaches seen before. Instead of telling the robot exactly what the right motion, the researchers let the robot try out motions on its own and give the robot a reward if it performs a good stroke, or a penalty if it performs bad (in fact, the robot doesn’t get lollipops as rewards, but delicious large numbers). The robot will then repetitively generate motions, collect rewards/penalties and step by step correct its motions in order to get more reward and finally execute an excellent table tennis stroke.&lt;/p&gt;

&lt;!-- 
However, you can also formulate the problem In a nutshell, the researchers make something similar as in stock price prediction: from the position of the ball you try to &quot;predict&quot;, or rather decide for, a motor command to send to the robot. Executing this motion will then allow the robot to hit the table tennis ball.
 --&gt;

&lt;p&gt;I hope I was able to get you excited about machine intelligence! In the next couple of posts I will try to convey the basic ideas of how machine intelligence approaches all of these problems.&lt;/p&gt;

&lt;p&gt;The next post will tell you in more detail why you don’t need to understand complex math and programming to understand machine intelligence.&lt;/p&gt;
</description>
        <pubDate>Tue, 28 Jul 2015 15:00:00 +0000</pubDate>
        <link>http://shoefer.github.io/intuitivemi/2015/07/28/datascience-showoff.html</link>
        <guid isPermaLink="true">http://shoefer.github.io/intuitivemi/2015/07/28/datascience-showoff.html</guid>
        
        
        <category>intuitivemi</category>
        
      </item>
    
      <item>
        <title>High-dimensional Spaces</title>
        <description>&lt;p&gt;In the &lt;a href=&quot;/intuitivemi/2015/07/19/data-numbers-representations.html&quot;&gt;last post&lt;/a&gt; we have seen examples for how numbers can encode information, becoming data. In this post we will talk about a very important way to look at data. This view will allow us to play around with data in a powerful way, and this view lies at the core of machine intelligence and the science of data.&lt;/p&gt;

&lt;h3 id=&quot;data-samples-as-points-in-space&quot;&gt;Data samples as points in space&lt;/h3&gt;

&lt;p&gt;I have previously told you that any &lt;a href=&quot;/intuitivemi/images/2015-07-19-data-numbers-representations_picture.png&quot;&gt;image&lt;/a&gt; can be represented by a &lt;a href=&quot;/intuitivemi/images/2015-07-19-data-numbers-representations_numbers.png&quot;&gt;table of numbers&lt;/a&gt; where each number encodes the gray scale value of the image. For the trick that I would like to teach you, this image is actually a bit too large. So for the sake of the argument, let us shrink the original 27x35 pixel image drastically until it gets really really tiny, namely 3x1, that means only 3 pixels wide and 1 pixel high - bear with me for a second even if this sounds silly to you. We won’t see anything on this image but that doesn’t matter right now. Our 3x1 image expressed in gray scale values now looks like this:&lt;/p&gt;

&lt;table class=&quot;data-table&quot;&gt;
&lt;tr&gt;
&lt;td style=&quot;background-color: #000; opacity: 0.909; width: 30px&quot;&gt;0.909&lt;/td&gt;
&lt;td style=&quot;background-color: #000; opacity: 1.0; width: 30px&quot;&gt;1.000&lt;/td&gt;
&lt;td style=&quot;background-color: #000; opacity: 0.860; width: 30px&quot;&gt;0.860&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;

&lt;p&gt;I have set the background color of each table cell to correspond to the actual gray scale value the number encodes. Here comes the trick: although we know that these three numbers encode gray scale values we can &lt;em&gt;pretend they were encoding the location of a point in 3D space&lt;/em&gt;. So instead of encoding “luminosity” we think of the numbers are being in “meters” or “centimeters”. As you might remember from high school we draw a location as an arrow in an coordinate system. So let’s draw our 3x1 “image” in a 3D coordinate system:&lt;/p&gt;

&lt;div class=&quot;imgcenter imgcap&quot;&gt;


&lt;img src=&quot;/intuitivemi/images/2015-07-21-vector_spaces-arrow.png&quot; id=&quot;vector-spaces-arrow&quot; class=&quot;&quot; width=&quot;500&quot; height=&quot;375&quot; /&gt;


&lt;/div&gt;

&lt;script&gt;
    $(&apos;#vector-spaces-arrow&apos;).gifplayer({label:&apos;play&apos;});
&lt;/script&gt;

&lt;div&gt;
&lt;img style=&quot;display:none; width:0px; height:0px; visibility:hidden; border:0; margin:0; padding:0;&quot; src=&quot;/intuitivemi/images/2015-07-21-vector_spaces-arrow.gif&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;So you might think: “drawing arrows is really fun but why the heck are we doing this?” There are two reasons for that: the first reason is this that we humans are really good at manipulating objects in 3D space. We know how to move objects, we know how to rotate them, how to distort and mirror them, how to project them on a 2D planar surface (by taking a picture of it), and much more. Thus, if we treat data as locations in space we can apply all our spatial 3D knowledge to it. 
In fact, &lt;a href=&quot;https://en.wikipedia.org/wiki/Linear_algebra&quot;&gt;linear algebra&lt;/a&gt; has got all the math worked out to simulate these 3D operations on computers (as you can admire in Toy Story, Madagascar, and so on) &lt;a href=&quot;#[1]&quot;&gt;[1]&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Now the question is, what does the 3D arrow sketched above have to do with the original 27x35 pixel wide image of my face? Here’s the gist: linear algebra is so general that it does not care about the dimensionality of the data - it works for 3-dimensional spaces in the same way as for 5-dimensional or 500-dimensional spaces - even though our brains are not capable of imagining 500-dimensional data visually! This allows us to treat the 27x35 &lt;a href=&quot;/intuitivemi/images/2015-07-19-data-numbers-representations_picture.png&quot;&gt;picture of my face&lt;/a&gt; as a point in some crazy unimaginable 945-dimensional space &lt;a href=&quot;#[2]&quot;&gt;[2]&lt;/a&gt;! We will see later that applying such operations as moving and rotating a point in this huge space will be the basis for extracting information from it, as for example detecting what is in the image represented by this point &lt;a href=&quot;#[3]&quot;&gt;[3]&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So just to make this clear: I will now continue drawing 3D arrows, but these arrows are in fact just sketches of 945-dimensional arrows (since they are impossible to draw). Therefore I will &lt;em&gt;write&lt;/em&gt; about 945-dimensional, but &lt;em&gt;draw&lt;/em&gt; 3-dimensional arrows.&lt;/p&gt;

&lt;h3 id=&quot;discriminating-between-categories--finding-separating-planes&quot;&gt;Discriminating between categories = finding separating planes&lt;/h3&gt;

&lt;p&gt;The really cool thing is that we can now understand how the computer can discriminate between images that depict different objects. Imagine we have another image of, say a &lt;a href=&quot;https://teara.govt.nz/en/photograph/5281/blobfish-and-snailfish&quot;&gt;blobfish&lt;/a&gt;. By the same procedure as before we can make the blobfish a point in 945-dimensional space. Now let’s assume we do not only have one picture of me and one of the blobfish, but a whole bunch of pictures for each category, Sebastians and blobfish.&lt;/p&gt;

&lt;p&gt;How could a computer automatically discriminate between these two categories? Geometrically, of course! By treating each image as a point in a 945-dimensional space, we can now come up with a geometrical interpretation of discriminating between categories: we find a plane which separates these two sets of points (blue is me, red the blobfish):&lt;/p&gt;

&lt;div class=&quot;imgcenter imgcap&quot;&gt;


&lt;img src=&quot;/intuitivemi/images/2015-07-21-vector_spaces-arrow-plane.png&quot; id=&quot;vector-spaces-arrow-plane&quot; class=&quot;&quot; width=&quot;500&quot; height=&quot;375&quot; /&gt;


&lt;/div&gt;

&lt;script&gt;
    $(&apos;#vector-spaces-arrow-plane&apos;).gifplayer({label:&apos;play&apos;});
&lt;/script&gt;

&lt;div&gt;
&lt;img style=&quot;display:none; width:0px; height:0px; visibility:hidden; border:0; margin:0; padding:0;&quot; src=&quot;/intuitivemi/images/2015-07-21-vector_spaces-arrow-plane.gif&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;Using this plane, the computer can now automatically discriminate between blobfish and Sebastians by checking &lt;em&gt;on which side of the plane&lt;/em&gt; the point lies.&lt;/p&gt;

&lt;p&gt;Sit back for a second and make sure that you have understood this. Because if so you can be proud - you have just understood the key principle behind &lt;a href=&quot;/intuitivemi/2015/07/28/datascience-showoff.html&quot;&gt;image classification&lt;/a&gt;, the problem that I have presented in the intro article! The majority of all approaches in machine intelligence draws such planes through the high-dimensional image space, assigns a category to each side of the plane, and then checks on which side of the plane a new, previously unseen image lies.&lt;/p&gt;

&lt;h4 id=&quot;caveats&quot;&gt;Caveats&lt;/h4&gt;

&lt;p&gt;Of course, there are some details that I have omitted. 
As mentioned before, the sketch is in 3 dimensions, although we should draw 945 dimensions, which is not possible; and also the thing separating the two object categories in 945 dimensions is not really a plane, but a &lt;em&gt;hyperplane&lt;/em&gt; (that’s what a plane is called in four or more dimensions - not to be confounded with &lt;a href=&quot;http://starwars.wikia.com/wiki/Hyperspace&quot;&gt;hyperspace&lt;/a&gt;). 
But still, the math - which I have not shown you, so you need to trust me on this one -  assures us that the concepts of points and planes also exist in 945-dimensional space, so it is totally valid to think of it in 3 dimensions only.&lt;/p&gt;

&lt;p&gt;Another important detail that I have omitted is how to find this (hyper-)plane. And in fact this is &lt;em&gt;the&lt;/em&gt; central problem of machine intelligence, or more precisely &lt;em&gt;machine learning&lt;/em&gt;. We will talk about how this learning works in the post after the next one.&lt;/p&gt;

&lt;p&gt;Finally, I have to lower your expectations about this method a bit: discriminating Sebastians and blowfish by finding a plane in this 945-dimensional space does not really work well in practise. The reason is that pictures of Sebastians and blowfish are not as nicely separable as I have suggested in the 3D arrow picture above, but rather scattered all around the space. But the general idea of finding discriminating hyperplanes in high-dimensional spaces still holds. And we can get it to work by first bringing the image into a different &lt;em&gt;representation&lt;/em&gt;, that is a different space, and then finding the hyperplane in this new space (at this point you should remember our discussion about representations in &lt;a href=&quot;/intuitivemi/2015/07/19/data-numbers-representations.html&quot;&gt;the previous post&lt;/a&gt;). You can either try to transform images in these representations explicitly, i.e. think of a good representation and program it yourself, or implicitly, trying to find a way of doing this automatically. We will talk about both approaches later, too.&lt;/p&gt;

&lt;h4 id=&quot;terminology&quot;&gt;Terminology&lt;/h4&gt;

&lt;p&gt;Some last notes on terminology.&lt;/p&gt;

&lt;p&gt;Often, sequence of numbers, such as the 3d point or the 945-dimensional representation of the image is called a &lt;em&gt;vector&lt;/em&gt;, and it then lives in a  &lt;em&gt;vector space&lt;/em&gt; of some dimensionality. Although being basically the same thing, the term vector has a slightly different connotation which we will ignore for now. But I will mostly use the vector mainly  because it sounds cooler.&lt;/p&gt;

&lt;p&gt;Secondly, mathematics tries to be parsimonious with concepts, mainly because it makes talking about things easier. Therefore, we get rid of the concept “numbers” by treating them as 1-dimensional vectors. So in the future if I talk about vectors, you can often think of them as just being numbers.&lt;/p&gt;

&lt;h4 id=&quot;summary&quot;&gt;Summary&lt;/h4&gt;

&lt;p&gt;To summarize, the approach of machine intelligence is the following: we transform some input, for example an &lt;a href=&quot;/intuitivemi/images/2015-07-19-data-numbers-representations_picture.png&quot;&gt;image&lt;/a&gt; into a &lt;a href=&quot;/intuitivemi/images/2015-07-19-data-numbers-representations_numbers.png&quot;&gt;table of numbers&lt;/a&gt;, call this a &lt;em&gt;vector&lt;/em&gt; and then treat this vector as a point in a high-dimensional space. We then apply our knowledge of how to manipulate points in 3D to transform this high-dimensional vector in order to extract information from it. Moreover, we can classify between different types of data, for example object categories, by finding discriminating hyperplanes in this space.&lt;/p&gt;

&lt;p&gt;In the next post we will look at how be more precise about what we mean by transformations and how to describe these hyperplanes by introducing the concept of functions.&lt;/p&gt;

&lt;h3 id=&quot;tldr&quot;&gt;&lt;a href=&quot;http://de.urbandictionary.com/define.php?term=tl%3Bdr&quot;&gt;TL;DR&lt;/a&gt;:&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Data can be viewed as points in high-dimensional spaces&lt;/li&gt;
  &lt;li&gt;We can apply the same transformations to points in this space as in 3D&lt;/li&gt;
  &lt;li&gt;Such points are called vectors&lt;/li&gt;
  &lt;li&gt;Hyperplanes in space discriminate between vectors, and hence categories&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;footnotes&quot;&gt;&lt;a name=&quot;further&quot;&gt;&lt;/a&gt;Footnotes:&lt;/h3&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;a name=&quot;[1]&quot;&gt;&lt;/a&gt;Actually, linear algebra can do even a bit more than what is “physically possible” in the 3D world, e.g. mirroring and skewing objects.&lt;/li&gt;
  &lt;li&gt;&lt;a name=&quot;[2]&quot;&gt;&lt;/a&gt;To turn images into an arrow/point, we need to remove the column and row structure of the image and write all of the numbers in one very long row. And since 27 times 35 = 945, the original image becomes a 945-dimensional point. This has indeed been common practise when learning from images (although more recent algorithms exploit the spatial, i.e. “grid” structure of the image, rather than just stitching the columns together).&lt;/li&gt;
  &lt;li&gt;&lt;a name=&quot;[3]&quot;&gt;&lt;/a&gt;It is important to notice that moving or rotating a point in 945-dimensional space which represents an image &lt;em&gt;is not the same&lt;/em&gt; as shifting or rotating the image! Since moving in 3D is equivalent to “adding a number to one or more dimensions”, this is also the definition of moving in 945-dimensional space. If you push a 3D object 3m up and 5m to the right you effectively add these two numbers some coordinates of the object. Hence, in the image example, moving is equivalent to changing the gray scale value of certain pixels. Neither do vector rotations actually rotate the image. 
&lt;!--  Rotations look even weirder: --&gt;
&lt;!-- TODO rotated image --&gt;&lt;/li&gt;
&lt;/ol&gt;
</description>
        <pubDate>Sat, 25 Jul 2015 16:02:00 +0000</pubDate>
        <link>http://shoefer.github.io/intuitivemi/2015/07/25/vector-spaces.html</link>
        <guid isPermaLink="true">http://shoefer.github.io/intuitivemi/2015/07/25/vector-spaces.html</guid>
        
        
        <category>intuitivemi</category>
        
      </item>
    
      <item>
        <title>Data, Numbers and Representations</title>
        <description>&lt;p&gt;I would like to start the intuitive introduction to machine intelligence by looking at the term &lt;em&gt;data&lt;/em&gt;.  Let us try to gain an informal but sufficient understanding of how we could define data.&lt;/p&gt;

&lt;h3 id=&quot;data-are-numbers&quot;&gt;Data are numbers&lt;/h3&gt;

&lt;p&gt;Big data, data mining, data analysis, data, data, data - everyone wants data. But what &lt;em&gt;is&lt;/em&gt; data? The philosophical answer could be: information. The data scientist’s answer would probably be: a bunch of numbers. 
Indeed, we are going to stick to the latter definition, namely that &lt;em&gt;data is everything that can be encoded by numbers&lt;/em&gt; &lt;a href=&quot;#numbersencode&quot;&gt;[1]&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Some examples: a picture you have taken with your digital camera is data. You can look at it, see the things depicted on it. But your digital camera uses numbers to store the pictures. Also the MP3 sound file that your are currently listening to is data. Even this text here is data! So how is data numbers?
&lt;!-- However, information is also quite an obscure term. So let&apos;s define data as &quot;everything you might consider doing something with&quot;.  --&gt;&lt;/p&gt;

&lt;p&gt;Let’s look at the concrete example of a picture:&lt;/p&gt;

&lt;div class=&quot;imgcenter imgcap&quot;&gt;




&lt;img src=&quot;/intuitivemi/images/2015-07-19-data-numbers-representations_numbers.png&quot; class=&quot;&quot; width=&quot;85%&quot; height=&quot;&quot; /&gt;




&lt;/div&gt;

&lt;p&gt;What do we see here? It’s a picture of &lt;a href=&quot;/intuitivemi/images/2015-07-19-data-numbers-representations_picture.png&quot;&gt;my face&lt;/a&gt; of course! Ok, if you don’t see it you should definitely watch &lt;a href=&quot;https://en.wikipedia.org/wiki/The_Matrix&quot;&gt;The Matrix&lt;/a&gt; more often. In case that doesn’t work, how do we get the picture back? The trick is that I have taken the gray scale picture of &lt;a href=&quot;/intuitivemi/images/2015-07-19-data-numbers-representations_picture.png&quot;&gt;my face&lt;/a&gt;  and replaced every dot in the original picture with a number between 0 and 1 where 0 means “black” and 1 means “white”, and the numbers inbetween represent the different &lt;a href=&quot;https://en.wikipedia.org/wiki/Fifty_Shades_of_Grey&quot;&gt;shades of gray&lt;/a&gt; between these black and white tones. When showing the image on the screen the computer translates these numbers back to gray values.&lt;/p&gt;

&lt;!-- 
(If you wonder why it is so difficult to recognize my face in the table of numbers while it is not in the gray scale picture read [this short post](/intuitivemi/2015/07/23/difficult-cv.html).)
 --&gt;

&lt;h3 id=&quot;data-is-numbers--what--encoding&quot;&gt;Data is numbers + what + encoding&lt;/h3&gt;

&lt;p&gt;However, the way how I have chosen the numbers to substitute gray values in the picture is entirely arbitrary. We could have done the same by representing black with 0.5 and white with 12, reverse the order, or even mix up the order of the numbers, etc. The way how you choose the numbers is completely up to you. But it is very important to know that: somebody else will not know how to make sense of the numbers unless you tell her that it is a picture and which numbers correspond to which gray scale. The latter information, of how to translate gray scale values to numbers and back is what we call the &lt;em&gt;representation&lt;/em&gt;, and is also sometimes called the &lt;em&gt;encoding&lt;/em&gt; of the data.&lt;/p&gt;

&lt;!-- 
Moreover, some representation might be better or worse with respect to some criterion. For example, when encoding gray scale values between 0 and 1 is immediately know that 0.5 is an average gray; we wouldn&apos;t have this intuitive understanding if we encoded the values between 41543 and 341125.
 --&gt;

&lt;p&gt;What about the sound file example? This one is a bit more tricky. You have to know quite a lot about the physics of sound. Let’s take a 10 seconds sound file. The easy way to think about how to encode sound by numbers is to divide the 10 seconds a sound file into say 10000 equally sized parts à 1 millisecond and assign a number to each part. Every number corresponds to the “loudness” of a part. That’s basically it! You might wonder now how a human voice, a guitar and the engine of a car can all be broken done to these few numbers. Well, it’s really just that! The real “magic” is happening in your brain which collects these pieces of information and generates a certain sensation which you identify as an opera singer or Jimi Hendrix playing his guitar. 
If you want to learn more about these things I recommend you to look into &lt;a href=&quot;#brainonmusic&quot;&gt;this book [2]&lt;/a&gt; which does a great job at explaining what sound and music really is and how the brain comes at perceiving it the way it is.&lt;/p&gt;

&lt;p&gt;What is finally left is the text example. This one is easy, too. I leave it as an exercise to you to think of how to encode or represent text as numbers. (A hint: think about how many different letters and characters we have in our alphabet).&lt;/p&gt;

&lt;h4 id=&quot;summary&quot;&gt;Summary&lt;/h4&gt;

&lt;p&gt;To summarize, we have learned that data is numbers plus information about what these numbers mean, called the representation or encoding.&lt;/p&gt;

&lt;p&gt;As a final remark, I cannot overemphasize how crucial the representation of data is. 
The representation of a problem can drastically influence the ability to solve it (as exemplified very well in &lt;a href=&quot;#funwithrepr&quot;&gt;[3]&lt;/a&gt;), and corresponds directly to the question of how to represent data.
Lots of the stuff you will read about in future articles deals with transforming numbers from one representation into another. In the next article we will come up with an interesting way of looking at data in order to extract information from it.&lt;/p&gt;

&lt;h3 id=&quot;tldr&quot;&gt;&lt;a href=&quot;http://de.urbandictionary.com/define.php?term=tl%3Bdr&quot;&gt;TL;DR&lt;/a&gt;:&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Data is numbers + representation (what these numbers stand for)&lt;/li&gt;
  &lt;li&gt;How data is represented is crucial&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;further-reading-and-thinking&quot;&gt;Further reading and thinking:&lt;/h3&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;a name=&quot;numbersencode&quot;&gt;Is there something that cannot be encoded by numbers?&lt;/a&gt; Some people would argue there isn’t. Do you agree? What would be the consequences of it?&lt;/li&gt;
  &lt;li&gt;&lt;a name=&quot;brainonmusic&quot;&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/This_Is_Your_Brain_on_Music&quot;&gt;This Is Your Brain on Music&lt;/a&gt; by Daniel J. Levitin&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a name=&quot;funwithrepr&quot;&gt;&lt;a href=&quot;https://catenary.wordpress.com/2006/08/19/fun-with-representations-i-nine-numbers/&quot;&gt;Fun with representations&lt;/a&gt;&lt;/a&gt; is a fantastic series of articles about representations, and how different representations enable us to solve complex problems more easily than others.&lt;/li&gt;
&lt;/ol&gt;
</description>
        <pubDate>Sun, 19 Jul 2015 15:07:27 +0000</pubDate>
        <link>http://shoefer.github.io/intuitivemi/2015/07/19/data-numbers-representations.html</link>
        <guid isPermaLink="true">http://shoefer.github.io/intuitivemi/2015/07/19/data-numbers-representations.html</guid>
        
        
        <category>intuitivemi</category>
        
      </item>
    
      <item>
        <title>Understanding Machine Intelligence without Formal Math</title>
        <description>&lt;p&gt;In this post I want to defend the basic idea of this series of articles, namely why I am trying to avoid mathematical formalism for explaining data science.&lt;/p&gt;

&lt;p&gt;If you don’t care about math anyway, you can skip this post and start reading about &lt;a href=&quot;/intuitivemi/2015/07/19/data-numbers-representations.html&quot;&gt;data&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I do not have anything against formulas. Quite the contrary: mathematical formalisms are a fantastic way to condense and sort out the necessary ingredients of a method. Without formalism, we could not program a computer to operationalize any method. Think of a formula as a packing list for your camping trip. It has to be complete and all the things you need have to be on it, and only on site you might realize if you did something wrong (“Oh no, I forgot to bring my &lt;a href=&quot;http://consumerist.com/2010/11/29/go-hiking-in-style-with-these-teva-high-heels/&quot;&gt;hiking heels&lt;/a&gt;”). In a similar way a formula has to contain everything required to describe your mathematical concept or method, and only when applying it / executing your method you realize whether something in your mathematical description is wrong or missing.&lt;/p&gt;

&lt;p&gt;However, I find formulas a very bad way to understand things. I see three main reasons for that.&lt;/p&gt;

&lt;p&gt;The first reason is that after studying and working in academia for a couple of years, I realized that the magic “ahhh, THAT’S how it works!” moments while trying to grasp a complex method always happened when I could find analogies in “bodily” or linguistic terms: while rotating three-dimensional structures in my head, while visualizing probabilities as a contour (like a mountainous landscape), while breaking down complex relationships to sets of causal if-then statements, and so on. Maybe not everybody thinks of mathematics like this. It is possible that some mathematical geniuses think differently when reasoning about math. But the vast majority of researchers and students that I have met so far strive for analogies in order to explain or grasp complex mathematical concepts. I think there is a lot of empirical evidence for that, for example that scientific papers plot graphs, bar charts and the like in order to visualize results instead of just printing some numbers. Two famous cognitive scientists even have made an endeavor to argue that the holy grail of mathematics, Euler’s formula, can be derived only from the way we perceive the world (in contrast to a Platonic view of universal mathematical truths) &lt;a href=&quot;#further&quot;&gt;[1,2]&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The second reason is that formulas often do not reflect the way how people figured out the concept or method underlying the formula &lt;a href=&quot;#further&quot;&gt;[3]&lt;/a&gt;. It is like not telling the whole story but just the end of it. You will have a hard time reconstructing the rest of the story from that little information. With formulas it is at least not impossible as they condense all information, only in a very concise manner; but you will still have a hard time figuring out its meaning without some context.&lt;/p&gt;

&lt;p&gt;The third reason is much simpler: Formulas scare many people. I believe one reason for this lies in the condensed nature of formulas that I have mentioned before. Formulas use a very precise and non-redundant language, which is far less verbose than natural language. Therefore, you can spend an entire day looking at one single formula without getting it. I believe this being the reason for some people thinking that they are not good at math, although they probably could be. They get disappointed too quickly because they cannot grasp the formula within an eyelash. Relax guys, that holds true for most of us.&lt;/p&gt;

&lt;p&gt;So let us now jump right into the topic and talk a bit about &lt;em&gt;data&lt;/em&gt;!&lt;/p&gt;

&lt;p&gt;&lt;i&gt;&lt;b&gt;Update 09/11/2016:&lt;/b&gt;  I have received a series of similar comments regarding this article, which I would like to respond to. The first critique was that I am claiming that all researchers think in intuitions, rather than in formulas, when devising mathematical equations and algorithms. That is of course not true; there are people who prefer to and are very good at thinking in abstract formulas, rather than in intuitions. And indeed, certain problems can be more easily solved “syntactically”, that means only by applying mathematical rules rather than thinking in intuitions (but we won’t do that in this series of articles). Secondly, I want to admit that, in some cases, the full aha-that’s-how-it-works moment comes when reconciling the intuitive and the formal, mathematical levels. But - at least for me - the intuitive level was always required to make full understanding happen.&lt;/i&gt;&lt;/p&gt;

&lt;h3 id=&quot;tldr&quot;&gt;&lt;a href=&quot;http://de.urbandictionary.com/define.php?term=tl%3Bdr&quot;&gt;TL;DR&lt;/a&gt;:&lt;/h3&gt;
&lt;ul&gt;
  &lt;li&gt;Formulas are good for condensing and bullet-proofing concepts and methods, but less often for understanding&lt;/li&gt;
  &lt;li&gt;Most people conceptualize complex things in analogies, relating them to perception (e.g. vision) and language&lt;/li&gt;
  &lt;li&gt;Formulas scare people&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;further-reading-and-thinking&quot;&gt;&lt;a name=&quot;further&quot;&gt;&lt;/a&gt;Further reading and thinking:&lt;/h3&gt;
&lt;ol&gt;
  &lt;li&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Euler&apos;s_formula&quot;&gt;Euler’s formula on Wikipedia&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Where_Mathematics_Comes_From&quot;&gt;Where Mathematics Comes From&lt;/a&gt; by Lakoff and Nunez&lt;/li&gt;
  &lt;li&gt;Of course, this might not be true for everybody. A colleague of mine argued that real understanding only happens if you understand both, the high-level concept or analogy, and the mathematical formula. I agree that to get a full understanding you need to care about the details – but to get a rough idea of how and why something works, I will argue that the mathematical details are not required.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.r2d3.us/visual-intro-to-machine-learning-part-1/&quot;&gt;A Visual Introduction to Machine Learning&lt;/a&gt; takes a similar stance as this blog and features cool visualizations of data and machine learning algorithms, as well as brief technical descriptions.&lt;/li&gt;
&lt;/ol&gt;
</description>
        <pubDate>Sun, 19 Jul 2015 15:00:00 +0000</pubDate>
        <link>http://shoefer.github.io/intuitivemi/2015/07/19/why-no-formulas.html</link>
        <guid isPermaLink="true">http://shoefer.github.io/intuitivemi/2015/07/19/why-no-formulas.html</guid>
        
        
        <category>intuitivemi</category>
        
      </item>
    
  </channel>
</rss>
