{"metadata": {"kernelspec": {"name": "python3", "display_name": "Python 3", "language": "python"}, "_is_fork": false, "_change_revision": 0, "language_info": {"file_extension": ".py", "mimetype": "text/x-python", "name": "python", "pygments_lexer": "ipython3", "codemirror_mode": {"name": "ipython", "version": 3}, "version": "3.6.3", "nbconvert_exporter": "python"}}, "nbformat_minor": 1, "nbformat": 4, "cells": [{"metadata": {"_cell_guid": "654456b6-e648-0379-0d66-1cc97af6d00d", "_uuid": "05d8df55804221a7b4c3af45b952b75122b90eaf"}, "source": ["**Import all required libraries**\n", "==============================="], "cell_type": "markdown"}, {"metadata": {"_cell_guid": "e5b02688-c589-5a89-e11c-837c6a99eb6e", "collapsed": true, "_uuid": "8248421e23ce47bd67acea96ad97740b02965eb3"}, "source": ["import numpy as np\n", "import pandas as pd "], "cell_type": "code", "outputs": [], "execution_count": 1}, {"metadata": {"_cell_guid": "22a7fd70-ab61-432d-24cb-93e558414495", "_uuid": "bc5647dfa72b8ca7fc9c554276cdc047de352d23"}, "source": ["**Load Train and Test data**\n", "============================"], "cell_type": "markdown"}, {"metadata": {"_cell_guid": "05226b08-226a-1a00-044d-a0e6b2101388", "_uuid": "b564bb129a7aacb29311604a5163a08d7b761a9c"}, "source": ["train = pd.read_csv('../input/train.csv')\n", "test = pd.read_csv('../input/test.csv')\n", "\n", "X = train.drop([\"label\"], axis=1)\n", "X = X.values.astype('int32')\n", "print(\"loaded\")"], "cell_type": "code", "outputs": [], "execution_count": 4}, {"metadata": {"_cell_guid": "1ae10fe0-dde9-7659-f53d-1a1bd625cfb1", "_uuid": "fa6721123b74d73ad9fa3ab3dbe2050c93dc8cab"}, "source": ["#reset\n", "X = train.drop([\"label\"], axis=1)\n", "X = X.values.astype('int32')\n", "\n", "# clean data\n", "print(X)\n", "cut = 2#50\n", "X[X <= cut] = 0\n", "X[X > cut] = 1\n", "print(X)"], "cell_type": "code", "outputs": [], "execution_count": 41}, {"metadata": {"_cell_guid": "dc04e6a4-4edd-4007-97b9-cdf81a5839e2", "_uuid": "25e267bd34e13ed5498cdb18ba31191c776db93f"}, "source": ["# create model\n", "X1 = np.split(X,2)[0]\n", "X2 = np.split(X,2)[1]\n", "\n", "y1 = np.split(train[\"label\"],2)[0]\n", "y2 = np.split(train[\"label\"],2)[1]\n", "\n", "from sklearn.tree import DecisionTreeClassifier\n", "from sklearn.tree import DecisionTreeRegressor\n", "\n", "model = DecisionTreeClassifier()\n", "\n", "model.fit(X2,y2) # easier to use 2 first than fix indexing\n", "\n", "p = model.predict(X1)\n", "\n", "total = 0\n", "for i in range(200):\n", "    if y1[i] == p[i]:\n", "        total += 1\n", "\n", "print(total/200) # print accuracy"], "cell_type": "code", "outputs": [], "execution_count": 42}, {"metadata": {"_cell_guid": "60957d82-c76f-4822-28ff-def7011a34fa", "_uuid": "67543e8825948c456e6fac35666c072258444f62"}, "source": ["Debug incorrect guesses to see what the model is not good at identifying"], "cell_type": "markdown"}, {"metadata": {"_cell_guid": "1541678d-a08b-d2b2-1e1e-eabf882baaec", "_uuid": "524b6b057c11e823d0c7e50de277e76196bc1e1c"}, "source": ["import matplotlib.pyplot as plt\n", "%matplotlib inline\n", "\n", "X_train = X\n", "X_train = X_train.reshape(X_train.shape[0], 28, 28)\n", "\n", "print('start')\n", "fig, axes = plt.subplots(nrows=2, ncols=8)\n", "\n", "I = 0\n", "for ax in axes.flat[:]:\n", "    for i in range(I+1, 500):\n", "        if(p[i]) != train[\"label\"][i]:\n", "            I = i\n", "            break\n", "    ax.set_title(p[I]);\n", "    ax.set_yticklabels([])\n", "    ax.set_xticklabels([])\n", "    ax.imshow(X_train[i], cmap=plt.get_cmap('gray'))"], "cell_type": "code", "outputs": [], "execution_count": 43}, {"metadata": {"_cell_guid": "7986423d-d554-45da-a761-97ff49fb8b92", "_uuid": "86e1562c8ac0e1e606dcc5f104f4e888d59270c5"}, "source": ["**Export results**\n", "============================"], "cell_type": "markdown"}, {"metadata": {"_cell_guid": "6949468c-fd27-19c5-15c7-0b357a961003", "_uuid": "5048d3d49c98da5641ea1aabff7b687187ce5a28"}, "source": ["model.fit(X,train[\"label\"])\n", "predictions = model.predict(test)\n", "\n", "submissions = pd.DataFrame({\"ImageId\": list(range(1,len(predictions)+1)),\n", "                         \"Label\": predictions})\n", "submissions.to_csv(\"DR.csv\", index=False, header=True)"], "cell_type": "code", "outputs": [], "execution_count": 47}]}