{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "cd81f348-f035-cc46-fe4d-bdcceb270032"
      },
      "source": [
        "# Bosch Python 3 Starter Script"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "579ce5c0-1e2d-294e-c701-428460b59ee0"
      },
      "outputs": [],
      "source": [
        "# This Python 3 environment comes with many helpful analytics libraries installed\n",
        "# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n",
        "# For example, here's several helpful packages to load in \n",
        "\n",
        "import numpy as np   # linear algebra\n",
        "import pandas as pd  # data processing, CSV file I/O (e.g. pd.read_csv)\n",
        "from subprocess import check_output\n",
        "\n",
        "# Input data files are available in the \"../input/\" directory.\n",
        "# Any results you write to the current directory are saved as output.\n",
        "\n",
        "def my_sys_call(command):\n",
        "    print(check_output(command.split()).decode(\"utf8\"))"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "6a1269e1-1b4f-c30c-41ea-188e0f8f551a"
      },
      "outputs": [],
      "source": [
        "#my_sys_call(\"head -2 ../input/train_categorical.csv\")\n",
        "#my_sys_call(\"head -2 ../input/train_date.csv\")\n",
        "#my_sys_call(\"head -2 ../input/train_numeric.csv\")\n",
        "my_sys_call(\"head -8 ../input/sample_submission.csv\")\n",
        "my_sys_call(\"tail -8 ../input/sample_submission.csv\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "28983ce6-8d0c-061e-88ec-dcfc618d2f4d"
      },
      "outputs": [],
      "source": [
        "def get_headers(filename,rows=1):\n",
        "    df = pd.read_csv(filename, nrows=rows)\n",
        "    return df.columns.values\n",
        "\n",
        "def get_all_headers(data_folder = \"../input\"):\n",
        "    date_headers = get_headers(data_folder + \"/train_date.csv\")\n",
        "    numeric_headers = get_headers(data_folder + \"/train_numeric.csv\")\n",
        "    categorical_headers = get_headers(data_folder + \"/train_categorical.csv\")\n",
        "    return date_headers,numeric_headers,categorical_headers"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "912d59f4-508c-b697-1098-6ee821bbb95d"
      },
      "outputs": [],
      "source": [
        "date_headers,numeric_headers,categorical_headers = get_all_headers()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "04beed87-a1e6-6bc6-67ab-7f7d61250ca7"
      },
      "outputs": [],
      "source": [
        "def load_response_column(numeric_cols, data_folder = \"../input\"):\n",
        "    df = pd.read_csv(data_folder + \"/train_numeric.csv\", \n",
        "        index_col = 0, header = 0, #nrows=100,\n",
        "        usecols = numeric_cols)\n",
        "    return df\n",
        "\n",
        "numeric_cols = [0, len(numeric_headers)-1]\n",
        "response_col = load_response_column(numeric_cols)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "0ec0064c-6237-336f-77e3-b04e65ab948c"
      },
      "outputs": [],
      "source": [
        "len(df)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "07c64bfc-f5d3-813b-c878-8106e5e2426a"
      },
      "outputs": [],
      "source": [
        "def describe_response_column(df):\n",
        "    response_levels = df['Response'].unique()\n",
        "    print(\"Unique values in response column: {}\".format(response_levels))\n",
        "    X_pos = df[df['Response']==1]\n",
        "    X_neg = df[df['Response']==0]\n",
        "    n_rows = len(df)\n",
        "    pos_rows = X_neg.shape[0]\n",
        "    neg_rows = X_pos.shape[0]\n",
        "    print(\"Rows with Response==0: {}/{}  ({}%)\".format(pos_rows,n_rows,100*pos_rows/n_rows))\n",
        "    print(\"Rows with Response==1: {}/{}  ({}%)\".format(neg_rows,n_rows,100*neg_rows/n_rows))\n",
        "    \n",
        "describe_response_column(response_col)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "736e5f9b-517a-7e69-c805-1c3bb37671aa"
      },
      "outputs": [],
      "source": [
        "submission = pd.read_csv(\"../input/sample_submission.csv\", index_col=0)\n",
        "#submission[\"Response\"] = ...  #fill in response here\n",
        "submission.to_csv(\"submission.csv.gz\", compression=\"gzip\")"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "df5bc4b1-5238-85f3-e4e6-1936f9e58dfb"
      },
      "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.5.2"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}