{"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":"2021-10-15T18:30:55.996607Z","iopub.execute_input":"2021-10-15T18:30:55.996980Z","iopub.status.idle":"2021-10-15T18:30:56.031827Z","shell.execute_reply.started":"2021-10-15T18:30:55.996877Z","shell.execute_reply":"2021-10-15T18:30:56.030495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport matplotlib.pyplot as plt\nfrom tensorflow.keras import layers,models\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2021-10-15T18:30:59.700169Z","iopub.execute_input":"2021-10-15T18:30:59.700468Z","iopub.status.idle":"2021-10-15T18:31:06.762510Z","shell.execute_reply.started":"2021-10-15T18:30:59.700436Z","shell.execute_reply":"2021-10-15T18:31:06.761810Z"},"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\")\nSample= pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2021-10-15T18:31:06.764879Z","iopub.execute_input":"2021-10-15T18:31:06.765320Z","iopub.status.idle":"2021-10-15T18:31:12.876183Z","shell.execute_reply.started":"2021-10-15T18:31:06.765285Z","shell.execute_reply":"2021-10-15T18:31:12.875241Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Train.head()","metadata":{"execution":{"iopub.status.busy":"2021-10-15T18:31:12.877498Z","iopub.execute_input":"2021-10-15T18:31:12.877759Z","iopub.status.idle":"2021-10-15T18:31:12.908313Z","shell.execute_reply.started":"2021-10-15T18:31:12.877729Z","shell.execute_reply":"2021-10-15T18:31:12.907335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X, Y = Train.drop('label', 1).values, Train.label.values\n","metadata":{"execution":{"iopub.status.busy":"2021-10-15T18:31:12.910846Z","iopub.execute_input":"2021-10-15T18:31:12.911617Z","iopub.status.idle":"2021-10-15T18:31:13.029635Z","shell.execute_reply.started":"2021-10-15T18:31:12.911540Z","shell.execute_reply":"2021-10-15T18:31:13.028071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val, Y_train, Y_val = train_test_split(X, Y, test_size=0.3, random_state=1, )\n\nX_train = X_train.reshape(-1,28,28)\nX_val = X_val.reshape(-1,28,28)","metadata":{"execution":{"iopub.status.busy":"2021-10-15T18:31:43.291750Z","iopub.execute_input":"2021-10-15T18:31:43.292466Z","iopub.status.idle":"2021-10-15T18:31:43.980739Z","shell.execute_reply.started":"2021-10-15T18:31:43.292423Z","shell.execute_reply":"2021-10-15T18:31:43.979727Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Train_data =np.expand_dims(X_train, axis=-1)\nValid_data=np.expand_dims(X_val, axis=-1)\nTrain_abel=Y_train\nValid_label=Y_val","metadata":{"execution":{"iopub.status.busy":"2021-10-15T18:32:22.327792Z","iopub.execute_input":"2021-10-15T18:32:22.328294Z","iopub.status.idle":"2021-10-15T18:32:22.332447Z","shell.execute_reply.started":"2021-10-15T18:32:22.328220Z","shell.execute_reply":"2021-10-15T18:32:22.331733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Valid_label= np.array(Valid_label)\nTrain_abel = np.array(Train_abel)\nValid_label=Valid_label.reshape(-1)\nTrain_abel=Train_abel.reshape(-1)","metadata":{"execution":{"iopub.status.busy":"2021-10-15T18:32:23.059026Z","iopub.execute_input":"2021-10-15T18:32:23.059481Z","iopub.status.idle":"2021-10-15T18:32:23.065865Z","shell.execute_reply.started":"2021-10-15T18:32:23.059444Z","shell.execute_reply":"2021-10-15T18:32:23.064780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Train_data.shape","metadata":{"execution":{"iopub.status.busy":"2021-10-15T18:32:23.955538Z","iopub.execute_input":"2021-10-15T18:32:23.955880Z","iopub.status.idle":"2021-10-15T18:32:23.961518Z","shell.execute_reply.started":"2021-10-15T18:32:23.955846Z","shell.execute_reply":"2021-10-15T18:32:23.960681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\nTest=np.array(Test)\nTest_data=Test.reshape(-1,28,28)\nTest_data= np.expand_dims(Test_data,axis=-1)","metadata":{"execution":{"iopub.status.busy":"2021-10-15T18:32:24.666358Z","iopub.execute_input":"2021-10-15T18:32:24.667004Z","iopub.status.idle":"2021-10-15T18:32:24.759725Z","shell.execute_reply.started":"2021-10-15T18:32:24.666945Z","shell.execute_reply":"2021-10-15T18:32:24.758696Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Test_data.shape","metadata":{"execution":{"iopub.status.busy":"2021-10-15T18:32:25.452287Z","iopub.execute_input":"2021-10-15T18:32:25.453002Z","iopub.status.idle":"2021-10-15T18:32:25.459577Z","shell.execute_reply.started":"2021-10-15T18:32:25.452962Z","shell.execute_reply":"2021-10-15T18:32:25.458379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = models.Sequential()\nmodel.add(layers.Conv2D(32, 3, padding='same', activation='relu', input_shape=Train_data[0].shape))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.BatchNormalization())\nmodel.add(layers.Conv2D(64, (3, 3), activation='relu'))\nmodel.add(layers.MaxPooling2D((2, 2)))\nmodel.add(layers.BatchNormalization())\nmodel.add(layers.Conv2D(128, (3, 3), activation='relu'))\nmodel.add(layers.Flatten())\nmodel.add(layers.Dense(100, activation='relu'))\nmodel.add(layers.Dense(10))","metadata":{"execution":{"iopub.status.busy":"2021-10-15T18:32:26.187990Z","iopub.execute_input":"2021-10-15T18:32:26.188325Z","iopub.status.idle":"2021-10-15T18:32:26.639869Z","shell.execute_reply.started":"2021-10-15T18:32:26.188295Z","shell.execute_reply":"2021-10-15T18:32:26.638955Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-10-15T18:32:27.553218Z","iopub.execute_input":"2021-10-15T18:32:27.554315Z","iopub.status.idle":"2021-10-15T18:32:27.564622Z","shell.execute_reply.started":"2021-10-15T18:32:27.554274Z","shell.execute_reply":"2021-10-15T18:32:27.563722Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',\n              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),\n              metrics=['accuracy'])\nstopping = tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=25, restore_best_weights=True)","metadata":{"execution":{"iopub.status.busy":"2021-10-15T18:32:28.953234Z","iopub.execute_input":"2021-10-15T18:32:28.954247Z","iopub.status.idle":"2021-10-15T18:32:28.975021Z","shell.execute_reply.started":"2021-10-15T18:32:28.954178Z","shell.execute_reply":"2021-10-15T18:32:28.973939Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"AJ = model.fit(Train_data, Train_abel, epochs=15, \n                    validation_data=(Valid_data, Valid_label),callbacks=[stopping])","metadata":{"execution":{"iopub.status.busy":"2021-10-15T18:32:40.257239Z","iopub.execute_input":"2021-10-15T18:32:40.258075Z","iopub.status.idle":"2021-10-15T18:38:12.871258Z","shell.execute_reply.started":"2021-10-15T18:32:40.258014Z","shell.execute_reply":"2021-10-15T18:38:12.869961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(Test_data)\n\n","metadata":{"execution":{"iopub.status.busy":"2021-10-15T18:38:16.780405Z","iopub.execute_input":"2021-10-15T18:38:16.781438Z","iopub.status.idle":"2021-10-15T18:38:22.476754Z","shell.execute_reply.started":"2021-10-15T18:38:16.781389Z","shell.execute_reply":"2021-10-15T18:38:22.476043Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index = tf.argmax(predictions, axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-10-15T18:38:22.478759Z","iopub.execute_input":"2021-10-15T18:38:22.479751Z","iopub.status.idle":"2021-10-15T18:38:22.485615Z","shell.execute_reply.started":"2021-10-15T18:38:22.479694Z","shell.execute_reply":"2021-10-15T18:38:22.485051Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"index.shape","metadata":{"execution":{"iopub.status.busy":"2021-10-15T18:38:22.486986Z","iopub.execute_input":"2021-10-15T18:38:22.487935Z","iopub.status.idle":"2021-10-15T18:38:22.506591Z","shell.execute_reply.started":"2021-10-15T18:38:22.487878Z","shell.execute_reply":"2021-10-15T18:38:22.505923Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Sample[\"Label\"]=index","metadata":{"execution":{"iopub.status.busy":"2021-10-15T18:38:22.694476Z","iopub.execute_input":"2021-10-15T18:38:22.695386Z","iopub.status.idle":"2021-10-15T18:38:22.700701Z","shell.execute_reply.started":"2021-10-15T18:38:22.695350Z","shell.execute_reply":"2021-10-15T18:38:22.699980Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Sample.to_csv(\"Su.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2021-10-15T18:38:27.043809Z","iopub.execute_input":"2021-10-15T18:38:27.044919Z","iopub.status.idle":"2021-10-15T18:38:27.106532Z","shell.execute_reply.started":"2021-10-15T18:38:27.044871Z","shell.execute_reply":"2021-10-15T18:38:27.105472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}