Skip to content
Snippets Groups Projects
01_introduction.ipynb 196 KiB
Newer Older
schmittu's avatar
schmittu committed
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
schmittu's avatar
schmittu committed
    "# Chapter 1: General Introduction to machine learning (ML)"
schmittu's avatar
schmittu committed
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
schmittu's avatar
schmittu committed
    "## ML = \"learning models from data\"\n",
schmittu's avatar
schmittu committed
    "\n",
schmittu's avatar
schmittu committed
    "\n",
schmittu's avatar
schmittu committed
    "### About models\n",
schmittu's avatar
schmittu committed
    "\n",
schmittu's avatar
schmittu committed
    "A \"model\" allows us to explain observations and to answer questions. For example:\n",
    "\n",
    "   1. Where will my car at given velocity stop when I break now ?\n",
schmittu's avatar
schmittu committed
    "   2. Where on the night sky will I see the moon tonight ?\n",
    "   2. Is the email I received spam ? \n",
    "   4. What article X should I recommend to my customers Y ?\n",
schmittu's avatar
schmittu committed
    "   \n",
schmittu's avatar
schmittu committed
    "- The first two questions can be answered based on existing physical models (formulas). \n",
    "\n",
    "- For the  questions 3 and 4 it is difficult to develop explicitly formulated models. \n",
schmittu's avatar
schmittu committed
    "\n",
schmittu's avatar
schmittu committed
    "### What is needed to apply ML ?\n",
    "\n",
    "Problems 3 and 4 have the following in common:\n",
    "\n",
    "- No exact model known or implementable because we have a vague understanding of the problem domain.\n",
    "- But enough data with sufficient and implicit information is available.\n",
schmittu's avatar
schmittu committed
    "\n",
schmittu's avatar
schmittu committed
    "\n",
schmittu's avatar
schmittu committed
    "\n",
    "E.g. for the spamming example:\n",
    "\n",
    "- We have no explicit formula for such a task\n",
schmittu's avatar
schmittu committed
    "- We have a vague understanding of the problem domeani, because we know that some words are specific for spam emails, other words are specific for my personal and job emails.\n",
schmittu's avatar
schmittu committed
    "- My mailbox is full with examples for spam vs non-spam.\n",
    "\n",
    "\n",
schmittu's avatar
schmittu committed
    "**In such cases machine learning offers approaches to build models based on example data.**\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "## ML: what is \"learning\" ?\n",
    "\n",
    "To create a predictive model, we first must \"learn\" such a model on given data. \n",
    "\n",
    "All ML algorithms have in common that they rely on internal data structures and/or parameters. Learning then builds up such data structures or adjusts parameters based on the given data. After that such models can be used to explain observations or to answer questions.\n",
    "\n",
    "The important difference between explicit models and models learned from data:\n",
    "\n",
    "- Explicit models usually offer exact answers to questions\n",
    "- Models we learn from data usually come with inherent uncertainty."
schmittu's avatar
schmittu committed
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
schmittu's avatar
schmittu committed
    "\n",
schmittu's avatar
schmittu committed
    "## Some history\n",
    "\n",
schmittu's avatar
schmittu committed
    "Some parts of ML are older than you might think. This is a rough time line with a few selected achievements from this field:\n",
schmittu's avatar
schmittu committed
    "\n",
    " \n",
    "    1812: Bayes Theorem\n",
    "    1913: Markov Chains\n",
    "    1951: First neural network\n",
schmittu's avatar
schmittu committed
    "    1959: first use or term \"machine learning\" AI pioneer Arthur Samuel\n",
schmittu's avatar
schmittu committed
    "    1969: Book \"Perceptrons\": Limitations of Neural Networks\n",
    "    1986: Backpropagation to learn neural networks\n",
    "    1995: Randomized Forests and Support Vector Machines\n",
schmittu's avatar
schmittu committed
    "    1998: Public appearance of ML: naive Bayes Classifier for Spam detection\n",
    "    2000+: Deep learning\n",
    "    \n",
    "So the field is not as new as one might think, but due to \n",
    "\n",
    "- more available data\n",
    "- more processing power \n",
    "- development of better algorithms \n",
    "\n",
    "more applications of machine learning appeared during the last 15 years."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Machine learning with Python\n",
    "\n",
    "Currently (2018) `Python` is the  dominant programming language for ML. Especially the advent of deep-learning pushed this forward. First releases of frameworks such as `TensorFlow` or `PyTorch` were released with`Python` support early.\n",
    "\n",
    "The prevalent packages in the Python eco-system used for ML include:\n",
    "\n",
    "- `pandas` for handling tabualar data\n",
    "- `matplotlib` and `seaborn` for plotting\n",
    "- `scikit-learn` for classical (non-deep-learning) ML\n",
    "- `tensorflow`, `PyTorch` and `Keras` for deep-learning.\n",
    "\n",
    "`scikit-learn` is very comprehensive and the online-documentation itself provides a good introducion into ML."
