{"cells":[{"metadata":{"_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5"},"cell_type":"markdown","source":"# Deep Learning with convolutionnal neural network"},{"metadata":{"_cell_guid":"031e63f9-bbdd-4cf8-a1f2-451b0686b8ff","_uuid":"498d8bdd8c8dac878e4490130ba3f43a08dfeb7e","collapsed":true,"trusted":true},"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt","execution_count":18,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"from keras.models import Sequential\nfrom keras.layers import Dense, Conv2D, Flatten, Dropout\nfrom keras.optimizers import SGD\nfrom keras.callbacks import EarlyStopping\nfrom keras.utils import to_categorical","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"33ba9b7f-e1f0-4d19-a6c5-e4260dc6483a","_uuid":"b841262893d6ebd5266d289f50070e7490998df6","trusted":true},"cell_type":"code","source":"#Loading dataset\ntrain = pd.read_csv('../input/train.csv')\n\n#Reshape data as images 28x28 pixels\npredictors = train.drop(columns=['label']).values.reshape(42000, 28,28,1)\n#Scaling improves the optimization of the neural network\npredictors = predictors / 255\n\n#Get target variable as dummy variables\ntarget = to_categorical(train['label'])\n\nprint(\"Shapes of predictors and target arrays:\", predictors.shape, target.shape)","execution_count":20,"outputs":[]},{"metadata":{"collapsed":true,"trusted":true,"_uuid":"f7e033585b0e17abeae82f38892e6f41c0e20836"},"cell_type":"code","source":"#Building of the neural network\ncnn = Sequential()\n#Input layer\ncnn.add(Conv2D(100, kernel_size=(2,2), activation='relu', input_shape=(28,28,1)))\n#Additional convolutional layers\ncnn.add(Conv2D(100, kernel_size=(2,2), activation='relu'))\ncnn.add(Dropout(0.5))\ncnn.add(Conv2D(100, kernel_size=(2,2), activation='relu'))\ncnn.add(Dropout(0.5))\ncnn.add(Conv2D(100, kernel_size=(2,2), activation='relu'))\ncnn.add(Dropout(0.5))\n#Flattening of the convolutionnal layer\ncnn.add(Flatten())\n#Dense layer between the flattening and output layers\ncnn.add(Dense(200, activation='relu'))\n#Output layer for 10 digits with probability values\ncnn.add(Dense(10, activation='softmax'))\n\n#Compile the model\ncnn.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])","execution_count":11,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"19691b611390c63f3513b9e98f60c4e276b4d420"},"cell_type":"code","source":"#Training of the model\nearly_stopping_monitor = EarlyStopping(patience=3)\ncnn.fit(predictors, target, validation_split=0.3, epochs=50)#, callbacks=[early_stopping_monitor])","execution_count":13,"outputs":[]},{"metadata":{"_uuid":"3868a19a652132c7f08bf4af396253a4736e1f29","trusted":true},"cell_type":"code","source":"#Prepare the submission dataset\ntest = pd.read_csv('../input/test.csv')\nsubmission = test.values.reshape(len(test), 28, 28,1) / 255\nprint(\"Submission dataset shape: \", submission.shape)\n\n#Make predictions on the test set\npredictions = cnn.predict(submission)  #Contains probabilities for each 10 digits\nprint(\"Predictions shape (contains pseudo-probabilities for each digit type) :\", predictions.shape)\n\n#Creating the submission dataframe\nsubmission_df = pd.DataFrame(predictions)\nsubmission_df['ImageId'] = pd.Series(range(1, 28001))\n#Take the corresponding column label for the max value (probability) of the row\nsubmission_df['label'] = submission_df.drop(columns=['ImageId']).idxmax(1)\nprint(\"20 first predictions on the submission digits: \\n\", submission_df[['label']].head(20))\n\n#Print the 10 first images of the submission dataset\nimages_subset = test.iloc[:20, :]\nfor i, row in images_subset.iterrows():\n    plt.subplot(2, 10, i+1)\n    pixels = row.values.reshape((28,28))\n    plt.imshow(pixels, cmap='gray')\n    plt.xticks([])\n    plt.yticks([])\nplt.title('20 first images from the submission dataset')\nplt.show()","execution_count":17,"outputs":[]},{"metadata":{"_uuid":"63ab6a3ae3c9fa998bfc4042cc4530e0f6c06503","collapsed":true,"trusted":true},"cell_type":"code","source":"#Save the submission as csv\nsubmission_df.loc[:, ['ImageId', 'label']].to_csv('submission_cnn_DB.csv', index=False)","execution_count":60,"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}