{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":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)\nimport seaborn as sns\nimport matplotlib.pyplot as plt\n# import warnings\nimport warnings\n# filter warnings\nwarnings.filterwarnings('ignore')\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\nimport os\nprint(os.listdir(\"../input\"))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","collapsed":true,"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":false},"cell_type":"markdown","source":"# Load the data\n"},{"metadata":{"trusted":true,"_uuid":"8512c7b293c3d340a0496700401533897d6021f2"},"cell_type":"code","source":"#read the train set\ntrain = pd.read_csv('../input/train.csv')\nprint(train.shape)\ntrain.head()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a86eca8216b25a60e0303e14c358e30cc3c5ce92"},"cell_type":"code","source":"train.info()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d16d7e992b704ffbf5857e2b5f12869f62a4c5ed"},"cell_type":"code","source":"train.describe()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"d364774ce513827b996d5a803105e311a3915765"},"cell_type":"code","source":"# read the test set\ntest= pd.read_csv(\"../input/test.csv\")\nprint(test.shape)\ntest.head()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"81aba4992f9b8847d52176eb450794df7a049b3d"},"cell_type":"markdown","source":"# Splitting\n\n"},{"metadata":{"trusted":true,"_uuid":"c1a1749f769b9f4a29baa39564e2fa36ff58661d"},"cell_type":"code","source":"Y_train=train[\"label\"]\nX_train=train.drop([\"label\"],axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"467258a90b65c9a61a4a9b951e2d4914fb26540a"},"cell_type":"code","source":"# visualize number of digits classes\nplt.figure(figsize=(15,7))\ng = sns.countplot(Y_train, palette=\"icefire\")\nplt.title(\"Number of digit classes\")\nY_train.value_counts()","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"7ede68b442ccb8df68d75c16107a489a217a9659"},"cell_type":"markdown","source":"# Normalization"},{"metadata":{"trusted":true,"_uuid":"c5f32b580325252ed44d6b9c85127205f479b19a"},"cell_type":"code","source":"X_train=X_train/255.0\ntest=test/255.0\nprint(\"X_train shape :\" ,X_train.shape)\nprint(\"Test shape :\",test.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9927901cdf5f42a57cdb6c821071eccecb8e690b"},"cell_type":"code","source":"print(type(X_train))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"c76e247952a313a809b819ac1e1a4d7785738fac"},"cell_type":"code","source":"#reshape \nX_train=X_train.values.reshape(-1,28,28,1)\ntest=test.values.reshape(-1,28,28,1)\nprint(\"x_train shape: \",X_train.shape)\nprint(\"test shape: \",test.shape)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"9a1c5e0a80b9680aa4c5b37c3c2eb9536e5f70b3"},"cell_type":"code","source":"#label encoding\nfrom keras.utils.np_utils import to_categorical # convert to one-hot-encoding\nY_train=to_categorical(Y_train,num_classes=10)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"195edf2465346ae789aa808be91fd4771bba6c71"},"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train,X_val,Y_train,Y_val=train_test_split(X_train,Y_train,test_size=0.2,random_state=2)\nprint(\"x_train shape\",X_train.shape)\nprint(\"x_test shape\",X_val.shape)\nprint(\"y_train shape\",Y_train.shape)\nprint(\"y_test shape\",Y_val.shape)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"25d1b542902719293e48340408275f5cbbde53c6"},"cell_type":"markdown","source":"## Implementing with Keras\n"},{"metadata":{"trusted":true,"_uuid":"780a8fbb60e41b43001d2b4b44f82ce544d335e8"},"cell_type":"code","source":"# \nfrom sklearn.metrics import confusion_matrix\nimport itertools\n\nfrom keras.utils.np_utils import to_categorical # convert to one-hot-encoding\nfrom keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D\nfrom keras.optimizers import RMSprop,Adam\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ReduceLROnPlateau\n\nmodel = Sequential()\n\nmodel.add(Conv2D(64, kernel_size = (5,5),padding = 'same',activation ='relu', input_shape = (28,28,1)))\nmodel.add(Conv2D(32,kernel_size=(3,3),padding=\"same\",activation=\"relu\"))\nmodel.add(MaxPool2D(pool_size=(2,2),padding=\"same\"))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(32,kernel_size=(5,5),padding=\"same\",activation=\"relu\"))\nmodel.add(MaxPool2D(pool_size=(2,2),padding=\"same\"))\nmodel.add(Dropout(0.25))\n\nmodel.add(Conv2D(16,kernel_size=(2,2),padding=\"same\",activation=\"relu\"))\nmodel.add(MaxPool2D(pool_size=(2,2),padding=\"same\"))\nmodel.add(Dropout(0.25))\n\nmodel.add(Flatten())\n\nmodel.add(Dense(128,activation = \"relu\"))\nmodel.add(Dropout(0.5))\nmodel.add(Dense(64,activation = \"relu\"))\nmodel.add(Dropout(0.5))\n\nmodel.add(Dense(10, activation = \"softmax\"))\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"cae78ff6c36c66abf61a6b68dfe5648dc23b5220"},"cell_type":"code","source":"model.summary()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"05458471db1fdfd9cec5a061f9efd9699bd3e8e3"},"cell_type":"code","source":"# Define the optimizer\noptimizer=Adam(lr=0.003,beta_1=0.9,beta_2=0.999)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8817eecf444d9f44526f03149a8625ed6ad149c8"},"cell_type":"code","source":"#compile model\nmodel.compile(optimizer = optimizer , loss = \"categorical_crossentropy\", metrics=[\"accuracy\"])\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"256ae9910eb9c1409461d50c227a3b4d8e748a89"},"cell_type":"code","source":"# data augmentation I dont use it now\n\ndatagen = ImageDataGenerator(\n    \"\"\"\n        featurewise_center=False,  # set input mean to 0 over the dataset\n        samplewise_center=False,  # set each sample mean to 0\n        featurewise_std_normalization=False,  # divide inputs by std of the dataset\n        samplewise_std_normalization=False,  # divide each input by its std\n        zca_whitening=False,  # dimesion reduction\n        rotation_range=0.5,  # randomly rotate images in the range 5 degrees\n        zoom_range = 0.5, # Randomly zoom image 5%\n        width_shift_range=0.5,  # randomly shift images horizontally 5%\n        height_shift_range=0.5,  # randomly shift images vertically 5%\n        horizontal_flip=False,  # randomly flip images\n        vertical_flip=False)  # randomly flip images\n\"\"\"\n    )\ndatagen.fit(X_train)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"74905bf2dc3a25be60355f8ae626374e2709d708"},"cell_type":"code","source":"#Fit the model\nhistory = model.fit_generator(datagen.flow(X_train,Y_train, batch_size=256),\n                              epochs = 20, validation_data = (X_val,Y_val), steps_per_epoch=X_train.shape[0]//256)\n","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"beb163f35ce8aa8cc2829ec4c6e7d18ac1d8ac09"},"cell_type":"code","source":"test_x = pd.read_csv('../input/test.csv')\ntest_x = test_x.values.reshape(-1,28,28,1)\ntest_x = test_x / 255.0","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"61a1eec180b8277c312ae5837d9b28760e91321c"},"cell_type":"code","source":"predictions=model.predict(test_x)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"7876ee8c496c57242ae46e03789d5afcbe7d943a"},"cell_type":"code","source":"predictions[100]","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"a9146a5e3184f316263235d8f09d6943e929501e"},"cell_type":"code","source":"predictions= np.argmax(predictions, axis=1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"e97ae62c9f4ba3d9005dd6464af88dc4155af172"},"cell_type":"code","source":"submission=pd.DataFrame({'ImageId':range(1,len(test_x)+1),'Label':predictions})\nsubmission.to_csv(\"cnn_results3.csv\",index=False)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"8694b3f36a78e7116922967ff7af062e45d07c29"},"cell_type":"code","source":"submission.head(7)\n","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}