{"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":"# 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":"2022-03-09T05:00:58.728881Z","iopub.execute_input":"2022-03-09T05:00:58.729218Z","iopub.status.idle":"2022-03-09T05:00:58.757234Z","shell.execute_reply.started":"2022-03-09T05:00:58.729134Z","shell.execute_reply":"2022-03-09T05:00:58.756527Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np \nimport pandas as pd \nfrom tensorflow import keras\nfrom keras import Sequential\nfrom keras.layers import Dense, Conv2D, Flatten, BatchNormalization, Activation\nfrom keras.utils.np_utils import to_categorical\nfrom keras.callbacks import ReduceLROnPlateau\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.optimizers import SGD","metadata":{"execution":{"iopub.status.busy":"2022-03-09T05:00:58.782507Z","iopub.execute_input":"2022-03-09T05:00:58.782787Z","iopub.status.idle":"2022-03-09T05:01:04.069663Z","shell.execute_reply.started":"2022-03-09T05:00:58.782755Z","shell.execute_reply":"2022-03-09T05:01:04.068798Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train_full=pd.read_csv('/kaggle/input/digit-recognizer/train.csv', header='infer')\nx_test=pd.read_csv('/kaggle/input/digit-recognizer/test.csv', header='infer')","metadata":{"execution":{"iopub.status.busy":"2022-03-09T05:01:04.071368Z","iopub.execute_input":"2022-03-09T05:01:04.071629Z","iopub.status.idle":"2022-03-09T05:01:09.048049Z","shell.execute_reply.started":"2022-03-09T05:01:04.071593Z","shell.execute_reply":"2022-03-09T05:01:09.047316Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train=x_train_full.iloc[:,1:]\ny_train=x_train_full.iloc[:,0]\n\nx_train","metadata":{"execution":{"iopub.status.busy":"2022-03-09T05:01:09.049302Z","iopub.execute_input":"2022-03-09T05:01:09.049613Z","iopub.status.idle":"2022-03-09T05:01:09.081828Z","shell.execute_reply.started":"2022-03-09T05:01:09.04954Z","shell.execute_reply":"2022-03-09T05:01:09.081057Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train=x_train/255.\nx_test=x_test/255.","metadata":{"execution":{"iopub.status.busy":"2022-03-09T05:01:09.083975Z","iopub.execute_input":"2022-03-09T05:01:09.084221Z","iopub.status.idle":"2022-03-09T05:01:09.24743Z","shell.execute_reply.started":"2022-03-09T05:01:09.08419Z","shell.execute_reply":"2022-03-09T05:01:09.246686Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# x_train=x_train.values.reshape(-1,28,28,1)\n# x_test=x_test.values.reshape(-1,28,28,1)\n\n# x_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-09T05:01:09.248896Z","iopub.execute_input":"2022-03-09T05:01:09.24944Z","iopub.status.idle":"2022-03-09T05:01:09.253146Z","shell.execute_reply.started":"2022-03-09T05:01:09.249402Z","shell.execute_reply":"2022-03-09T05:01:09.252324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train_vectors=to_categorical(y_train)","metadata":{"execution":{"iopub.status.busy":"2022-03-09T05:01:09.254593Z","iopub.execute_input":"2022-03-09T05:01:09.255073Z","iopub.status.idle":"2022-03-09T05:01:09.262791Z","shell.execute_reply.started":"2022-03-09T05:01:09.255034Z","shell.execute_reply":"2022-03-09T05:01:09.262171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, x_val, y_train, y_val= train_test_split(x_train, y_train_vectors, test_size=0.25, random_state=5)","metadata":{"execution":{"iopub.status.busy":"2022-03-09T05:01:09.264087Z","iopub.execute_input":"2022-03-09T05:01:09.26448Z","iopub.status.idle":"2022-03-09T05:01:09.483777Z","shell.execute_reply.started":"2022-03-09T05:01:09.264446Z","shell.execute_reply":"2022-03-09T05:01:09.483055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x_train.shape)\nprint(x_val.shape)\nprint(y_train.shape)\nprint(y_val.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-09T05:01:09.485116Z","iopub.execute_input":"2022-03-09T05:01:09.485394Z","iopub.status.idle":"2022-03-09T05:01:09.493875Z","shell.execute_reply.started":"2022-03-09T05:01:09.48536Z","shell.execute_reply":"2022-03-09T05:01:09.492944Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Dense(512, input_shape=(784,), activation=None))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(Dense(256, input_shape=(512,), activation=None))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(Dense(128, input_shape=(256,), activation=None))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(Dense(64, input_shape=(128,), activation=None))\nmodel.add(BatchNormalization())\nmodel.add(Activation('relu'))\nmodel.add(Dense(10, input_shape=(64,), activation=None))\nmodel.add(Activation('softmax'))","metadata":{"execution":{"iopub.status.busy":"2022-03-09T05:01:09.495201Z","iopub.execute_input":"2022-03-09T05:01:09.495617Z","iopub.status.idle":"2022-03-09T05:01:11.747044Z","shell.execute_reply.started":"2022-03-09T05:01:09.49558Z","shell.execute_reply":"2022-03-09T05:01:11.746336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sgd = SGD(learning_rate=0.005)\n\nmodel.compile(loss=\"categorical_crossentropy\", optimizer=sgd, metrics=[\"accuracy\"])","metadata":{"execution":{"iopub.status.busy":"2022-03-09T05:01:11.749495Z","iopub.execute_input":"2022-03-09T05:01:11.749932Z","iopub.status.idle":"2022-03-09T05:01:11.761803Z","shell.execute_reply.started":"2022-03-09T05:01:11.749891Z","shell.execute_reply":"2022-03-09T05:01:11.760971Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train_datagen = ImageDataGenerator(featurewise_center=False,\n#                              samplewise_center=False,\n#                              featurewise_std_normalization=False,\n#                              samplewise_std_normalization=False,\n#                              zca_whitening=False,\n#                              rotation_range=10,\n#                              zoom_range=0.1,\n#                              width_shift_range=0.1,\n#                              height_shift_range=0.1,\n#                              horizontal_flip=False,\n#                              vertical_flip=False\n#                             )\n\n# train_generator = train_datagen.flow(x_train, y_train,\n#                                      batch_size=120,\n#                                      shuffle=True)\n\n# val_datagen = ImageDataGenerator()\n# val_generator = val_datagen.flow(x_val, y_val,\n#                                  batch_size=120,\n#                                  shuffle=True)","metadata":{"execution":{"iopub.status.busy":"2022-03-09T05:01:11.763093Z","iopub.execute_input":"2022-03-09T05:01:11.763392Z","iopub.status.idle":"2022-03-09T05:01:11.768709Z","shell.execute_reply.started":"2022-03-09T05:01:11.763359Z","shell.execute_reply":"2022-03-09T05:01:11.767812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"trained_model = model.fit(x_train,y_train,validation_data=(x_val,y_val), epochs=100, batch_size=50)","metadata":{"execution":{"iopub.status.busy":"2022-03-09T05:01:11.770202Z","iopub.execute_input":"2022-03-09T05:01:11.770649Z","iopub.status.idle":"2022-03-09T05:05:24.799179Z","shell.execute_reply.started":"2022-03-09T05:01:11.77061Z","shell.execute_reply":"2022-03-09T05:05:24.798405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions_final = model.predict(x_test)\n\nprint(predictions_final)\n\npredictions_final = np.argmax(predictions_final, axis=1)\n\nprint(predictions_final)","metadata":{"execution":{"iopub.status.busy":"2022-03-09T05:05:43.698957Z","iopub.execute_input":"2022-03-09T05:05:43.699493Z","iopub.status.idle":"2022-03-09T05:05:45.118572Z","shell.execute_reply.started":"2022-03-09T05:05:43.699454Z","shell.execute_reply":"2022-03-09T05:05:45.117831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\n\nsub[\"Label\"] = predictions_final\n\nprint(sub)\n\nsub.to_csv('submission_ann.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-03-09T05:05:50.094873Z","iopub.execute_input":"2022-03-09T05:05:50.095126Z","iopub.status.idle":"2022-03-09T05:05:50.153197Z","shell.execute_reply.started":"2022-03-09T05:05:50.095098Z","shell.execute_reply":"2022-03-09T05:05:50.152496Z"},"trusted":true},"execution_count":null,"outputs":[]}]}