{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30646,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"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\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-02-22T15:21:01.782096Z","iopub.execute_input":"2024-02-22T15:21:01.782487Z","iopub.status.idle":"2024-02-22T15:21:02.263067Z","shell.execute_reply.started":"2024-02-22T15:21:01.782456Z","shell.execute_reply":"2024-02-22T15:21:02.261635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:21:06.556168Z","iopub.execute_input":"2024-02-22T15:21:06.556751Z","iopub.status.idle":"2024-02-22T15:21:13.583208Z","shell.execute_reply.started":"2024-02-22T15:21:06.556686Z","shell.execute_reply":"2024-02-22T15:21:13.581780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:21:25.577161Z","iopub.execute_input":"2024-02-22T15:21:25.578172Z","iopub.status.idle":"2024-02-22T15:21:25.612145Z","shell.execute_reply.started":"2024-02-22T15:21:25.578119Z","shell.execute_reply":"2024-02-22T15:21:25.610750Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:21:44.395999Z","iopub.execute_input":"2024-02-22T15:21:44.398186Z","iopub.status.idle":"2024-02-22T15:21:44.436399Z","shell.execute_reply.started":"2024-02-22T15:21:44.398097Z","shell.execute_reply":"2024-02-22T15:21:44.435036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = train['label']\ntrain.drop('label', inplace = True, axis = \"columns\")","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:22:18.943621Z","iopub.execute_input":"2024-02-22T15:22:18.944045Z","iopub.status.idle":"2024-02-22T15:22:19.071326Z","shell.execute_reply.started":"2024-02-22T15:22:18.944014Z","shell.execute_reply":"2024-02-22T15:22:19.070213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:22:47.444169Z","iopub.execute_input":"2024-02-22T15:22:47.444627Z","iopub.status.idle":"2024-02-22T15:22:47.466098Z","shell.execute_reply.started":"2024-02-22T15:22:47.444594Z","shell.execute_reply":"2024-02-22T15:22:47.464761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:23:14.439890Z","iopub.execute_input":"2024-02-22T15:23:14.440295Z","iopub.status.idle":"2024-02-22T15:23:14.445454Z","shell.execute_reply.started":"2024-02-22T15:23:14.440264Z","shell.execute_reply":"2024-02-22T15:23:14.444393Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample = np.reshape(train.iloc[1, :].values, (28, 28))\nplt.imshow(sample)","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:23:22.759767Z","iopub.execute_input":"2024-02-22T15:23:22.760811Z","iopub.status.idle":"2024-02-22T15:23:23.044837Z","shell.execute_reply.started":"2024-02-22T15:23:22.760763Z","shell.execute_reply":"2024-02-22T15:23:23.043482Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data Preprocessing","metadata":{}},{"cell_type":"code","source":"X = train.values.reshape((len(train), 28, 28))\nX_test = test.values.reshape(len(test), 28, 28)\nX.shape, X_test.shape","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:24:17.780740Z","iopub.execute_input":"2024-02-22T15:24:17.781503Z","iopub.status.idle":"2024-02-22T15:24:17.791050Z","shell.execute_reply.started":"2024-02-22T15:24:17.781463Z","shell.execute_reply":"2024-02-22T15:24:17.789573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = X / 255. #scale the dataset\nX_test = X_test / 255.","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:24:38.677460Z","iopub.execute_input":"2024-02-22T15:24:38.677910Z","iopub.status.idle":"2024-02-22T15:24:38.904524Z","shell.execute_reply.started":"2024-02-22T15:24:38.677876Z","shell.execute_reply":"2024-02-22T15:24:38.903284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Model 1, Logistic Regression**","metadata":{}},{"cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nfrom sklearn.model_selection import cross_val_score\nfrom sklearn.metrics import accuracy_score, f1_score","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:25:16.930816Z","iopub.execute_input":"2024-02-22T15:25:16.931235Z","iopub.status.idle":"2024-02-22T15:25:17.738455Z","shell.execute_reply.started":"2024-02-22T15:25:16.931207Z","shell.execute_reply":"2024-02-22T15:25:17.737044Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"lr = LogisticRegression()\nlr.fit(X.reshape((42000, 28 * 28)), y.values) # reshape the data to this because lr expects 2d data","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:25:46.620873Z","iopub.execute_input":"2024-02-22T15:25:46.621501Z","iopub.status.idle":"2024-02-22T15:25:57.779066Z","shell.execute_reply.started":"2024-02-22T15:25:46.621451Z","shell.execute_reply":"2024-02-22T15:25:57.777515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cross_val_score(LogisticRegression(max_iter = 1000), X.reshape((42000, 28 * 28)), y.values, cv=4).mean()","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:26:37.390893Z","iopub.execute_input":"2024-02-22T15:26:37.391457Z","iopub.status.idle":"2024-02-22T15:30:13.714139Z","shell.execute_reply.started":"2024-02-22T15:26:37.391419Z","shell.execute_reply":"2024-02-22T15:30:13.712513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = lr.predict(X_test.reshape((28000, 28 * 28)))","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:36:07.110363Z","iopub.execute_input":"2024-02-22T15:36:07.110836Z","iopub.status.idle":"2024-02-22T15:36:07.177132Z","shell.execute_reply.started":"2024-02-22T15:36:07.110802Z","shell.execute_reply":"2024-02-22T15:36:07.175327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:36:16.089208Z","iopub.execute_input":"2024-02-22T15:36:16.089631Z","iopub.status.idle":"2024-02-22T15:36:16.098608Z","shell.execute_reply.started":"2024-02-22T15:36:16.089598Z","shell.execute_reply":"2024-02-22T15:36:16.097286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.index.rename('ImageId', inplace=True)","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:36:29.019295Z","iopub.execute_input":"2024-02-22T15:36:29.020663Z","iopub.status.idle":"2024-02-22T15:36:29.025409Z","shell.execute_reply.started":"2024-02-22T15:36:29.020622Z","shell.execute_reply":"2024-02-22T15:36:29.024427Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.index += 1","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:36:38.619490Z","iopub.execute_input":"2024-02-22T15:36:38.619953Z","iopub.status.idle":"2024-02-22T15:36:38.626752Z","shell.execute_reply.started":"2024-02-22T15:36:38.619918Z","shell.execute_reply":"2024-02-22T15:36:38.625798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['label'] = pred\ntest['label'].to_csv('submission 1.csv')","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:36:50.548146Z","iopub.execute_input":"2024-02-22T15:36:50.548563Z","iopub.status.idle":"2024-02-22T15:36:50.594909Z","shell.execute_reply.started":"2024-02-22T15:36:50.548532Z","shell.execute_reply":"2024-02-22T15:36:50.593593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Model 2, Forests","metadata":{}},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:37:38.298390Z","iopub.execute_input":"2024-02-22T15:37:38.298863Z","iopub.status.idle":"2024-02-22T15:37:38.668399Z","shell.execute_reply.started":"2024-02-22T15:37:38.298826Z","shell.execute_reply":"2024-02-22T15:37:38.667159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cross_val_score(RandomForestClassifier(), X.reshape((42000, 28 * 28)), y).mean()","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:38:00.918027Z","iopub.execute_input":"2024-02-22T15:38:00.918478Z","iopub.status.idle":"2024-02-22T15:40:13.595610Z","shell.execute_reply.started":"2024-02-22T15:38:00.918444Z","shell.execute_reply":"2024-02-22T15:40:13.594212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# DEEP LEARNING","metadata":{}},{"cell_type":"code","source":"from sklearn.linear_model import Perceptron","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:40:13.597642Z","iopub.execute_input":"2024-02-22T15:40:13.598003Z","iopub.status.idle":"2024-02-22T15:40:13.604994Z","shell.execute_reply.started":"2024-02-22T15:40:13.597974Z","shell.execute_reply":"2024-02-22T15:40:13.603428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cross_val_score(Perceptron(), X.reshape((42000, 28 * 28)), y).mean()","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:40:13.606313Z","iopub.execute_input":"2024-02-22T15:40:13.606653Z","iopub.status.idle":"2024-02-22T15:40:59.088850Z","shell.execute_reply.started":"2024-02-22T15:40:13.606625Z","shell.execute_reply":"2024-02-22T15:40:59.087250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Stacking multiple layers together","metadata":{}},{"cell_type":"code","source":"import keras\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:44:04.395366Z","iopub.execute_input":"2024-02-22T15:44:04.395887Z","iopub.status.idle":"2024-02-22T15:44:21.950579Z","shell.execute_reply.started":"2024-02-22T15:44:04.395849Z","shell.execute_reply":"2024-02-22T15:44:21.949278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Xtrain, Xval, ytrain, yval = train_test_split(X.reshape((42000, 28 * 28)),y, stratify = y,random_state = 42 )","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:44:30.950169Z","iopub.execute_input":"2024-02-22T15:44:30.950654Z","iopub.status.idle":"2024-02-22T15:44:31.444736Z","shell.execute_reply.started":"2024-02-22T15:44:30.950616Z","shell.execute_reply":"2024-02-22T15:44:31.443499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.models.Sequential()\nmodel.add(keras.layers.Input(shape = (28 * 28)))\nmodel.add(keras.layers.Dense(512, activation = 'relu'))\nmodel.add(keras.layers.Dense(256, activation = 'relu'))\nmodel.add(keras.layers.Dense(128, activation = 'relu'))\nmodel.add(keras.layers.Dropout(rate = 0.25))\nmodel.add(keras.layers.Dense(10, activation = 'softmax'))\nmodel.compile(loss = \"sparse_categorical_crossentropy\", metrics = ['accuracy'], optimizer = \"adam\")","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:46:34.180313Z","iopub.execute_input":"2024-02-22T15:46:34.180801Z","iopub.status.idle":"2024-02-22T15:46:34.275526Z","shell.execute_reply.started":"2024-02-22T15:46:34.180767Z","shell.execute_reply":"2024-02-22T15:46:34.274231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"callback = keras.callbacks.EarlyStopping(patience = 5,monitor = 'val_accuracy')","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:47:06.422266Z","iopub.execute_input":"2024-02-22T15:47:06.422695Z","iopub.status.idle":"2024-02-22T15:47:06.429030Z","shell.execute_reply.started":"2024-02-22T15:47:06.422665Z","shell.execute_reply":"2024-02-22T15:47:06.427681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(Xtrain,ytrain,validation_data = (Xval, yval),epochs = 10, callbacks = [callback])","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:47:34.490126Z","iopub.execute_input":"2024-02-22T15:47:34.491080Z","iopub.status.idle":"2024-02-22T15:49:57.794915Z","shell.execute_reply.started":"2024-02-22T15:47:34.491038Z","shell.execute_reply":"2024-02-22T15:49:57.793589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['train', 'val'], loc='upper left')\nplt.show()\n# summarize history for loss\nplt.plot(history.history['loss'])\nplt.plot(history.history['val_loss'])\nplt.title('model loss')\nplt.ylabel('loss')\nplt.xlabel('epoch')\nplt.legend(['train', 'val'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:49:57.797425Z","iopub.execute_input":"2024-02-22T15:49:57.797964Z","iopub.status.idle":"2024-02-22T15:49:58.306680Z","shell.execute_reply.started":"2024-02-22T15:49:57.797918Z","shell.execute_reply":"2024-02-22T15:49:58.305305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Model is Clearly Overfitting** <br>\nPossible solutions : <br>\nUse batch Normalization <br>\nUse More dropout layers","metadata":{}},{"cell_type":"code","source":"predictions = model.predict(X_test.reshape(28000, 28 * 28))\npredictions","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:50:57.187373Z","iopub.execute_input":"2024-02-22T15:50:57.187894Z","iopub.status.idle":"2024-02-22T15:51:00.466954Z","shell.execute_reply.started":"2024-02-22T15:50:57.187855Z","shell.execute_reply":"2024-02-22T15:51:00.465757Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds = np.argmax(predictions, axis = 1)\npreds","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:51:15.707674Z","iopub.execute_input":"2024-02-22T15:51:15.710980Z","iopub.status.idle":"2024-02-22T15:51:15.719025Z","shell.execute_reply.started":"2024-02-22T15:51:15.710931Z","shell.execute_reply":"2024-02-22T15:51:15.717787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.index.rename('ImageId', inplace=True)\ntest.index += 1\ntest['label'] = preds\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:51:32.832139Z","iopub.execute_input":"2024-02-22T15:51:32.832846Z","iopub.status.idle":"2024-02-22T15:51:32.858341Z","shell.execute_reply.started":"2024-02-22T15:51:32.832788Z","shell.execute_reply":"2024-02-22T15:51:32.857163Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['label'].to_csv(\"submission stacking.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:52:21.769327Z","iopub.execute_input":"2024-02-22T15:52:21.769742Z","iopub.status.idle":"2024-02-22T15:52:21.813854Z","shell.execute_reply.started":"2024-02-22T15:52:21.769697Z","shell.execute_reply":"2024-02-22T15:52:21.812661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Convolutional Neural Networks","metadata":{}},{"cell_type":"code","source":"from keras.preprocessing.image import ImageDataGenerator\nimport keras","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:53:28.110192Z","iopub.execute_input":"2024-02-22T15:53:28.110627Z","iopub.status.idle":"2024-02-22T15:53:28.117015Z","shell.execute_reply.started":"2024-02-22T15:53:28.110595Z","shell.execute_reply":"2024-02-22T15:53:28.115557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_aug = ImageDataGenerator(\n        vertical_flip = False,\n        fill_mode = 'nearest',\n        rotation_range = 10,  \n        zoom_range = 0.1, \n        width_shift_range = 0.1, \n        height_shift_range = 0.1)","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:53:50.447492Z","iopub.execute_input":"2024-02-22T15:53:50.447957Z","iopub.status.idle":"2024-02-22T15:53:50.454374Z","shell.execute_reply.started":"2024-02-22T15:53:50.447915Z","shell.execute_reply":"2024-02-22T15:53:50.453006Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"../input/digit-recognizer/train.csv\")\ntest = pd.read_csv(\"../input/digit-recognizer/test.csv\")\n\n\nX_train = train.drop(labels = [\"label\"], axis = 1)\ny_train = train['label']\n\nX_test = test","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:54:02.970389Z","iopub.execute_input":"2024-02-22T15:54:02.970874Z","iopub.status.idle":"2024-02-22T15:54:08.669855Z","shell.execute_reply.started":"2024-02-22T15:54:02.970837Z","shell.execute_reply":"2024-02-22T15:54:08.668683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.values.reshape(-1, 28, 28, 1)\nX_test = X_test.values.reshape(-1, 28, 28, 1)","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:54:36.610828Z","iopub.execute_input":"2024-02-22T15:54:36.611238Z","iopub.status.idle":"2024-02-22T15:54:36.617581Z","shell.execute_reply.started":"2024-02-22T15:54:36.611209Z","shell.execute_reply":"2024-02-22T15:54:36.616303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nXtrain, Xval, ytrain, yval = train_test_split(X_train, y_train, random_state = 42)","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:54:48.600763Z","iopub.execute_input":"2024-02-22T15:54:48.601392Z","iopub.status.idle":"2024-02-22T15:54:49.083174Z","shell.execute_reply.started":"2024-02-22T15:54:48.601342Z","shell.execute_reply":"2024-02-22T15:54:49.081776Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_gen = train_aug.flow(Xtrain, ytrain, batch_size=64)\ntest_gen = train_aug.flow(Xval, yval, batch_size = 64)","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:57:00.699325Z","iopub.execute_input":"2024-02-22T15:57:00.699769Z","iopub.status.idle":"2024-02-22T15:57:00.767799Z","shell.execute_reply.started":"2024-02-22T15:57:00.699735Z","shell.execute_reply":"2024-02-22T15:57:00.766583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Conv2D, MaxPool2D, BatchNormalization, Dense, Dropout, Flatten\nfrom tensorflow.keras.optimizers import Adam\nmodel = Sequential()\n\nmodel.add(Conv2D(32, 3, padding = 'same', kernel_initializer = 'he_normal', activation = 'relu', input_shape = (28,28,1)))\nmodel.add(MaxPool2D(2))\nmodel.add(BatchNormalization())\n\nmodel.add(Conv2D(64, 3, padding = 'same', kernel_initializer = 'he_normal', activation = 'relu'))\nmodel.add(MaxPool2D(2))\nmodel.add(BatchNormalization())\n\nmodel.add(Conv2D(128, 3, padding = 'same', kernel_initializer = 'he_normal', activation = 'relu'))\nmodel.add(MaxPool2D(2))\nmodel.add(BatchNormalization())\n\nmodel.add(Flatten())\nmodel.add(Dropout(0.3))\n\nmodel.add(Dense(128, kernel_initializer = 'he_normal', activation = 'relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(10, activation = 'softmax'))\n\nmodel.summary()\nmodel.compile(optimizer = \"adam\", loss = 'sparse_categorical_crossentropy' , metrics = ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:57:09.407846Z","iopub.execute_input":"2024-02-22T15:57:09.408303Z","iopub.status.idle":"2024-02-22T15:57:09.665928Z","shell.execute_reply.started":"2024-02-22T15:57:09.408270Z","shell.execute_reply":"2024-02-22T15:57:09.664575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(train_gen, epochs = 30, validation_data = test_gen)","metadata":{"execution":{"iopub.status.busy":"2024-02-22T15:57:17.829350Z","iopub.execute_input":"2024-02-22T15:57:17.829835Z","iopub.status.idle":"2024-02-22T16:18:51.160539Z","shell.execute_reply.started":"2024-02-22T15:57:17.829788Z","shell.execute_reply":"2024-02-22T16:18:51.159251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_gen = train_aug.flow(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-02-22T16:24:00.878505Z","iopub.execute_input":"2024-02-22T16:24:00.878994Z","iopub.status.idle":"2024-02-22T16:24:00.931101Z","shell.execute_reply.started":"2024-02-22T16:24:00.878958Z","shell.execute_reply":"2024-02-22T16:24:00.929646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(test_gen)","metadata":{"execution":{"iopub.status.busy":"2024-02-22T16:24:04.013146Z","iopub.execute_input":"2024-02-22T16:24:04.013668Z","iopub.status.idle":"2024-02-22T16:24:19.925148Z","shell.execute_reply.started":"2024-02-22T16:24:04.013632Z","shell.execute_reply":"2024-02-22T16:24:19.923697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = np.argmax(predictions, axis = 1)\npredictions","metadata":{"execution":{"iopub.status.busy":"2024-02-22T16:24:19.927391Z","iopub.execute_input":"2024-02-22T16:24:19.928084Z","iopub.status.idle":"2024-02-22T16:24:19.937539Z","shell.execute_reply.started":"2024-02-22T16:24:19.928046Z","shell.execute_reply":"2024-02-22T16:24:19.936126Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['label'] = predictions","metadata":{"execution":{"iopub.status.busy":"2024-02-22T16:24:19.939273Z","iopub.execute_input":"2024-02-22T16:24:19.939681Z","iopub.status.idle":"2024-02-22T16:24:19.949787Z","shell.execute_reply.started":"2024-02-22T16:24:19.939641Z","shell.execute_reply":"2024-02-22T16:24:19.948210Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.to_csv(\"keras submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-02-22T16:24:19.952180Z","iopub.execute_input":"2024-02-22T16:24:19.952746Z","iopub.status.idle":"2024-02-22T16:24:28.394240Z","shell.execute_reply.started":"2024-02-22T16:24:19.952677Z","shell.execute_reply":"2024-02-22T16:24:28.392859Z"},"trusted":true},"execution_count":null,"outputs":[]}]}