schmittu's avatar
schmittu committed
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
schmittu's avatar
schmittu committed
    "## ML terms: What are \"features\" ?\n",
schmittu's avatar
schmittu committed
    "\n",
schmittu's avatar
schmittu committed
    "A typical and very common situation is that our data is presented as a table, as in the following example:"
schmittu's avatar
schmittu committed
   ]
  },
  {
   "cell_type": "code",
schmittu's avatar
schmittu committed
   "execution_count": 1,
schmittu's avatar
schmittu committed
   "metadata": {},
schmittu's avatar
schmittu committed
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>alcohol_content</th>\n",
       "      <th>bitterness</th>\n",
       "      <th>darkness</th>\n",
       "      <th>fruitiness</th>\n",
       "      <th>is_yummy</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>3.739295</td>\n",
       "      <td>0.422503</td>\n",
       "      <td>0.989463</td>\n",
       "      <td>0.215791</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>4.207849</td>\n",
       "      <td>0.841668</td>\n",
       "      <td>0.928626</td>\n",
       "      <td>0.380420</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>4.709494</td>\n",
       "      <td>0.322037</td>\n",
       "      <td>5.374682</td>\n",
       "      <td>0.145231</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>4.684743</td>\n",
       "      <td>0.434315</td>\n",
       "      <td>4.072805</td>\n",
       "      <td>0.191321</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>4.148710</td>\n",
       "      <td>0.570586</td>\n",
       "      <td>1.461568</td>\n",
       "      <td>0.260218</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   alcohol_content  bitterness  darkness  fruitiness  is_yummy\n",
       "0         3.739295    0.422503  0.989463    0.215791         0\n",
       "1         4.207849    0.841668  0.928626    0.380420         0\n",
schmittu's avatar
schmittu committed
       "2         4.709494    0.322037  5.374682    0.145231         1\n",
       "3         4.684743    0.434315  4.072805    0.191321         1\n",
       "4         4.148710    0.570586  1.461568    0.260218         0"
      ]
     },
schmittu's avatar
schmittu committed
     "execution_count": 1,
schmittu's avatar
schmittu committed
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
schmittu's avatar
schmittu committed
   "source": [
    "import pandas as pd\n",
    "\n",
    "features = pd.read_csv(\"beers.csv\")\n",
    "features.head()"
   ]
  },
schmittu's avatar
schmittu committed
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
schmittu's avatar
schmittu committed
    "**Definitions**:\n",
schmittu's avatar
schmittu committed
    "- every row of such a matrix is called a **sample** or **feature vector**. \n",
schmittu's avatar
schmittu committed
    "\n",
    "- the cells in a row are **feature values**.\n",
    "- every column name is called a **feature name** or **attribute**."
schmittu's avatar
schmittu committed
   ]
  },
schmittu's avatar
schmittu committed
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
schmittu's avatar
schmittu committed
    "This table shown holds five samples.\n",
schmittu's avatar
schmittu committed
    "\n",
    "The feature names are `alcohol_content`, `bitterness`, `darkness`, `fruitiness` and `is_yummy`."
schmittu's avatar
schmittu committed
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
schmittu's avatar
schmittu committed
    "(Almost) all machine learning algorithms require that your data is numerical and/or categorial. In some applications it is not obvious how to transform data to a numerical presentation.\n",
    "\n",
schmittu's avatar
schmittu committed
    "**Definition**:\n",
schmittu's avatar
schmittu committed
    "\n",
    "*Categorical data*: data which has only a limited set of allowed values. A `taste` feature could only allow values `sour`, `bitter`, `sweet`, `salty`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
schmittu's avatar
schmittu committed
    "A straight-forward application for machine-learning on the previos beer dataset is: **\"can we predict `is_yummy` from the other features\"** ?\n",
schmittu's avatar
schmittu committed
    "\n",
