{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "b5fb0ca9-b8a4-02ef-f7d5-5458f927500f"
      },
      "source": [
        "# Checking conversion of numbers from 255 greyscale to black & white with different brightness thresholds"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "58166aa1-b46f-1868-adb5-a9654e0281e7"
      },
      "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",
        "\n",
        "import matplotlib.pyplot as plt\n",
        "%matplotlib inline\n",
        "\n",
        "# Input data files are available in the \"../input/\" directory.\n",
        "# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory\n",
        "\n",
        "from subprocess import check_output\n",
        "print(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n",
        "\n",
        "# Any results you write to the current directory are saved as output."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "a2d209b4-e4de-c4cb-dca3-afb348636f9e"
      },
      "outputs": [],
      "source": [
        "# load train set\n",
        "trn = pd.read_csv(\"../input/train.csv\")\n",
        "\n",
        "# Split labels and images\n",
        "trn_img = (trn.ix[:,1:].values).astype('float32')\n",
        "trn_lbl = trn.ix[:,0].values.astype('int32')\n",
        "\n",
        "# Load test set\n",
        "tst_img = (pd.read_csv(\"../input/test.csv\").values).astype('float32')\n",
        "\n",
        "# Show a few labels to pick a subset\n",
        "trn_lbl[1:100]"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "afa14463-dd55-f023-1725-f7f8a8f2b58c"
      },
      "outputs": [],
      "source": [
        "# Take subset and reshape\n",
        "vis_img = trn_img[30006:30014,:]\n",
        "vis_lbl = trn_lbl[30006:30014]\n",
        "vis_img = vis_img.reshape(vis_img.shape[0], 28, 28)\n",
        "vis_img.shape"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e0646680-c407-78d9-b65f-cc0e4129c4b4"
      },
      "outputs": [],
      "source": [
        "# Visualize greyscale numbers\n",
        "plt.subplots(1, 8, figsize=(12,2))\n",
        "for i in range(0, 8):\n",
        "    plt.subplot(180 + (i+1))\n",
        "    plt.imshow(vis_img[i], cmap=plt.get_cmap('gray'))\n",
        "    plt.title(vis_lbl[i]);\n",
        "    plt.axis('off')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "834a4748-9442-a61c-1eeb-569a4b7926e9"
      },
      "source": [
        "## PIcked this special slice because 4th digit failed to me on prediction for 112 threshold, now I can see why, predicted was 3 ... no wonder"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "7d2e63b2-5664-9f42-077b-4eb308b9e3fa"
      },
      "outputs": [],
      "source": [
        "# Threshold will be from 64 brightness to 192 in steps of 16\n",
        "plt.subplots(9, 9, figsize=(13, 14))\n",
        "for k in range(0, 9):\n",
        "\tthreshold = k * 16 + 64\n",
        "\tbaw_img = vis_img.__ge__(threshold)\n",
        "\tplt.subplot(9,9,k*9+1)\n",
        "\tplt.axis('off')\n",
        "\tplt.title(threshold)\n",
        "\tfor i in range(0, 8):\n",
        "\t\tplt.subplot(9,9,k*9+i+2)\n",
        "\t\tplt.imshow(baw_img[i], cmap=plt.get_cmap('gray'))\n",
        "\t\tplt.axis('off')\n",
        "\t\tif k == 0:\n",
        "\t\t\tplt.title(vis_lbl[i])"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "2ce085b3-ee14-42af-016f-0fcabec37026"
      },
      "outputs": [],
      "source": [
        "# Picking 112 as threshold\n",
        "#threshold = 112\n",
        "#csv_trn_lbl = trn_lbl\n",
        "#csv_trn_img = trn_img.__ge__(threshold)\n",
        "#csv_tst_img = tst_img.__ge__(threshold)\n",
        "\n",
        "#np.savetxt('csv_trn_lbl.csv', csv_trn_lbl, fmt='%1d', delimiter=',')\n",
        "#np.savetxt('csv_trn_img.csv', csv_trn_img, fmt='%1d', delimiter=',')\n",
        "#np.savetxt('csv_tst_img.csv', csv_tst_img, fmt='%1d', delimiter=',')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "17bf73df-a0bc-b43b-482c-2414bc0f36b6"
      },
      "source": [
        "Used Octave and logistic regression and splitted the training set in 30k for training and 12k for validation\n",
        "\n",
        "## 112 threshold got a 90.591667 accuracy"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "f2cf8b34-9ab2-6c1a-2915-b1e317f8958b"
      },
      "outputs": [],
      "source": [
        "# Picking 176 as threshold (skipping test set this time)\n",
        "#threshold = 176\n",
        "#csv_trn_lbl = trn_lbl\n",
        "#csv_trn_img = trn_img.__ge__(threshold)\n",
        "#csv_tst_img = tst_img.__ge__(threshold)\n",
        "\n",
        "#np.savetxt('csv_trn_lbl_176.csv', csv_trn_lbl, fmt='%1d', delimiter=',')\n",
        "#np.savetxt('csv_trn_img_176.csv', csv_trn_img, fmt='%1d', delimiter=',')\n",
        "#np.savetxt('csv_tst_img_176.csv', csv_tst_img, fmt='%1d', delimiter=',')"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "e7267929-5f00-8ab7-43f8-31d64f9cc7ee"
      },
      "source": [
        "## 176 threshold accuracy went down to: 89.650000"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "efeb03fd-4ed6-1c24-b1be-86c56669acde"
      },
      "source": [
        "## greyscale accuracy with same conditions is: 91.150000\n",
        "## thus 112 threshold was really a good aproximation"
      ]
    }
  ],
  "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
}