{"cells":[{"metadata":{"_uuid":"a8b0dab2f335f919b85a4ed53e942681d165451a"},"cell_type":"markdown","source":"In this notebook I used the following refrences :\n\n1. https://www.kaggle.com/yassineghouzam/introduction-to-cnn-keras-0-997-top-6\n2. https://machinelearningmastery.com/handwritten-digit-recognition-using-convolutional-neural-networks-python-keras/\n","outputs":[],"execution_count":null},{"metadata":{"_cell_guid":"c67a1d5f-51ba-40e1-b938-5e9ae23b61df","_uuid":"a856bcff44ec86d82e8fb42df971f1509d222304","trusted":true},"cell_type":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nfrom keras.models import Sequential\nfrom keras.layers import Dense\nfrom keras.layers import Dropout\nfrom keras.layers import Flatten\nfrom keras.layers.convolutional import Conv2D\nfrom keras.layers.convolutional import MaxPooling2D\nfrom keras.utils import np_utils\nfrom keras.preprocessing.image import ImageDataGenerator\n\nfrom keras import backend as K\nK.set_image_dim_ordering('th')\n\nfrom collections import Counter\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nfrom sklearn.utils import shuffle\nfrom sklearn.model_selection import train_test_split\n%matplotlib inline","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"5291e1dd5c4b2eb17c476f91a4eaab68a2342c39"},"cell_type":"markdown","source":"1) Load the data:","outputs":[],"execution_count":null},{"metadata":{"_cell_guid":"4724d059-3fc4-461d-8700-85833ee66fb5","_uuid":"2228a5b0474b5f39fa1a948ce8a45de7599c6a98","collapsed":true,"trusted":true},"cell_type":"code","source":"# Load the data\ntrain = pd.read_csv(\"../input/train.csv\")\ntest = pd.read_csv(\"../input/test.csv\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7d4d43d6ebcfc214b3a8085d852d9a17bdfc8dfe"},"cell_type":"markdown","source":"2)  Divide the data to labels and features, split the training data to training and valiation data and explore it:","outputs":[],"execution_count":null},{"metadata":{"_cell_guid":"87ef490e-a5da-406d-a5ec-f4fcbf77b6d4","_uuid":"afb7996638918c11d1b5e74c90275bf119c65a1e","trusted":true},"cell_type":"code","source":"y_train = train[\"label\"]\nX_train = train.drop(labels = [\"label\"],axis = 1)\n\n# fix random seed for reproducibility\nseed = 7\nnp.random.seed(seed)\n\n#split the data to train and validation data\nX_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size = 0.2, random_state=seed)\n\nn_train = len(X_train)\nn_train_y = len(y_train)\nn_test = len(test)\n\ncounter_train = Counter(y_train)\ncounter_valid = Counter(y_val)\n\nn_classes = len(counter_train.keys())\n\nprint(\"Number of training examples =\", n_train)\nprint(\"Number of labels in training examples =\", n_train_y)\nprint(\"Number of testing examples =\", n_test)\nprint(\"Number of classes =\", n_classes)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"4ff1be5c-09c3-471f-b97e-29d5cdfa7681","_uuid":"23bc75d89ece713dbece603989db96a9b9076d42","trusted":true},"cell_type":"code","source":"X_train.isnull().any().describe()","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"30159d3d-3c78-42c8-ab7c-e3aaea1cc55c","_uuid":"62269306512170999cbf2dca5c1c18fc52b368c6","trusted":true},"cell_type":"code","source":"test.isnull().any().describe()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"3797ed63b1bed10dd1b58c7ed4a67b1cc9c565b1"},"cell_type":"markdown","source":"3) Visualize the frequency of the digits in the training data","outputs":[],"execution_count":null},{"metadata":{"_cell_guid":"c4a8cafd-82cb-49aa-8ca5-09f6957f9674","_uuid":"b5465566e2b3bb7b8eb809e74d19b05cc8aed001","trusted":true},"cell_type":"code","source":"frequency = sns.countplot(y_train)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"a2d58e70dc2cab9c46d21a4ff5b96dcc3806d97e"},"cell_type":"markdown","source":"4) Reshape the vecotres to fit into the model","outputs":[],"execution_count":null},{"metadata":{"_cell_guid":"87b9f7dd-5291-485a-ab64-a80a4a68189e","_uuid":"a185bfae8a677737b737963cfade75416d577788","collapsed":true,"trusted":true},"cell_type":"code","source":"X_train = X_train.values.reshape(-1,1,28,28).astype('float32')\nX_val = X_val.values.reshape(-1,1,28,28).astype('float32')\ntest = test.values.reshape(-1,1,28,28).astype('float32')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"8309989cd170d653da0bf817b2c59e03de19e077"},"cell_type":"markdown","source":"4) Shaffle the data to avoid bais based on the order of the data and visualise the digits images: ","outputs":[],"execution_count":null},{"metadata":{"_cell_guid":"fbc86f3f-4716-48a8-a8c4-c575e030932a","_uuid":"09c01c5f46771bc18c698b710a1431016b65a500","trusted":true},"cell_type":"code","source":"### Data exploration visualization\nX_train, y_train = shuffle(X_train, y_train)\n\nfig = plt.figure()\nfig.suptitle('Example images of the German Traffic Signs', fontsize=18)\n\nfor i in range(50):\n    image = X_train[i].squeeze()\n    plt.axis(\"off\")\n    plt.subplot(5,10,i+1)\n    plt.imshow(image,cmap='gray')","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"971cc67da3ebb00e9c768fdede7235bb03020eda"},"cell_type":"markdown","source":"5) Preprocess the data : normalize it and one hot encode the lables:","outputs":[],"execution_count":null},{"metadata":{"_cell_guid":"69742fe0-f6da-45e9-8103-ea5a084a9af4","_uuid":"c46ceb96f42acc25496d62b4395ac577765b7e17","collapsed":true,"trusted":true},"cell_type":"code","source":"# normalize inputs from 0-255 to 0-1\nX_train = X_train / 255\nX_val = X_val / 255\ntest = test / 255\n# one hot encode outputs\ny_train = np_utils.to_categorical(y_train)\ny_val = np_utils.to_categorical(y_val)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"41d23e020a91823f6c9d5ab26ca5cec91cb82b9c"},"cell_type":"markdown","source":"6) Define the CNN model with keras:","outputs":[],"execution_count":null},{"metadata":{"_cell_guid":"6588b904-3d17-46fc-a19b-d722123c0ba8","_uuid":"571c642b7c53eb6895b15e30a4b349f0293e0128","collapsed":true,"trusted":true},"cell_type":"code","source":"# define the model\ndef model():\n# create model\n    model = Sequential()\n    model.add(Conv2D(30, (5, 5), input_shape=(1, 28, 28), activation='relu'))\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n    model.add(Conv2D(15, (3, 3), activation='relu'))\n    model.add(MaxPooling2D(pool_size=(2, 2)))\n    model.add(Dropout(0.2))\n    model.add(Flatten())\n    model.add(Dense(128, activation='relu'))\n    model.add(Dense(50, activation='relu'))\n    model.add(Dense(n_classes, activation='softmax'))\n    # Compile model\n    model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\n    return model","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"274917f23ca0ed15dc8b9c6978c27932e9447095"},"cell_type":"markdown","source":"7) Fit the data in the model and evaluate the accuracy using the validation data: ","outputs":[],"execution_count":null},{"metadata":{"_cell_guid":"bf2e64d1-92bf-4d78-9008-e3ddbe550993","_uuid":"5c8ed7faea6068443ef1c9bd22c24d17bdcd9d1e","trusted":true},"cell_type":"code","source":"# build the model\nmodel = model()\n# Fit the model\n\nepochs=38\nbatch_size=200\n\n# Fit the model\nmodel.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=epochs, batch_size=batch_size, verbose=0)\n\nscores = model.evaluate(X_val, y_val, verbose=0)\nprint(\"CNN Error: %.2f%%\" % (100-scores[1]*100))","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"81ec92e0edb4fca95bac163bc68330ebe4e97ba5"},"cell_type":"markdown","source":"8) Predict the results of the test data:","outputs":[],"execution_count":null},{"metadata":{"_cell_guid":"ae806eb3-b50b-4cb6-b0c7-5f57f0c1da80","_uuid":"c354746638942e5dd73c87365ce4a75ba9d440fc","collapsed":true,"trusted":true},"cell_type":"code","source":"# predict results\nresults = model.predict(test)\n# select the indix with the maximum probability\nresults = np.argmax(results,axis = 1)\nresults = pd.Series(results,name=\"Label\")","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"f87343202aec3d7bdbc57cab37df93788331d95a"},"cell_type":"markdown","source":"9) Create a submission file:","outputs":[],"execution_count":null},{"metadata":{"_cell_guid":"5dc38e6e-e0f6-4435-9d86-997e36a9ac4d","_uuid":"dce0050cefc4a56158ba42e527fd1b73430dbe61","collapsed":true,"trusted":true},"cell_type":"code","source":"submission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),results],axis = 1)\nsubmission.to_csv(\"digits.csv\",index=False)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}