schmittu's avatar
schmittu committed
    "In this case we would call the features `alcohol_content`, `bitterness`, `darkness`, `fruitiness` our **input features** and `is_yummy` our **target value**."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### How to represent images as  feature vectors ?\n",
    "\n",
    "To simplify our explanations we consider gray images only here. Computers represent images as matrices. Every cell in the matrix represents one pixel, and the numerical value in the matrix cell its gray value.\n",
    "\n",
    "As we said, most machine learning algorithms require that every sample is represented as a  vector containing numbers. \n",
    "\n",
    "So how can we represent images as vectors then ?\n",
schmittu's avatar
schmittu committed
    "\n",
    "`scikit-learn`  includes some example data sets which we load now:"
schmittu's avatar
schmittu committed
   ]
  },
  {
   "cell_type": "code",
schmittu's avatar
schmittu committed
   "execution_count": 8,
schmittu's avatar
schmittu committed
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.datasets import load_digits\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "code",
schmittu's avatar
schmittu committed
   "execution_count": 9,
schmittu's avatar
schmittu committed
   "metadata": {},
   "outputs": [],
   "source": [
schmittu's avatar
schmittu committed
    "dd = load_digits()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next we plot the first nine digits from this data set:"
schmittu's avatar
schmittu committed
   ]
  },
  {
   "cell_type": "code",
schmittu's avatar
schmittu committed
   "execution_count": 76,
schmittu's avatar
schmittu committed
   "metadata": {},
schmittu's avatar
schmittu committed
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAABAsAAACBCAYAAACmXjMaAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDIuMi4zLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvIxREBQAAEttJREFUeJzt3V9o3ed9x/HPd/YCWRN8FNYs4IQcO2kGvbEyicLomOXOHt0fJl3MIQ0bOrmxbzpkCCzeleU7+SKzdjGGRdfIsI6AulUqo7SzmJXRmxK5PiaL3YZEHJOYjTREUsICMYmfXVhZ8/Ps6Pf1zqNznq/eLyiN1S+Pnp/fOr9z+uRIspSSAAAAAAAAPvUrvd4AAAAAAADoLxwWAAAAAACACg4LAAAAAABABYcFAAAAAACggsMCAAAAAABQwWEBAAAAAACo4LAAAAAAAABUcFgAAAAAAAAqOCwAAAAAAAAVO3MsamYpx7qfGhgYcM3v3r279uz777/vWvvatWuu+U8++cQ175VSsm6sk7uh1xNPPFF7dudO35e1t+H6+rpr/i68m1L6YjcW6reO9913X+3Zxx9/3LX2hx9+6Jp//fXXXfN3oZiODz30kGvec0/96KOPXGtfuXLFNc899e7s2LGj9myz2XSt/eabbzp3k10xj0XPc50kXb9+vfZsp9Nx7qbvFNPRK+drnMuXL3u3k1sxHR988EHXvOe+6v3/Mvfee69r3vvc+Oqrr9aevXHjhm7cuFHEc+Mjjzzimm80GrVn3333Xdfa77zzjms+9+sb1XwsZjksyO3gwYOu+ampqdqzi4uLrrWPHz/uml9dXXXN46aZmZnas54HuiSdOHHCNb+wsOCavwtXc3+CXhkeHq49Oz8/71q73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T/6O2uuoaO5ds2DSU6RhC4oURHOhYmcMdt01CiYwSBG0p07LuO2b8NIaX0sZl9U9KPJO2Q9O2U0mu5P2+P/Iak75mZdPPv9h9TSj/s7Za6g47ld9xmDSU6RhCyoURHOhYpZMdt1lCiYwQhG0p07MeO2X91IgAAAAAAKMtWfBsCAAAAAAAoCIcFAAAAAACggsMCAAAAAABQwWEBAAAAAACo4LAAAAAAAABUcFgAAAAAAAAqOCwAAAAAAAAVHBYAAAAAAICK/wHIF1w8ycQXMQAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 1296x360 with 9 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
schmittu's avatar
schmittu committed
   "source": [
    "N = 9\n",
schmittu's avatar
schmittu committed
    "\n",
schmittu's avatar
schmittu committed
    "plt.figure(figsize=(2 * N, 5))\n",
schmittu's avatar
schmittu committed
    "\n",
    "for i, image in enumerate(dd.images[:N]):\n",
    "    plt.subplot(1, N, i + 1)\n",
schmittu's avatar
schmittu committed
    "    plt.imshow(image, cmap=\"gray\")"
   ]
  },
schmittu's avatar
schmittu committed
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
schmittu's avatar
schmittu committed
    "And this is the first image from the data set, it is a 8 x 8 matrix with values 0 to 15. The range 0 to 15 is fixed for this specific data set. Other formats allow e.g. values 0..255 or floating point values in the range 0 to 1."
