{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "ed7e710b-739e-4d6c-c80c-7d182bbf8761"
      },
      "source": [
        "# Moving average on the target\n",
        "\n",
        "We use a moving average of window-size 10.000 over the target column `is_duplicate` and plot the mean for every window."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e08a0ae4-2b6a-41cf-e8c2-db6324ea8fb4"
      },
      "outputs": [],
      "source": [
        "%matplotlib inline\n",
        "\n",
        "import matplotlib.pyplot as plt\n",
        "import numpy as np\n",
        "import pandas as pd\n",
        "\n",
        "def chunks(l, n):\n",
        "    \"\"\"Yield successive n-sized chunks from l.\"\"\"\n",
        "    for i in range(0, len(l), n):\n",
        "        yield l[i:i + n]\n",
        "\n",
        "df_train = pd.read_csv(\"../input/train.csv\")\n",
        "y = list(df_train[\"is_duplicate\"])\n",
        "\n",
        "means = []\n",
        "for chunk in chunks(y, 10000):\n",
        "    means.append(np.mean(chunk))\n",
        "\n",
        "plt.plot(means)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "5f8b639f-3b12-2f04-ccd4-aa95b607e1d2"
      },
      "outputs": [],
      "source": [
        "means = []\n",
        "for chunk in chunks(y, 750):\n",
        "    means.append(np.mean(chunk))\n",
        "\n",
        "plt.plot(means)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "3358f4da-82e7-0726-293e-f4941eba7b0c"
      },
      "outputs": [],
      "source": [
        "print(np.mean(y))\n",
        "print(np.mean(np.r_[y, np.zeros(500000)]))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "7ff10a9e-b33e-ad9e-a1f3-bb8d4224fe13"
      },
      "outputs": [],
      "source": ""
    }
  ],
  "metadata": {
    "_change_revision": 0,
    "_is_fork": false,
    "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.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}