{"metadata": {"kernelspec": {"language": "python", "display_name": "Python 3", "name": "python3"}, "language_info": {"version": "3.6.3", "pygments_lexer": "ipython3", "codemirror_mode": {"version": 3, "name": "ipython"}, "mimetype": "text/x-python", "name": "python", "file_extension": ".py", "nbconvert_exporter": "python"}}, "nbformat_minor": 1, "nbformat": 4, "cells": [{"metadata": {"_uuid": "36565d0917380e5e8accce661e1bce6c6ef712fd", "_cell_guid": "028ebc2b-135d-4b71-a0f6-de1c6c9eed57"}, "outputs": [], "execution_count": null, "cell_type": "code", "source": ["# Importing general packages and set basic plot styling\n", "%matplotlib inline\n", "# pandas for data manipulation\n", "import pandas as pd     \n", "\n", "# numpy for linear algebra operations\n", "import numpy as np      \n", "\n", "# scipy for statistical tools\n", "import scipy as sp      \n", "\n", "# matplotlib for plotting and visualization\n", "import matplotlib.pyplot as plt     \n", "\n", "# sklearn for splitting data into training and testing for cross validation\n", "from sklearn.model_selection import train_test_split\n", "\n", "# seaborn for data visualization\n", "import seaborn as sns               \n", "from IPython.display import display\n", "import warnings\n", "warnings.filterwarnings('ignore')\n", "\n", "# keras for machine learning tools (convolutional networks)\n", "from keras.models import Sequential\n", "from keras.layers import Dense\n", "from keras.layers import Dropout\n", "from keras.layers import Flatten\n", "from keras.layers.convolutional import Conv2D\n", "from keras.layers.convolutional import MaxPooling2D\n", "from keras.utils import np_utils\n", "from keras import backend as K\n", "K.set_image_dim_ordering('tf')\n", "\n", "# fix random seed for reproducibility\n", "np.random.seed(7)\n", "\n", "# Import for image manipulation\n", "from scipy import ndimage\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\"))"]}, {"metadata": {"_uuid": "a7241a7828945483cb969aac13811a37913cb8c0", "_cell_guid": "fd42a49f-4cb0-4dde-a4a3-e395892df9f1"}, "outputs": [], "execution_count": null, "cell_type": "code", "source": ["# Load the training and testing data (already completed)\n", "train_data = pd.read_csv('../input/train.csv')\n", "test_data = pd.read_csv('../input/test.csv')\n", "\n", "# Print data information (already completed)\n", "train_data.info()\n", "train_data.head()"]}, {"metadata": {"_uuid": "2364c9d1ab831053af88eca7b05133f2e8543855", "_cell_guid": "372810a0-b0f5-4e2d-aa16-c35442e0e900"}, "outputs": [], "execution_count": null, "cell_type": "code", "source": ["# Split given training data into new training and testing sets for cross validation\n", "# Specify fraction of data to use for testing\n", "testsize = .2\n", "\n", "# Create new data sets for cross validation (already completed)\n", "cross_train_data, cross_test_data = train_test_split(train_data, test_size=testsize)"]}, {"metadata": {"_uuid": "c7d755dedf068610a24f9e881ee6472065304633", "_cell_guid": "bcd9e4bd-cf09-4b74-a3de-6621f36f4256", "scrolled": true}, "outputs": [], "execution_count": null, "cell_type": "code", "source": ["# ----- Create training and testing data matrices ----- \n", "\n", "# To create the train input matrix we need to drop the 'label' feature from the data\n", "X_train = cross_train_data.drop(['label'], axis=1).values # add the command to remove the 'label' data and convert to numpy array\n", "\n", "# To create the train target matrix we only want to keep the 'label' feature from the data\n", "Y_train = cross_train_data[['label']].values # add the command to keep only the 'label' data and convert to numpy array\n", "\n", "# Repeat the above procedure on \"cross_test_data\" to create the test input and target array\n", "X_test = \n", "Y_test = \n", "\n", "# Print matrices (optional)\n", "print(\"X_train =\", X_train)\n", "print(\"Y_train =\", Y_train)"]}, {"metadata": {"_uuid": "5475866f0922fa2425195aaaee45404e6f5ca9bd", "_cell_guid": "e5b6c1f4-30c0-46c5-bf38-550bf9cb1fbd", "scrolled": true}, "outputs": [], "execution_count": null, "cell_type": "code", "source": ["# ----- Resize X's and Y's so they will be in correct format for convolutional neural network (already completed)\n", "\n", "# Print original shape of X_train and X_test\n", "print(\"Original shape of X_train:\", np.shape(X_train))\n", "print(\"Original shape of X_test:\", np.shape(X_test))\n", "\n", "# Reshape each vector in the input data into a 28 x 28 matrix\n", "X_train = np.reshape(X_train, (X_train.shape[0], 28, 28, 1))\n", "X_test = np.reshape(X_test, (X_test.shape[0], 28, 28, 1))\n", "\n", "# Print new shape of X_train and X_test\n", "print(\"New shape of X_train:\", np.shape(X_train))\n", "print(\"New shape of X_test:\", np.shape(X_test))"]}, {"metadata": {"_uuid": "6ebd1beac2a992bd9f5db20242789b5b201df6fd", "_cell_guid": "500a994c-7204-48df-8295-55d48805b144"}, "outputs": [], "execution_count": null, "cell_type": "code", "source": ["# ----- Create a plot of the first four digits from MNIST data set (optional) -----\n", "# Configure plotting region (already completed)\n", "f, axarr = plt.subplots(2, 2, figsize=(8,8))\n", "\n", "for i in range(2):\n", "    for j in range(2):\n", "        # Reshape digit into correct format for plotting\n", "        digit = # set digit equal to 28x28 numpy array corresponding to the (i+j)th entry of X_train\n", "        \n", "        # Plot (i+j)th digit (already completed)\n", "        axarr[i,j].imshow(digit, cmap='gray')"]}, {"metadata": {"_uuid": "5247204848dfd1617c5a084fe2907dcfe3670c81", "_cell_guid": "c168856f-58fd-4fe0-9174-3ecaa8cae3ea", "collapsed": true}, "outputs": [], "execution_count": null, "cell_type": "code", "source": ["# ----- Normalize input data in matrices for training and testing sets -----\n", "X_train = X_train / 255 # enter the code to normalize the matrix X_train\n", "X_test = X_test / 25# enter the code to normalize the matrix X_test"]}, {"metadata": {"_uuid": "fec652068509bc1872f7243658694efe333f5b42", "_cell_guid": "8d882501-51df-4cca-a5e9-5ab6f2138be7"}, "outputs": [], "execution_count": null, "cell_type": "code", "source": ["# ----- Encode the outputs using one hot encoding (already completed) -----\n", "# Encode Y_train\n", "Y_train_1hot = np_utils.to_categorical(Y_train)\n", "\n", "# Encode Y_test\n", "Y_test_1hot = np_utils.to_categorical(Y_test)\n", "\n", "# Print Y_train_1hot and Y_test_1hot to view one-hot encoding vectors\n", "print(\"One-hot encoding of Y_train:\\n\", Y_train_1hot, \"\\n\")\n", "print(\"One-hot encoding of Y_test:\\n\", Y_test_1hot, \"\\n\")\n", "\n", "# Store the number of classes\n", "num_classes = Y_train_1hot.shape[1]\n", "\n", "# Print the number of classes\n", "print(\"The total number of classes :\", num_classes)"]}, {"metadata": {"_uuid": "03bf9b4641f3d513029efa9c2c1ba8e481c28b0f", "_cell_guid": "0212ddd6-c765-4e0e-8689-04653d0cea39", "collapsed": true}, "outputs": [], "execution_count": null, "cell_type": "code", "source": ["# Define python function which executes a convolutional neural network model\n", "def basic_cnn_model():\n", "    # Define sequential model using keras (already completed)\n", "    model = Sequential()\n", "    \n", "    # ----- Add first layer of network (2D Convolutional layer) -----\n", "    # Number of filters (dimension of output)          :  \n", "    # Kernel Size (dimension of 2D convolution window) :  \n", "    # Input Shape (shape of input)                     :  (1, 28, 28)  # (already completed)\n", "    # Activation layer                                 :  ReLU (Rectified Linear Unit)  # (already completed)\n", "    model.add(Conv2D(#num_filters, #kernel_size, input_shape=(28, 28, 1), activation='relu'))\n", "    \n", "    # ----- Add second layer of network (2D Max Pooling layer) -----\n", "    # Pool Size (dimension of 2D pooling window) :  # Enter here and below \n", "    model.add(MaxPooling2D(pool_size= #enter pool size here))\n", "    \n", "    # ----- Add third layer of network (Dropout layer) -----\n", "    # Dropout Rate (fraction of the input units to drop) :  # Enter a number between 0 and 0.5\n", "    model.add(Dropout(#dropout_rate))\n", "    \n", "    # ----- Add fourth layer of network (Flatten layer) -----\n", "    # This combines all information from previous output into a single vector (already completed)\n", "    model.add(Flatten())\n", "    \n", "    # ----- Add fifth layer of network (Hidden layer) -----\n", "    # Number of neurons :  # Enter number of neurons here and below\n", "    # Number of inputs  :  (this is automatically determined since the model type is 'Sequential')\n", "    # Activation layer  :  ReLU (Rectified Linear Unit)  (already completed)\n", "    model.add(Dense(#num_neurons, activation='relu'))\n", "    \n", "    # ----- Add final layer of network (Output layer) -----\n", "    # Number of outputs :  10  (already completed)\n", "    # Number of inputs  :  (this is automatically determined since the model type is 'Sequential')\n", "    # Activation layer  :  Softmax  (already completed)\n", "    model.add(Dense(num_classes, activation='softmax'))\n", "    \n", "    # ----- Compile the model -----\n", "    # Loss function              :  Categorical Crossentropy  (already completed)\n", "    # Gradient Descent algorithm :  adam (More on adam here: https://arxiv.org/abs/1412.6980)\n", "    # Classification metric      :  accuracy  (already completed)\n", "    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\n", "    \n", "    # Return model\n", "    return model"]}, {"metadata": {"_uuid": "2ff4fa4390640d82daed7c2128a73d3b2d9bfa68", "_cell_guid": "ab8233ea-dc0f-496a-bb52-a214e12a305a"}, "outputs": [], "execution_count": null, "cell_type": "code", "source": ["# Build the model (already completed)\n", "cnn1 = basic_cnn_model()\n", "\n", "# Fit the model\n", "epch = # Specify number of epochs for training (stick to less than 10 for now so it doesn't take forever)\n", "batch_sz = # Specify batch size for training (I'll leave this up to you to decide)\n", "cnn1.fit(X_train, Y_train_1hot, epochs=epch, batch_size=batch_sz, verbose=2)\n"]}, {"metadata": {"_uuid": "b3af81741a80decfe62162d8dee4ba5a27765f6e", "_cell_guid": "425b69c1-dbc9-46a4-ae11-885e8e43ef5c", "collapsed": true}, "outputs": [], "execution_count": null, "cell_type": "code", "source": ["# Evaluate model (already completed)\n", "cnn1_scores = cnn1.evaluate(X_test, Y_test_1hot, verbose=0)"]}, {"metadata": {"_uuid": "7b437227aa05b2406bcf3f0ab68fa7b4d87e350a", "_cell_guid": "2ecb5606-2372-4d2b-8739-4e821344595e"}, "outputs": [], "execution_count": null, "cell_type": "code", "source": ["# Print score (already completed)\n", "print(\"Percentage of correctly classified digits in testing set:\", 100*cnn1_scores[1])"]}, {"metadata": {"_uuid": "67023eac83e13b5fe9f0da1d5e25e90a3e66ead1", "_cell_guid": "a695372c-da76-417d-aea3-bae374dff7a4", "collapsed": true}, "outputs": [], "execution_count": null, "cell_type": "code", "source": []}]}