schmittu's avatar
schmittu committed
   ]
  },
schmittu's avatar
schmittu committed
  {
   "cell_type": "code",
schmittu's avatar
schmittu committed
   "execution_count": 11,
schmittu's avatar
schmittu committed
   "metadata": {},
schmittu's avatar
schmittu committed
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(8, 8)\n",
      "[[ 0.  0.  5. 13.  9.  1.  0.  0.]\n",
      " [ 0.  0. 13. 15. 10. 15.  5.  0.]\n",
      " [ 0.  3. 15.  2.  0. 11.  8.  0.]\n",
      " [ 0.  4. 12.  0.  0.  8.  8.  0.]\n",
      " [ 0.  5.  8.  0.  0.  9.  8.  0.]\n",
      " [ 0.  4. 11.  0.  1. 12.  7.  0.]\n",
      " [ 0.  2. 14.  5. 10. 12.  0.  0.]\n",
      " [ 0.  0.  6. 13. 10.  0.  0.  0.]]\n"
     ]
    }
   ],
schmittu's avatar
schmittu committed
   "source": [
schmittu's avatar
schmittu committed
    "print(dd.images[0].shape)\n",
    "print(dd.images[0])"
schmittu's avatar
schmittu committed
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
schmittu's avatar
schmittu committed
    "To transform such an image to a feature vector we just have to concatenate the rows to one single vector of size 64:"
schmittu's avatar
schmittu committed
   ]
  },
  {
   "cell_type": "code",
schmittu's avatar
schmittu committed
   "execution_count": 12,
schmittu's avatar
schmittu committed
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(64,)\n",
      "[ 0.  0.  5. 13.  9.  1.  0.  0.  0.  0. 13. 15. 10. 15.  5.  0.  0.  3.\n",
      " 15.  2.  0. 11.  8.  0.  0.  4. 12.  0.  0.  8.  8.  0.  0.  5.  8.  0.\n",
      "  0.  9.  8.  0.  0.  4. 11.  0.  1. 12.  7.  0.  0.  2. 14.  5. 10. 12.\n",
      "  0.  0.  0.  0.  6. 13. 10.  0.  0.  0.]\n"
     ]
    }
   ],
   "source": [
    "vector = dd.images[0].flatten()\n",
    "print(vector.shape)\n",
    "print(vector)"
   ]
  },
  {
   "cell_type": "markdown",
schmittu's avatar
schmittu committed
   "metadata": {},
   "source": [
schmittu's avatar
schmittu committed
    "### How to present textual data as feature vectors ?"
schmittu's avatar
schmittu committed
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
schmittu's avatar
schmittu committed
    "If we start a machine learning project for texts, we first have to choose and fix an enumerated dictionary or words for this project. The final representation of texts as feature vectors depends on this dictionary. \n",
    "\n",
    "Such a dictionary can be very large, but for the sake of simplicity we use a very small enumerated dictionary to explain the overall procedure:\n",
schmittu's avatar
schmittu committed
    "\n",
    "\n",
    "| Word     | Index |\n",
    "|----------|-------|\n",
    "| like     | 0     |\n",
    "| dislike  | 1     |\n",
    "| american | 2     |\n",
    "| italian  | 3     |\n",
    "| beer     | 4     |\n",
    "| pizza    | 5     |\n",
    "\n",
    "To \"vectorize\" a given text we count the words in the text which also exist in the vocabulary and put the counts at the given position `Index`.\n",
    "\n",
    "E.g. `\"I dislike american pizza, but american beer is nice\"`:\n",
    "\n",
schmittu's avatar
schmittu committed
    "| Word     | Index | Count |\n",
schmittu's avatar
schmittu committed
    "|----------|-------|-------|\n",
schmittu's avatar
schmittu committed
    "| like     | 0     | 0     |\n",
schmittu's avatar
schmittu committed
    "| dislike  | 1     | 1     |\n",
    "| american | 2     | 2     |\n",
    "| italian  | 3     | 0     |\n",
    "| beer     | 4     | 1     |\n",
    "| pizza    | 5     | 1     |\n",
    "\n",
schmittu's avatar
schmittu committed
    "The according feature vector is the `Count` column, which is:\n",
schmittu's avatar
schmittu committed
    "\n",
schmittu's avatar
schmittu committed
    "`[0, 1, 2, 0, 1, 1]`\n",
    "\n",
    "In real case scenarios the dictionary is much bigger, this results then in vectors with only few non-zero entries (so called sparse vectors)."
schmittu's avatar
schmittu committed
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
schmittu's avatar
schmittu committed
    "And this is how we can compute such a word vector using Python:"
schmittu's avatar
schmittu committed
   ]
  },
  {
   "cell_type": "code",
schmittu's avatar
schmittu committed
   "execution_count": 77,
schmittu's avatar
schmittu committed
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 0 1 1]\n"
     ]
    }
   ],
   "source": [
    "from sklearn.feature_extraction.text import CountVectorizer\n",
    "from itertools import count\n",
    "\n",
schmittu's avatar
schmittu committed
    "vocabulary = {\"like\": 0, \"dislike\": 1, \"american\": 2, \"italian\": 3, \"beer\": 4, \"pizza\": 5}\n",
schmittu's avatar
schmittu committed
    "\n",
schmittu's avatar
schmittu committed
    "vectorizer = CountVectorizer(vocabulary=vocabulary)\n",
schmittu's avatar
schmittu committed
    "\n",
schmittu's avatar
schmittu committed
    "# create count vector for a pice of text:\n",
    "vector = vectorizer.fit_transform([\"I dislike american pizza. But american beer is nice\"]).toarray().flatten()\n",
schmittu's avatar
schmittu committed
    "print(vector)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
schmittu's avatar
schmittu committed
    "## Taxonomy of machine learning\n",
    "\n",
schmittu's avatar
schmittu committed
    "Most applications of ML belong to two categories: **supervised** and **unsupervised** learning.\n",
schmittu's avatar
schmittu committed
    "\n",
schmittu's avatar
schmittu committed
    "### Supervised learning \n",
schmittu's avatar
schmittu committed
    "\n",
schmittu's avatar
schmittu committed
    "In supervised learning the the data comes with an additional target value that we want to predict. Such a problem can be either \n",
    "\n",
    "- **classification**: we want to predict a categorical value.\n",
schmittu's avatar
schmittu committed
    "    \n",
schmittu's avatar
schmittu committed
    "- **regression**: we want to predict numbers in a given range.\n",
schmittu's avatar
schmittu committed
    "    \n",
schmittu's avatar
schmittu committed
    "  \n",
schmittu's avatar
schmittu committed
    "\n",
    "Examples for supervised learning:\n",
    "\n",
    "- Classification: Predict the class `is_yummy`  based on the attributes `alcohol_content`,\t`bitterness`, \t`darkness` and `fruitiness`. (two class problem).\n",
    "\n",
    "- Classification: predict the digit-shown based on a 8 x 8 pixel image (this is a multi-class problem).\n",
    "\n",
schmittu's avatar
schmittu committed
    "- Regression: Predict the length of a salmon based on its age and weight."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Unsupervised learning \n",
    "\n",
    "In unsupervised learning, in which the training data consists of samples without any corresponding target values, one tries to find structure in data. Some common applications are\n",
    "\n",
    "- Clustering: find groups in data.\n",
    "- Density estimation: find a probability distribution in your data.\n",
    "- Dimension reduction (e.g. PCA): find latent structures in your data.\n",
    "\n",
    "Examples for unsupervised learning:\n",
    "\n",
    "- Can we split up our beer data set into sub groups of similar beers ?\n",
    "- Can we reduce our data set because groups of features are somehow correlated ?\n",
schmittu's avatar
schmittu committed
    "\n",
schmittu's avatar
schmittu committed
    "<table>\n",
    "    <tr>\n",
    "    <td><img src=\"./cluster-image.png/\" width=60%></td>\n",
    "    <td><img src=\"./nonlin-pca.png/\" width=60%></td>\n",
    "    </tr>\n",
    "    <tr>\n",
    "        <td><center>Clustering</center></td>\n",
    "        <td><center>Dimension reduction: detecting 2D structure in 3D data</center></td>\n",
    "    </tr>\n",
    "</table>\n",
schmittu's avatar
schmittu committed
    "\n",
    "\n",
    "\n",
    "This course will only introduce concepts and methods from **supervised learning**."
schmittu's avatar
schmittu committed
   ]
  },
schmittu's avatar
schmittu committed
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## How to apply machine learning in practice ?\n",
    "\n",
    "Application of machine learning in practice consists of several phases:\n",
    "\n",
schmittu's avatar
schmittu committed
    "1. Understand and clean your data.\n",
    "1. Learn / train a model \n",
schmittu's avatar
schmittu committed
    "2. Analyze model for its quality / performance\n",
    "2. Apply this model to new incoming data\n",
    "\n",
schmittu's avatar
Loading
Loading full blame...