{"cells":[{"metadata":{"_cell_guid":"654456b6-e648-0379-0d66-1cc97af6d00d","_uuid":"6b48ce0e361bdb67689dd2f254ecedd9ade1f5ff"},"cell_type":"markdown","source":"**Import all required libraries**\n==============================="},{"metadata":{"_cell_guid":"e5b02688-c589-5a89-e11c-837c6a99eb6e","_uuid":"f043e48097bfd98e41710142dd8aac41fa88a801","trusted":true,"collapsed":true},"cell_type":"code","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\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nfrom keras.models import Sequential\nfrom keras.layers import Dense , Dropout , Lambda, Flatten\nfrom keras.optimizers import Adam ,RMSprop\nfrom sklearn.model_selection import train_test_split\nfrom keras import  backend as K\nfrom keras.preprocessing.image import ImageDataGenerator\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\nfrom subprocess import check_output\nprint(check_output([\"ls\", \"../input\"]).decode(\"utf8\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"22a7fd70-ab61-432d-24cb-93e558414495","_uuid":"62fbd0fe9c338b7ac0b04e688c8ee7947e6170f7"},"cell_type":"markdown","source":"**Load Train and Test data**\n============================"},{"metadata":{"_cell_guid":"05226b08-226a-1a00-044d-a0e6b2101388","_uuid":"4eff577bcd43479a3b7e91180393cbad9fcfca33","trusted":true,"collapsed":true},"cell_type":"code","source":"# create the training & test sets, skipping the header row with [1:]\ntrain = pd.read_csv(\"../input/train.csv\")\nprint(train.shape)\ntrain.head()","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"2ec570a6-b41a-2139-5e0e-4941c4f0a9d0","_uuid":"67f0854ad0d812a1395130144a0adef9966fec88","trusted":true,"collapsed":true},"cell_type":"code","source":"test= pd.read_csv(\"../input/test.csv\")\nprint(test.shape)\ntest.head()","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"1ae10fe0-dde9-7659-f53d-1a1bd625cfb1","_uuid":"bdffbed77ce62da528c60e43f2b1bea9f57fcdbc","trusted":true,"collapsed":true},"cell_type":"code","source":"\n\n\n\nX_train = (train.iloc[:,1:].values).astype('float32') # all pixel values\ny_train = train.iloc[:,0].values.astype('int32') # only labels i.e targets digits\nX_test = test.values.astype('float32')\n\n\nX = X_train\ny = y_train\nX_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.10, random_state=42)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"250b1126-ce1d-6d3f-9736-2504f7a1e098","_uuid":"5e3e1e3574c3e019eadfd14e4dda41fd15b4de2a","trusted":true,"collapsed":true},"cell_type":"code","source":"y.shape","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"e0f15f8a-ac08-540a-58db-dab989cc687c","_uuid":"4c96cf1c9cdc364ae3faff6b8c3c97aa7fa982d4","trusted":true,"collapsed":true},"cell_type":"code","source":"\\\n\n\nmean_px = X_train.mean().astype(np.float32)\nstd_px = X_train.std().astype(np.float32)\n\ndef standardize(x): \n    return (x-mean_px)/std_px","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"1541678d-a08b-d2b2-1e1e-eabf882baaec","_uuid":"7998af1ce3c065c4a54a73cd97fed9afebde7e96","trusted":true,"collapsed":true},"cell_type":"code","source":"X_train = X_train.reshape(-1,28,28,1)\nX_test = X_test.reshape(-1, 28, 28,1)\nX_val = X_val.reshape(-1,28,28,1)\nX_test.shape","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"6be2f3e9-42eb-85b6-9162-c25e4d706155","_uuid":"4051a0e6612b8e6d4b8aef6a4d131be621cd3a14","trusted":true,"collapsed":true},"cell_type":"code","source":"y_train[2]","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"6949468c-fd27-19c5-15c7-0b357a961003","_uuid":"6d4c1323f1fa3f89a16532fed893ec5d72051bcb","trusted":true,"collapsed":true},"cell_type":"code","source":"from keras.utils import np_utils\ny_train = np_utils.to_categorical(y_train,10)\nX = X.reshape(-1,28,28,1)\ny = np_utils.to_categorical(y,10)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"6a89dcdd-7b68-6ed1-2c39-b3a1edb3e7be","_uuid":"dc7ece2b7ee08767b664149d67d922d8c1d0bbb1"},"cell_type":"markdown","source":"**Designing Neural Network Architecture**\n========================================="},{"metadata":{"_cell_guid":"39107235-d87a-af4d-44fb-80c9c3aa0212","_uuid":"1070353d05490ccec23933c62f11cdfd2d7e5032","trusted":true,"collapsed":true},"cell_type":"code","source":"# fix random seed for reproducibility\nseed = 43\nnp.random.seed(seed)","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"5dbe450c-845f-aaa2-dbde-21414a91d8c1","_uuid":"5f54b59d89cd4e43dd129d9b133950ba83b5cad8","trusted":true,"collapsed":true},"cell_type":"code","source":"from keras.models import  Sequential\nfrom keras.layers.core import  Lambda , Dense, Flatten, Dropout\nfrom keras.callbacks import EarlyStopping\nfrom keras.layers import BatchNormalization, Convolution2D , MaxPooling2D","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"46b81b17854a98f2b380da694691502c1e583bfb"},"cell_type":"markdown","source":"## Convolutional Neural Network\nCNNs are extremely efficient for images.\n"},{"metadata":{"trusted":true,"collapsed":true,"_uuid":"8b72580fbb06f5f4f769c514cb0d7d2f15aa2c2f"},"cell_type":"code","source":"from keras.layers.normalization import BatchNormalization\n\ndef get_bn_model():\n    model = Sequential([\n        Lambda(standardize, input_shape=(28,28,1)),\n        Convolution2D(32,(3,3), activation='relu'),\n        BatchNormalization(axis=1),\n        Convolution2D(32,(3,3), activation='relu'),\n        MaxPooling2D(),\n        BatchNormalization(axis=1),\n        Convolution2D(64,(3,3), activation='relu'),\n        BatchNormalization(axis=1),\n        Convolution2D(64,(3,3), activation='relu'),\n        MaxPooling2D(),\n        Flatten(),\n        BatchNormalization(),\n        Dense(512, activation='relu'),\n        BatchNormalization(),\n        Dense(10, activation='softmax')\n        ])\n    model.compile(Adam(), loss='categorical_crossentropy', metrics=['accuracy'])\n    return model","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"78e382d0b3de14312e762edc480b5d215be82269","collapsed":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"0fc055b482971b36561aaf9421c8a9c53df2900b","collapsed":true},"cell_type":"code","source":"model= get_bn_model()\ngen =ImageDataGenerator(rotation_range=8, width_shift_range=0.08, shear_range=0.3,\n                               height_shift_range=0.08, zoom_range=0.08)\nbatches = gen.flow(X, y, batch_size=64)\nhistory=model.fit_generator(generator=batches, steps_per_epoch=batches.n, epochs=1)\n\n","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"c2841d54-f3dd-1ee8-a30d-4457dec0a67a","_uuid":"4262c6bfb15ec96993e83bd2a2552eadf14fb33d","trusted":true,"collapsed":true},"cell_type":"code","source":"X_test.shape\n# predict results\nresults = model.predict(X_test)\n\n# select the indix with the maximum probability\nresults = np.argmax(results,axis = 1)\n\nresults = pd.Series(results,name=\"Label\")\nsubmission = pd.concat([pd.Series(range(1,28001),name = \"ImageId\"),results],axis = 1)\n\nsubmission.to_csv(\"Aniket_Digit_pred2.csv\",index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true},"cell_type":"code","source":"X_test.shape","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"collapsed":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"_change_revision":0,"_is_fork":false,"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":4}