{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# Import\nimport pandas as pd\nimport numpy as np\nimport random\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom keras.utils.np_utils import to_categorical \nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2021-05-20T18:34:45.910236Z","iopub.execute_input":"2021-05-20T18:34:45.910696Z","iopub.status.idle":"2021-05-20T18:34:45.916574Z","shell.execute_reply.started":"2021-05-20T18:34:45.910656Z","shell.execute_reply":"2021-05-20T18:34:45.915303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load data\ntrain = pd.read_csv('../input/digit-recognizer/train.csv')\ntest = pd.read_csv('../input/digit-recognizer/test.csv')\nsubs =  pd.read_csv('../input/digit-recognizer/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2021-05-20T18:32:09.588090Z","iopub.execute_input":"2021-05-20T18:32:09.589048Z","iopub.status.idle":"2021-05-20T18:32:15.986880Z","shell.execute_reply.started":"2021-05-20T18:32:09.589006Z","shell.execute_reply":"2021-05-20T18:32:15.985708Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Find null-values\nprint('Null values in train data: ' + str(train.isnull().any().sum()))\nprint('Null values in test data: ' + str(test.isnull().any().sum()))","metadata":{"execution":{"iopub.status.busy":"2021-05-20T18:32:17.679865Z","iopub.execute_input":"2021-05-20T18:32:17.680411Z","iopub.status.idle":"2021-05-20T18:32:17.712526Z","shell.execute_reply.started":"2021-05-20T18:32:17.680375Z","shell.execute_reply":"2021-05-20T18:32:17.711572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split data for train and prediction datasets\nX = train.drop('label', axis=1).values\ny = train.label.values\nX_pred = test.values\nX = X / 255\nX_pred = X_pred / 255","metadata":{"execution":{"iopub.status.busy":"2021-05-20T18:32:19.651177Z","iopub.execute_input":"2021-05-20T18:32:19.651730Z","iopub.status.idle":"2021-05-20T18:32:20.041400Z","shell.execute_reply.started":"2021-05-20T18:32:19.651694Z","shell.execute_reply":"2021-05-20T18:32:20.040367Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Show 5 random samples from train data\nnn = 5\nfor i, num in enumerate([random.randint(1, X.shape[0]) for _ in range(nn)]):\n    plt.subplot(1, nn, i+1)\n    plt.xlabel(y[num])\n    plt.imshow(X[num].reshape(28, 28))","metadata":{"execution":{"iopub.status.busy":"2021-05-20T18:32:21.315312Z","iopub.execute_input":"2021-05-20T18:32:21.315700Z","iopub.status.idle":"2021-05-20T18:32:22.026056Z","shell.execute_reply.started":"2021-05-20T18:32:21.315667Z","shell.execute_reply":"2021-05-20T18:32:22.024534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Data shape\nprint(X.shape)\nprint(y.shape)\nprint(X_pred.shape)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T18:32:23.587992Z","iopub.execute_input":"2021-05-20T18:32:23.588384Z","iopub.status.idle":"2021-05-20T18:32:23.594780Z","shell.execute_reply.started":"2021-05-20T18:32:23.588349Z","shell.execute_reply":"2021-05-20T18:32:23.593652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert data\nX = X.reshape([42000,28,28,1])\ny = y.reshape([42000,1])\nX_pred = X_pred.reshape([28000,28,28,1])\ny = to_categorical(y, num_classes = 10)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T18:32:25.465814Z","iopub.execute_input":"2021-05-20T18:32:25.466392Z","iopub.status.idle":"2021-05-20T18:32:25.473037Z","shell.execute_reply.started":"2021-05-20T18:32:25.466349Z","shell.execute_reply":"2021-05-20T18:32:25.472226Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split X and y data for train and test datasets\nX_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.1)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T18:32:27.833997Z","iopub.execute_input":"2021-05-20T18:32:27.834366Z","iopub.status.idle":"2021-05-20T18:32:28.459848Z","shell.execute_reply.started":"2021-05-20T18:32:27.834334Z","shell.execute_reply":"2021-05-20T18:32:28.458762Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make model\nmodel = tf.keras.Sequential([])\nmodel.add(tf.keras.layers.Conv2D(32, (3, 3), padding = 'same', activation='relu', input_shape=(28, 28, 1)))\nmodel.add(tf.keras.layers.MaxPooling2D(2, 2)),\nmodel.add(tf.keras.layers.Conv2D(64, (3, 3), padding = 'same', activation='relu'))\nmodel.add(tf.keras.layers.MaxPooling2D(2, 2)),\nmodel.add(tf.keras.layers.Conv2D(128, (3, 3), padding = 'same', activation='relu'))\nmodel.add(tf.keras.layers.MaxPooling2D(2, 2)),\nmodel.add(tf.keras.layers.Conv2D(128, (3, 3), padding = 'same', activation='relu'))\nmodel.add(tf.keras.layers.MaxPooling2D(2, 2)),\nmodel.add(tf.keras.layers.Dropout(0.25))\nmodel.add(tf.keras.layers.Flatten())\nmodel.add(tf.keras.layers.Dense(256, activation='relu'))\nmodel.add(tf.keras.layers.Dropout(0.5))\nmodel.add(tf.keras.layers.Dense(10, activation='softmax'))\nprint(model.summary())","metadata":{"execution":{"iopub.status.busy":"2021-05-20T18:44:12.896810Z","iopub.execute_input":"2021-05-20T18:44:12.897194Z","iopub.status.idle":"2021-05-20T18:44:13.004912Z","shell.execute_reply.started":"2021-05-20T18:44:12.897161Z","shell.execute_reply":"2021-05-20T18:44:13.003859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Complile model\nmodel.compile(optimizer='adam', \n              loss='categorical_crossentropy', \n              metrics=['accuracy']\n             )","metadata":{"execution":{"iopub.status.busy":"2021-05-20T18:44:17.547361Z","iopub.execute_input":"2021-05-20T18:44:17.547929Z","iopub.status.idle":"2021-05-20T18:44:17.561687Z","shell.execute_reply.started":"2021-05-20T18:44:17.547875Z","shell.execute_reply":"2021-05-20T18:44:17.560732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Add Data Generator\ndatagen = ImageDataGenerator(rotation_range=20,\n                             zoom_range=0.2\n                            )\ndatagen.fit(X_train)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T18:44:19.159690Z","iopub.execute_input":"2021-05-20T18:44:19.160197Z","iopub.status.idle":"2021-05-20T18:44:19.387888Z","shell.execute_reply.started":"2021-05-20T18:44:19.160158Z","shell.execute_reply":"2021-05-20T18:44:19.386725Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Fit model\n#model.fit(X_train, y_train, validation_data = (X_test,y_test), epochs=10)\nmodel.fit_generator(datagen.flow(X_train, y_train, batch_size=32),\n                    steps_per_epoch=len(X_train) / 32, epochs=10)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T18:44:20.816645Z","iopub.execute_input":"2021-05-20T18:44:20.817084Z","iopub.status.idle":"2021-05-20T18:51:20.854742Z","shell.execute_reply.started":"2021-05-20T18:44:20.817044Z","shell.execute_reply":"2021-05-20T18:51:20.853483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluation\nmodel.evaluate(X_test,y_test)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T18:51:32.752248Z","iopub.execute_input":"2021-05-20T18:51:32.752849Z","iopub.status.idle":"2021-05-20T18:51:34.340080Z","shell.execute_reply.started":"2021-05-20T18:51:32.752795Z","shell.execute_reply":"2021-05-20T18:51:34.338976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Predict data\npredictions = model.predict(X_pred)\ny_pred = np.argmax(predictions, axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T18:51:40.827377Z","iopub.execute_input":"2021-05-20T18:51:40.827802Z","iopub.status.idle":"2021-05-20T18:51:49.761479Z","shell.execute_reply.started":"2021-05-20T18:51:40.827763Z","shell.execute_reply":"2021-05-20T18:51:49.760378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Show 5 random prediction\nnn = 5\nfor i, num in enumerate([random.randint(1, X_pred.shape[0]) for _ in range(nn)]):\n    plt.subplot(1, nn, i+1)\n    plt.xlabel(y_pred[num])\n    plt.imshow(X_pred[num].reshape(28, 28))    ","metadata":{"execution":{"iopub.status.busy":"2021-05-20T18:51:53.187354Z","iopub.execute_input":"2021-05-20T18:51:53.187866Z","iopub.status.idle":"2021-05-20T18:51:53.659408Z","shell.execute_reply.started":"2021-05-20T18:51:53.187825Z","shell.execute_reply":"2021-05-20T18:51:53.658245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Save data\nsubs['Label'] = pd.DataFrame(y_pred)\nsubs.to_csv('./DRSubmissions.csv', index = False)","metadata":{"execution":{"iopub.status.busy":"2021-05-20T18:51:58.918178Z","iopub.execute_input":"2021-05-20T18:51:58.918593Z","iopub.status.idle":"2021-05-20T18:51:58.997973Z","shell.execute_reply.started":"2021-05-20T18:51:58.918555Z","shell.execute_reply":"2021-05-20T18:51:58.996614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}