{"nbformat_minor": 1, "nbformat": 4, "cells": [{"cell_type": "markdown", "source": ["# Importing Library"], "metadata": {"_uuid": "3c59f40e8bd9bd7ca71411caaa458e910856b332", "_cell_guid": "31bd9c6f-16c3-4ce2-a096-1e0c8e86353a"}}, {"execution_count": null, "cell_type": "code", "outputs": [], "source": ["import numpy as np\n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "%matplotlib inline"], "metadata": {"collapsed": true, "_uuid": "9c875fafcd8c6fe60621df48bf208874b81c8226", "_cell_guid": "10d819bf-15f0-469e-8c87-d0b279313b52"}}, {"cell_type": "markdown", "source": ["# Dataset\n", "Dataset is taken from https://www.kaggle.com/c/digit-recognizer "], "metadata": {"_uuid": "e734e76be294f5c33c0e14b4494ba4edb18fc67e", "_cell_guid": "a785ec08-1673-4978-8a13-80a565c2982f"}}, {"execution_count": null, "cell_type": "code", "outputs": [], "source": ["df = pd.read_csv(\"../input/train.csv\")\n", "df.shape"], "metadata": {"_uuid": "f7de117a2bcc31d8f1fb347670b1d2dcc076a22b", "_cell_guid": "b22cf667-f7c6-4361-8688-e1ea0a7e57d9"}}, {"execution_count": null, "cell_type": "code", "outputs": [], "source": ["df.columns"], "metadata": {"_uuid": "9b470f2cf87604a70c9ad4e8736f90f0b145f40b", "_cell_guid": "75f3ae22-b501-4109-b796-49e555f4ad25"}}, {"cell_type": "markdown", "source": ["going to classify the digit 1 as 1  and rest of the digit as 0\n", "Took all the digit 1 image and equal number of other digit i.e dataset contain 4684 images of digit 1 and 4684 image of rest of the image overall(shape of data (9368, 785)) "], "metadata": {"_uuid": "f3923ae3acbe671c13d88716cb4758b997d56a5e", "_cell_guid": "4bb79687-93de-4be0-b602-5076f4e701ad"}}, {"execution_count": null, "cell_type": "code", "outputs": [], "source": ["def only1(x):\n", "    if x == 1:\n", "        return x\n", "    else:\n", "        return 0\n", "df[\"label\"]=df.label.apply(only1)"], "metadata": {"collapsed": true, "_uuid": "f7f12edee38c50fcec43778ad70fd2f5c6622225", "_cell_guid": "47fa0b11-9b87-4eea-80a2-7592b33cb685"}}, {"execution_count": null, "cell_type": "code", "outputs": [], "source": ["df.label.value_counts()"], "metadata": {"_uuid": "b0c151e945d5325c62c7831854d35d040afa8edf", "_cell_guid": "6d785cfa-4981-4f95-95e1-3dfe31fe4e8f"}}, {"execution_count": null, "cell_type": "code", "outputs": [], "source": ["df1 = df[df.label == 1]\n", "df2 = df[df.label == 0]\n", "df2 = df2.sample(n=4684)\n", "frame = [df1,df2]\n", "df_final = pd.concat(frame)\n", "df_final.shape\n", "df_final = df_final.sample(frac=1).reset_index(drop=True)\n"], "metadata": {"collapsed": true, "_uuid": "b962b4766319d7fdda985957b02ca1d732ced3f0", "_cell_guid": "3f0c954f-9b0c-49ed-9827-769d1ee89eff"}}, {"execution_count": null, "cell_type": "code", "outputs": [], "source": ["df_final.head(5)"], "metadata": {"_uuid": "e0b21f1d3fdd31afddadbc0a271d8d45c4830999", "_cell_guid": "e88cf7c6-e550-42bb-bef2-40d5233d9171"}}, {"execution_count": null, "cell_type": "code", "outputs": [], "source": ["df_y = df_final[\"label\"]\n", "df_x = df_final.iloc[: , 1:]\n", "df_y.values.reshape(1,9368)"], "metadata": {"_uuid": "711353e39d15d4bbbd263a2c18fb536dd6fcefbe", "_cell_guid": "db538897-8a00-4b6c-bbd6-13a76fa2c66b"}}, {"execution_count": null, "cell_type": "code", "outputs": [], "source": ["from sklearn.cross_validation import train_test_split\n", "x_train, x_test, y_train, y_test = train_test_split(df_x, df_y, test_size=0.3, random_state=42)"], "metadata": {"_uuid": "2cb89e4a3d74e04f50c1002fbf619a67f4c648f5", "_cell_guid": "25e4adb0-e6d7-4482-b230-7c69f93553df"}}, {"execution_count": null, "cell_type": "code", "outputs": [], "source": ["print (x_train.shape)\n", "print (x_test.shape)\n", "print (y_train.shape)\n", "print (y_test.shape)"], "metadata": {"_uuid": "c0a45befacd724f8186d262d7737847357044596", "_cell_guid": "113cceb9-f82c-42f2-b7e0-70bb7a62592b"}}, {"execution_count": null, "cell_type": "code", "outputs": [], "source": ["x_test = x_test.T\n", "x_train = x_train.T\n", "y_train = y_train.reshape(1,6557)\n", "y_train = y_train.T\n", "y_test = y_test.reshape(1,2811)\n", "y_test = y_test.T"], "metadata": {"_uuid": "67b8c3fc1b9be6553869e04eaa7919a7933e26b5", "_cell_guid": "0b8d4b10-677b-4a3e-876b-2f3cf50c859f"}}, {"execution_count": null, "cell_type": "code", "outputs": [], "source": ["y_train = y_train.T"], "metadata": {"collapsed": true, "_uuid": "7582569b2e4416220a8745c1854ecdc41f10925f", "_cell_guid": "be0e1ce5-8c0b-4e52-be37-37133f845ac4"}}, {"execution_count": null, "cell_type": "code", "outputs": [], "source": ["y_test = y_test.T"], "metadata": {"collapsed": true, "_uuid": "3c1a21ef5bb889bf56184db6a2b30645196e6289", "_cell_guid": "1d3230fd-439c-449d-b574-dc62eaa3ca1a"}}, {"execution_count": null, "cell_type": "code", "outputs": [], "source": ["x_train = x_train/255\n", "x_test = x_test/255"], "metadata": {"collapsed": true, "_uuid": "35a07d3fe3482698a07b18f4504f5c1a7309ee57", "_cell_guid": "db28eb28-9145-4b58-86af-da0a0b80792f"}}, {"execution_count": null, "cell_type": "code", "outputs": [], "source": ["def sigmoid(x):\n", "    sig = 1/(1 + np.exp(-x))\n", "    return sig "], "metadata": {"collapsed": true, "_uuid": "26bef2ea3b056f68a4b23a610a82a474b86be3dc", "_cell_guid": "b57dcfc8-db20-47f7-a78d-7fcc8cf77335"}}, {"execution_count": null, "cell_type": "code", "outputs": [], "source": ["def initialize_with_zeroes(dim):\n", "    w = np.zeros((dim,1),dtype=np.float)\n", "    b = np.zeros((1,1), dtype=np.float)\n", "    return w,b"], "metadata": {"collapsed": true, "_uuid": "961aa6dd6a881a48dc0eef33951849b6dd0fa502", "_cell_guid": "d3942710-a706-48a1-b9ba-ec203d8fffec"}}, {"execution_count": null, "cell_type": "code", "outputs": [], "source": ["def propagate(w,b, x, y):\n", "    m = x.shape[1]\n", "    A = sigmoid(np.dot(w.T,x) + b)\n", "    cost =np.sum(np.dot(y,np.log(A).T)+ np.dot((1-y),np.log(1-A).T))/(-m)\n", "    dw = (np.dot(x,(A-y).T))/(m)\n", "    db = (np.sum(A-y))/(m)\n", "    cost = np.squeeze(cost)\n", "    grads = {\"dw\": dw,\n", "             \"db\": db}\n", "    return grads, cost\n", "    "], "metadata": {"collapsed": true, "_uuid": "e521f01e5f71bc8295343a114b239b663c5c71ba", "_cell_guid": "b38b5ac5-7247-4393-b388-c23cc0873c0f"}}, {"execution_count": null, "cell_type": "code", "outputs": [], "source": ["def optimize(w, b, x, y, num_iterations, learning_rate, print_cost = False):\n", "    costs = []\n", "    for i in range(num_iterations):\n", "        grads, cost = propagate(w, b, x,y)\n", "        dw = grads[\"dw\"]\n", "        db = grads[\"db\"]\n", "        w = w-np.dot(learning_rate,dw)\n", "        b = b-np.dot(learning_rate,db)\n", "        if i % 100 == 0:\n", "            costs.append(cost)\n", "        if print_cost and i % 100 == 0:\n", "            print (\"Cost after iteration %i: %f\" %(i, cost))\n", "        params = {\"w\": w,\n", "                  \"b\": b}\n", "        grads = {\"dw\": dw,\n", "                 \"db\": db}\n", "        return params, grads, costs"], "metadata": {"collapsed": true, "_uuid": "dbd15b5825ddeb826b9585412816aaa70c815e1b", "_cell_guid": "582e2b53-3f09-405c-9668-cf5b26e0324f"}}, {"execution_count": null, "cell_type": "code", "outputs": [], "source": ["def predict(w, b, x):\n", "    m = x.shape[1]\n", "    Y_prediction = np.zeros((1,m))\n", "    w = w.reshape(x.shape[0], 1)\n", "    A = sigmoid(np.dot(w.T,x)+b)\n", "    for i in range(A.shape[1]):\n", "        if A[0,i]<=0.5:\n", "            Y_prediction[0][i]=0\n", "        elif A[0,i]>0.5:\n", "            Y_prediction[0][i]=1\n", "    return Y_prediction"], "metadata": {"collapsed": true, "_uuid": "a47fe9e1eae08175f3d131e63e761f60009cd0c2", "_cell_guid": "676889dd-c597-44a0-8c15-d07424ce3a61"}}, {"execution_count": null, "cell_type": "code", "outputs": [], "source": ["def model(X_train, Y_train, X_test, Y_test, num_iterations = 5000, learning_rate = 0.5, print_cost = False):\n", "    w, b = initialize_with_zeroes(X_train.shape[0])\n", "    parameters, grads, costs = optimize(w, b, X_train, Y_train , num_iterations, learning_rate, print_cost = False)\n", "    # Retrieve parameters w and b from dictionary \"parameters\"\n", "    w = parameters[\"w\"]\n", "    b = parameters[\"b\"]\n", "    # Predict test/train set examples (\u2248 2 lines of code)\n", "    Y_prediction_test =predict(w,b,X_test)\n", "    Y_prediction_train =predict(w, b, X_train) \n", "\n", "    ### END CODE HERE ###\n", "\n", "    # Print train/test Errors\n", "    print(\"train accuracy: {} %\".format(100 - np.mean(np.abs(Y_prediction_train - Y_train)) * 100))\n", "    print(\"test accuracy: {} %\".format(100 - np.mean(np.abs(Y_prediction_test - Y_test)) * 100))\n", "\n", "    \n", "    d = {\"costs\": costs,\n", "         \"Y_prediction_test\": Y_prediction_test, \n", "         \"Y_prediction_train\" : Y_prediction_train, \n", "         \"w\" : w, \n", "         \"b\" : b,\n", "         \"learning_rate\" : learning_rate,\n", "         \"num_iterations\": num_iterations}\n", "    \n", "    return d"], "metadata": {"collapsed": true, "_uuid": "dff1d78015964258257ce893d745acfa2ea6da22", "_cell_guid": "a285af32-8494-414b-b62d-0977e4fdf9d2"}}, {"execution_count": null, "cell_type": "code", "outputs": [], "source": ["d = model(x_train, y_train, x_test, y_test , num_iterations = 1000, learning_rate = 0.005, print_cost = True)"], "metadata": {"_uuid": "9d6eb4539d124886c8198f5871fef0603fb408ba", "_cell_guid": "5993c334-e597-4261-bc94-f6cd195a594a"}}, {"execution_count": null, "cell_type": "code", "outputs": [], "source": [], "metadata": {"collapsed": true, "_uuid": "3ea21d649a96442c4f51810548d06f3f7f4f9633", "_cell_guid": "5f34d98d-2199-4d78-ae96-3397a69b8eb7"}}], "metadata": {"kernelspec": {"language": "python", "name": "python3", "display_name": "Python 3"}, "language_info": {"pygments_lexer": "ipython3", "mimetype": "text/x-python", "file_extension": ".py", "version": "3.6.1", "codemirror_mode": {"version": 3, "name": "ipython"}, "name": "python", "nbconvert_exporter": "python"}}}