Skip to content
Snippets Groups Projects
01_introduction.ipynb 33 KiB
Newer Older
  • Learn to ignore specific revisions
  • schmittu's avatar
    schmittu committed
    {
     "cells": [
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
    
    schmittu's avatar
    schmittu committed
        "# Chapter 1: General Introduction"
    
    schmittu's avatar
    schmittu committed
       ]
      },
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
        "## What is machine learning ?"
       ]
      },
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
        "- Discipline in the overlap of computer science and statistics\n",
    
    schmittu's avatar
    schmittu committed
        "- Subset of Artificial Intelligence (AI)\n",
    
    schmittu's avatar
    schmittu committed
        "- Term \"Machine Learning\" was first used in 1959 by AI pioneer Arthur Samuel\n",
    
    schmittu's avatar
    schmittu committed
        "\n",
        "- **Learn models from data**\n"
    
    schmittu's avatar
    schmittu committed
       ]
      },
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
    
    schmittu's avatar
    schmittu committed
        "So the field is not as new as one might think, but due to more available data, processing power and development of better algorithms more applications of machine learning appeared during the last 15 years."
       ]
      },
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
    
    schmittu's avatar
    schmittu committed
        "## About  models\n",
    
    schmittu's avatar
    schmittu committed
        "\n",
    
    schmittu's avatar
    schmittu committed
        "Model examples: \n",
    
    schmittu's avatar
    schmittu committed
        "\n",
    
    schmittu's avatar
    schmittu committed
        "   1. Where will my car 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 mathematically explicit models (formulas). \n",
        "\n",
        "For the  questions 3 and 4 it is difficult to develop explicitly formulated models. \n",
        "\n",
        "These problems 3 and 4 have the following in common:\n",
    
    schmittu's avatar
    schmittu committed
        "\n",
        "- No exact model known or implementable\n",
    
    schmittu's avatar
    schmittu committed
        "- Vague understanding of the problem domain\n",
        "- Enough data with sufficient (implicit) information available\n",
        "\n",
        "E.g. for the spamming example:\n",
        "\n",
        "- We have no explicit formula for such a task\n",
        "- We know that specific words are specific for spam emails, other words are specific for my personal and job emails.\n",
        "- My mailbox is full with examples for spam vs non-spam.\n",
        "\n",
        "\n",
        "**In such cases machine learning offers approaches to build models based on example data.**\n"
    
    schmittu's avatar
    schmittu committed
       ]
      },
    
    schmittu's avatar
    schmittu committed
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": []
      },
    
    schmittu's avatar
    schmittu committed
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
        "## Some history\n",
        "\n",
    
    schmittu's avatar
    schmittu committed
        "Rough time line with a few examples\n",
    
    schmittu's avatar
    schmittu committed
        "\n",
        " \n",
        "    1812: Bayes Theorem\n",
        "    1913: Markov Chains\n",
        "    1951: First neural network\n",
        "    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: Application of naive Bayes Classifier for Spam detection\n",
    
    schmittu's avatar
    schmittu committed
        "    2000+: Deep learning"
       ]
      },
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
    
    schmittu's avatar
    schmittu committed
        "## What are features ?\n",
    
    schmittu's avatar
    schmittu committed
        "\n",
    
    schmittu's avatar
    schmittu committed
        "In most cases we can arange data used for machine learning as a matrix:"
    
    schmittu's avatar
    schmittu committed
       ]
      },
      {
       "cell_type": "code",
    
    schmittu's avatar
    schmittu committed
       "execution_count": 3,
    
    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",
           "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": 3,
    
    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",
        "\n",
        "- every row of such a matrix is called a **sample** or **feature vector**. \n",
        "- every column name is called a **feature name** or **attribute**.\n",
        "- the cells are **feature values**."
       ]
      },
    
    schmittu's avatar
    schmittu committed
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
    
    schmittu's avatar
    schmittu committed
        "This table holds five samples.\n",
        "\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",
        "Definition:\n",
        "\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": [
        "### How to represent images as  feature vectors\n",
    
    schmittu's avatar
    schmittu committed
        "\n",
        "Computers represent images as matrices. Every cell in the matrix represents one pixel, and the value in the matrix cell its color.\n",
        "\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": 4,
    
    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": 5,
    
    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": 6,
    
    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+vfZsp9Nx7qbv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HvrOz1xsI6HdSStfM7EFJ58zsZymlf+/1puBGxxjoWD4axkDHGOgYAx3LR8MY+r7jVryz4JqkRz7z54c3PhZSSunaxn+/I+l7uvl2mgjoWL5t1VCiYwRBG0p0pGOBgnbcVg0lOkYQtKFEx77ruBWHBa9I+pKZ7TGzeyQ9Len7W/B5t5yZfcHM7v/0nyX9vqT/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",
        "plt.figure(figsize=(2 * N, 5))\n",
        "for i, image in enumerate(dd.images[:N], 1):\n",
        "    plt.subplot(1, N, i)\n",
        "    plt.imshow(image, cmap=\"gray\")"
       ]
      },
    
    schmittu's avatar
    schmittu committed
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
        "And this is the first image from the data set, it is a 8 x 8 matrix with values 0 to 15:"
       ]
      },
    
    schmittu's avatar
    schmittu committed
      {
       "cell_type": "code",
    
    schmittu's avatar
    schmittu committed
       "execution_count": 7,
    
    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 vectore 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": 13,
       "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": [
        "To transform some text into a feature vector, we first need a enumerated dictionary. Such a dictionary can be very large, but for the sake of simplicity we use a very small dictionary to explain the overall procedure:\n",
        "\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",
        "| like     | 0     | 1     |\n",
        "| 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",
        "`[0, 1, 2, 0, 1, 1]`"
    
    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": 8,
    
    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",
        "# map words to index in created vector:\n",
        "vocabulary = [\"like\", \"dislike\", \"american\", \"italian\", \"beer\", \"pizza\"]\n",
        "\n",
        "vectorizer = CountVectorizer(vocabulary=dict(zip(vocabulary, count())))\n",
        "\n",
        "# crate count vector for a pice of text:\n",
        "vector = vectorizer.fit_transform([\"I dislike american pizza. But american beer is nice\"]).toarray()[0]\n",
        "print(vector)"
       ]
      },
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
    
    schmittu's avatar
    schmittu committed
        "## Taxonomy of machine learning\n",
        "\n",
        "We can separate learning problems in a few large categories: **supervised** and **unsupervised** learning.\n",
        "\n",
        "In **supervised learning** the the data comes with additional attributes that we want to predict. Such a problem can be either \n",
        "\n",
    
    schmittu's avatar
    schmittu committed
        "- **classification**: samples belong to two or more discrete classes and we want to learn from already labeled data how to predict the class of unlabeled data. This is the same as saying, that the output is categorical.\n",
    
    schmittu's avatar
    schmittu committed
        "    \n",
        "- **regression**: if the desired output consists of one or more continuous variables, then the task is called regression.\n",
        "    \n",
        "    \n",
        "\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",
        "- Regression: Predict the length of a salmon based on its age and weight.\n",
        "\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. Common applications are\n",
        "\n",
        "- Clustering \n",
        "- Density estimation\n",
        "- Dimension reduction (PCA, ...)\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",
        "1. Learn / train a model from example data\n",
        "2. Analyze model for its quality / performance\n",
        "2. Apply this model to new incoming data\n",
        "\n",
        "In practice steps 1. and 2. are iterated for different machine learning algorithms until performance is optimal or sufficient. "
       ]
      },
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
        "## Exercise section"
       ]
      },
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
        "Our example beer data set reflects the very personal opinion of one of the tutors which beer he likes and which not. To learn a predictive model and to understand influential factors all beers went through some lab analysis to measure alcohol content, bitterness, darkness and fruitiness."
       ]
      },
    
    schmittu's avatar
    schmittu committed
      {
       "cell_type": "code",
    
    schmittu's avatar
    schmittu committed
       "execution_count": 28,
       "metadata": {},
       "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",
           "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"
          ]
         },
         "execution_count": 28,
         "metadata": {},
         "output_type": "execute_result"
        }
       ],
       "source": [
        "import pandas as pd\n",
        "from sklearn.linear_model import LogisticRegression\n",
        "from sklearn.svm import SVC\n",
        "\n",
        "# read some data\n",
        "\n",
        "beer_data = pd.read_csv(\"beers.csv\")\n",
        "beer_data.head()"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": 29,
       "metadata": {},
       "outputs": [
        {
         "name": "stdout",
         "output_type": "stream",
         "text": [
          "   alcohol_content  bitterness  darkness  fruitiness\n",
          "0         3.739295    0.422503  0.989463    0.215791\n",
          "1         4.207849    0.841668  0.928626    0.380420\n",
          "2         4.709494    0.322037  5.374682    0.145231\n",
          "3         4.684743    0.434315  4.072805    0.191321\n",
          "4         4.148710    0.570586  1.461568    0.260218\n",
          "\n",
          "0    0\n",
          "1    0\n",
          "2    1\n",
          "3    1\n",
          "4    0\n",
          "Name: is_yummy, dtype: int64\n"
         ]
        }
       ],
       "source": [
        "# split matrix into features and labels\n",
        "features = beer_data.iloc[:, :-1]\n",
        "labels = beer_data.iloc[:, -1]\n",
        "\n",
        "print(features.head())\n",
        "print()\n",
        "print(labels.head())"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": 32,
       "metadata": {},
       "outputs": [
        {
         "name": "stdout",
         "output_type": "stream",
         "text": [
          "LogisticRegression(C=2, class_weight=None, dual=False, fit_intercept=True,\n",
          "          intercept_scaling=1, max_iter=100, multi_class='ovr', n_jobs=1,\n",
          "          penalty='l2', random_state=None, solver='liblinear', tol=0.0001,\n",
          "          verbose=0, warm_start=False)\n"
         ]
        }
       ],
       "source": [
        "classifier = LogisticRegression(C=1)\n",
        "classifier.fit(features, labels)\n",
        "print(classifier)"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": 34,
       "metadata": {},
       "outputs": [
        {
         "name": "stdout",
         "output_type": "stream",
         "text": [
          "225 examples\n",
          "199 labeled correctly\n"
         ]
        }
       ],
       "source": [
        "predicted_labels = classifier.predict(features)\n",
        "\n",
        "\n",
        "print(len(labels), \"examples\")\n",
        "print(sum(predicted_labels == labels), \"labeled correctly\")"
       ]
      },
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
        "## Comment\n",
        "Are you surprised that not all labels where predicted correctly ?\n",
        "\n",
        "Reasons for this can be:\n",
        "- missing information: maybe other features of beer which contribute to the rating where not measured or can not be measured.\n",
        "- noisy information: features can be noisy"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": null,
       "metadata": {},
       "outputs": [],
       "source": []
      },
      {
       "cell_type": "code",
       "execution_count": 27,
       "metadata": {},
       "outputs": [
        {
         "name": "stdout",
         "output_type": "stream",
         "text": [
          "(225,)\n",
          "(225,)\n",
          "205\n"
         ]
        }
       ],
       "source": [
        "classifier = SVC()\n",
        "classifier.fit(features, labels)\n",
        "\n",
        "predicted_labels = classifier.predict(features)\n",
        "\n",
        "print(predicted_labels.shape)\n",
        "print(labels.shape)\n",
        "print(sum(predicted_labels == labels))"
       ]
      },
      {
       "cell_type": "code",
       "execution_count": 1,
    
    schmittu's avatar
    schmittu committed
       "metadata": {},
    
    schmittu's avatar
    schmittu committed
       "outputs": [
        {
         "name": "stderr",
         "output_type": "stream",
         "text": [
          "/Users/uweschmitt/Projects/machinelearning-introduction-workshop/venv3.6/lib/python3.6/site-packages/ipykernel_launcher.py:9: UserWarning: get_ipython_dir has moved to the IPython.paths module since IPython 4.0.\n",
          "  if __name__ == '__main__':\n"
         ]
        },
        {
         "data": {
          "text/html": [
           "<style>\n",
           "    \n",
           "    @import url('http://fonts.googleapis.com/css?family=Source+Code+Pro');\n",
           "    \n",
           "    @import url('http://fonts.googleapis.com/css?family=Kameron');\n",
           "    @import url('http://fonts.googleapis.com/css?family=Crimson+Text');\n",
           "    \n",
           "    @import url('http://fonts.googleapis.com/css?family=Lato');\n",
           "    @import url('http://fonts.googleapis.com/css?family=Source+Sans+Pro');\n",
           "    \n",
           "    @import url('http://fonts.googleapis.com/css?family=Lora'); \n",
           "\n",
           "    \n",
           "    body {\n",
           "        font-family: 'Lora', Consolas, sans-serif;\n",
           "       \n",
           "        -webkit-print-color-adjust: exact important !;\n",
           "       \n",
           "    }\n",
           "    .rendered_html code\n",
           "    {\n",
           "        color: black;\n",
           "        background: #eaf0ff;\n",
           " \n",
           "        padding: 1pt;\n",
           "        font-family:  'Source Code Pro', Consolas, monocco, monospace;\n",
           "    }\n",
           "    \n",
           "    .CodeMirror pre {\n",
           "    font-family: 'Source Code Pro', monocco, Consolas, monocco, monospace;\n",
           "    }\n",
           "    \n",
           "    .cm-s-ipython span.cm-keyword {\n",
           "        font-weight: normal;\n",
           "     }\n",
           "     \n",
           "     strong {\n",
           "         background: #ffe7e7;\n",
           "         padding: 1pt;\n",
           "     }\n",
           "     \n",
           "    \n",
           "    div #notebook {\n",
           "        # font-size: 10pt; \n",
           "        line-height: 145%;\n",
           "        }\n",
           "        \n",
           "    li {\n",
           "        line-heigt: 145%;\n",
           "    }\n",
           "\n",
           "    div.output_area pre {\n",
           "        background: #fff9d8 !important;\n",
           "        padding: 5pt;\n",
           "       \n",
           "       -webkit-print-color-adjust: exact; \n",
           "        \n",
           "    }\n",
           " \n",
           "    \n",
           " \n",
           "    h1, h2, h3, h4 {\n",
           "        font-family: Kameron, arial;\n",
           "    }\n",
           "    \n",
           "    div#maintoolbar {display: none !important;}\n",
           "    </style>"
          ],
          "text/plain": [
           "<IPython.core.display.HTML object>"
          ]
         },
    
    schmittu's avatar
    schmittu committed
         "execution_count": 1,
    
    schmittu's avatar
    schmittu committed
         "metadata": {},
         "output_type": "execute_result"
        }
       ],
       "source": [
        "#REMOVEBEGIN\n",
        "# THE LINES BELOW ARE JUST FOR STYLING THE CONTENT ABOVE !\n",
        "\n",
        "from IPython import utils\n",
        "from IPython.core.display import HTML\n",
        "import os\n",
        "def css_styling():\n",
        "    \"\"\"Load default custom.css file from ipython profile\"\"\"\n",
        "    base = utils.path.get_ipython_dir()\n",
        "    styles = \"\"\"<style>\n",
        "    \n",
        "    @import url('http://fonts.googleapis.com/css?family=Source+Code+Pro');\n",
        "    \n",
        "    @import url('http://fonts.googleapis.com/css?family=Kameron');\n",
        "    @import url('http://fonts.googleapis.com/css?family=Crimson+Text');\n",
        "    \n",
        "    @import url('http://fonts.googleapis.com/css?family=Lato');\n",
        "    @import url('http://fonts.googleapis.com/css?family=Source+Sans+Pro');\n",
        "    \n",
        "    @import url('http://fonts.googleapis.com/css?family=Lora'); \n",
        "\n",
        "    \n",
        "    body {\n",
        "        font-family: 'Lora', Consolas, sans-serif;\n",
        "       \n",
        "        -webkit-print-color-adjust: exact important !;\n",
        "       \n",
        "    }\n",
        "    .rendered_html code\n",
        "    {\n",
        "        color: black;\n",
        "        background: #eaf0ff;\n",
        " \n",
        "        padding: 1pt;\n",
        "        font-family:  'Source Code Pro', Consolas, monocco, monospace;\n",
        "    }\n",
        "    \n",
        "    .CodeMirror pre {\n",
        "    font-family: 'Source Code Pro', monocco, Consolas, monocco, monospace;\n",
        "    }\n",
        "    \n",
        "    .cm-s-ipython span.cm-keyword {\n",
        "        font-weight: normal;\n",
        "     }\n",
        "     \n",
        "     strong {\n",
        "         background: #ffe7e7;\n",
        "         padding: 1pt;\n",
        "     }\n",
        "     \n",
        "    \n",
        "    div #notebook {\n",
        "        # font-size: 10pt; \n",
        "        line-height: 145%;\n",
        "        }\n",
        "        \n",
        "    li {\n",
        "        line-heigt: 145%;\n",
        "    }\n",
        "\n",
        "    div.output_area pre {\n",
        "        background: #fff9d8 !important;\n",
        "        padding: 5pt;\n",
        "       \n",
        "       -webkit-print-color-adjust: exact; \n",
        "        \n",
        "    }\n",
        " \n",
        "    \n",
        " \n",
        "    h1, h2, h3, h4 {\n",
        "        font-family: Kameron, arial;\n",
        "    }\n",
        "    \n",
        "    div#maintoolbar {display: none !important;}\n",
        "    </style>\"\"\"\n",
        "    return HTML(styles)\n",
        "css_styling()\n",
        "#REMOVEEND"
       ]
    
    schmittu's avatar
    schmittu committed
      },
      {
       "cell_type": "code",
       "execution_count": null,
       "metadata": {},
       "outputs": [],
       "source": []
    
    schmittu's avatar
    schmittu committed
      }
     ],
     "metadata": {
      "kernelspec": {
       "display_name": "Python 3",
       "language": "python",
       "name": "python3"
      },
      "language_info": {
       "codemirror_mode": {
        "name": "ipython",
        "version": 3
       },
       "file_extension": ".py",
       "mimetype": "text/x-python",
       "name": "python",
       "nbconvert_exporter": "python",
       "pygments_lexer": "ipython3",
       "version": "3.6.6"
      }
     },
     "nbformat": 4,
     "nbformat_minor": 2
    }