{"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-08-12T09:02:33.813545Z","iopub.execute_input":"2022-08-12T09:02:33.818191Z","iopub.status.idle":"2022-08-12T09:02:33.863385Z","shell.execute_reply.started":"2022-08-12T09:02:33.818046Z","shell.execute_reply":"2022-08-12T09:02:33.861800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nimport matplotlib.pyplot as plt\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:02:35.027881Z","iopub.execute_input":"2022-08-12T09:02:35.028377Z","iopub.status.idle":"2022-08-12T09:02:35.715475Z","shell.execute_reply.started":"2022-08-12T09:02:35.028327Z","shell.execute_reply":"2022-08-12T09:02:35.714059Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras import layers","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:02:35.718440Z","iopub.execute_input":"2022-08-12T09:02:35.718971Z","iopub.status.idle":"2022-08-12T09:02:42.655815Z","shell.execute_reply.started":"2022-08-12T09:02:35.718927Z","shell.execute_reply":"2022-08-12T09:02:42.654518Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dir = '/kaggle/input/digit-recognizer/train.csv'\ntest_dir = '/kaggle/input/digit-recognizer/test.csv'","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:02:42.659623Z","iopub.execute_input":"2022-08-12T09:02:42.661016Z","iopub.status.idle":"2022-08-12T09:02:42.669783Z","shell.execute_reply.started":"2022-08-12T09:02:42.660957Z","shell.execute_reply":"2022-08-12T09:02:42.666529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv(train_dir)\ndf_test = pd.read_csv(test_dir)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:02:42.679377Z","iopub.execute_input":"2022-08-12T09:02:42.679761Z","iopub.status.idle":"2022-08-12T09:02:50.368380Z","shell.execute_reply.started":"2022-08-12T09:02:42.679732Z","shell.execute_reply":"2022-08-12T09:02:50.366559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df_train.drop('label',1)\ny = df_train['label']","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:02:50.372138Z","iopub.execute_input":"2022-08-12T09:02:50.376798Z","iopub.status.idle":"2022-08-12T09:02:50.514806Z","shell.execute_reply.started":"2022-08-12T09:02:50.376747Z","shell.execute_reply":"2022-08-12T09:02:50.513534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train,X_test, y_train, y_test = train_test_split(X,y, test_size = 0.25, random_state = 42)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:02:50.520278Z","iopub.execute_input":"2022-08-12T09:02:50.523236Z","iopub.status.idle":"2022-08-12T09:02:50.931332Z","shell.execute_reply.started":"2022-08-12T09:02:50.523167Z","shell.execute_reply":"2022-08-12T09:02:50.929847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# X_train = X_train/255.\n# X_test = X_test/255.","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:02:50.937684Z","iopub.execute_input":"2022-08-12T09:02:50.940679Z","iopub.status.idle":"2022-08-12T09:02:50.948515Z","shell.execute_reply.started":"2022-08-12T09:02:50.940631Z","shell.execute_reply":"2022-08-12T09:02:50.947115Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.values.reshape(-1,28,28,1)\nX_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:02:50.954512Z","iopub.execute_input":"2022-08-12T09:02:50.957917Z","iopub.status.idle":"2022-08-12T09:02:50.976388Z","shell.execute_reply.started":"2022-08-12T09:02:50.955443Z","shell.execute_reply":"2022-08-12T09:02:50.975001Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = X_test.values.reshape(-1,28,28,1)\nX_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:02:50.982338Z","iopub.execute_input":"2022-08-12T09:02:50.983732Z","iopub.status.idle":"2022-08-12T09:02:50.998724Z","shell.execute_reply.started":"2022-08-12T09:02:50.983675Z","shell.execute_reply":"2022-08-12T09:02:50.997348Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"num_classes = len(df_train['label'].unique())\nclasses = df_train['label'].unique()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:02:51.001734Z","iopub.execute_input":"2022-08-12T09:02:51.003127Z","iopub.status.idle":"2022-08-12T09:02:51.019944Z","shell.execute_reply.started":"2022-08-12T09:02:51.003083Z","shell.execute_reply":"2022-08-12T09:02:51.018388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = keras.utils.to_categorical(y_train,num_classes)\ny_test = keras.utils.to_categorical(y_test,num_classes)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:02:51.031019Z","iopub.execute_input":"2022-08-12T09:02:51.034084Z","iopub.status.idle":"2022-08-12T09:02:51.045811Z","shell.execute_reply.started":"2022-08-12T09:02:51.034024Z","shell.execute_reply":"2022-08-12T09:02:51.043134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset = tf.data.Dataset.from_tensor_slices((X_train, y_train))\ndataset = dataset.batch(32)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:02:51.048185Z","iopub.execute_input":"2022-08-12T09:02:51.048781Z","iopub.status.idle":"2022-08-12T09:02:55.679754Z","shell.execute_reply.started":"2022-08-12T09:02:51.048718Z","shell.execute_reply":"2022-08-12T09:02:55.678202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset ","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:02:55.681374Z","iopub.execute_input":"2022-08-12T09:02:55.682159Z","iopub.status.idle":"2022-08-12T09:02:55.697125Z","shell.execute_reply.started":"2022-08-12T09:02:55.682115Z","shell.execute_reply":"2022-08-12T09:02:55.695362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"dataset_test = tf.data.Dataset.from_tensor_slices((X_test, y_test)).batch(32)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:02:55.699291Z","iopub.execute_input":"2022-08-12T09:02:55.700299Z","iopub.status.idle":"2022-08-12T09:02:55.977210Z","shell.execute_reply.started":"2022-08-12T09:02:55.700259Z","shell.execute_reply":"2022-08-12T09:02:55.975535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for images,label in dataset.take(1):\n    print(label)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:02:55.979257Z","iopub.execute_input":"2022-08-12T09:02:55.979976Z","iopub.status.idle":"2022-08-12T09:02:56.395141Z","shell.execute_reply.started":"2022-08-12T09:02:55.979934Z","shell.execute_reply":"2022-08-12T09:02:56.393544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(images[0].numpy())","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:02:56.400339Z","iopub.execute_input":"2022-08-12T09:02:56.403452Z","iopub.status.idle":"2022-08-12T09:02:56.721942Z","shell.execute_reply.started":"2022-08-12T09:02:56.403400Z","shell.execute_reply":"2022-08-12T09:02:56.720628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def model(img_height, img_width,num_classes):\n    mod = keras.Sequential([\n                          layers.Rescaling(1./255, input_shape=(img_height, img_width, 1)),\n                          layers.Conv2D(16, 3, padding='same', activation='relu'),\n                          layers.Conv2D(32, 3, padding='same', activation='relu'),\n                          layers.Conv2D(64, 3, padding='same', activation='relu'),\n                          layers.Conv2D(128, 3, padding='same', activation='relu'),\n                          layers.MaxPooling2D(),\n                          \n                          layers.Flatten(),\n                          layers.Dense(64, activation='relu'),\n                          layers.Dense(128, activation='relu'),\n                          layers.Dense(64, activation='relu'),\n                          layers.Dense(num_classes)\n                        ])\n    \n    return mod","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:02:56.724778Z","iopub.execute_input":"2022-08-12T09:02:56.725763Z","iopub.status.idle":"2022-08-12T09:02:56.736434Z","shell.execute_reply.started":"2022-08-12T09:02:56.725712Z","shell.execute_reply":"2022-08-12T09:02:56.734533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = model(28,28,num_classes)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:02:56.739381Z","iopub.execute_input":"2022-08-12T09:02:56.741060Z","iopub.status.idle":"2022-08-12T09:02:56.883084Z","shell.execute_reply.started":"2022-08-12T09:02:56.740998Z","shell.execute_reply":"2022-08-12T09:02:56.881789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',\n              loss=tf.keras.losses.CategoricalCrossentropy(from_logits = True),\n              metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:02:56.885541Z","iopub.execute_input":"2022-08-12T09:02:56.885873Z","iopub.status.idle":"2022-08-12T09:02:56.905494Z","shell.execute_reply.started":"2022-08-12T09:02:56.885843Z","shell.execute_reply":"2022-08-12T09:02:56.903729Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:02:56.908585Z","iopub.execute_input":"2022-08-12T09:02:56.910078Z","iopub.status.idle":"2022-08-12T09:02:56.920651Z","shell.execute_reply.started":"2022-08-12T09:02:56.910033Z","shell.execute_reply":"2022-08-12T09:02:56.919181Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"epochs=10\nhistory = model.fit(dataset,\n                    validation_data=dataset_test,\n                    epochs=epochs,\n                    verbose = 1\n                    )","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:02:56.922860Z","iopub.execute_input":"2022-08-12T09:02:56.923954Z","iopub.status.idle":"2022-08-12T09:04:43.734768Z","shell.execute_reply.started":"2022-08-12T09:02:56.923890Z","shell.execute_reply":"2022-08-12T09:04:43.733029Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i,j in dataset_test.take(1):\n    print(i.shape,j.shape)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:04:43.736648Z","iopub.execute_input":"2022-08-12T09:04:43.737466Z","iopub.status.idle":"2022-08-12T09:04:43.838592Z","shell.execute_reply.started":"2022-08-12T09:04:43.737421Z","shell.execute_reply":"2022-08-12T09:04:43.832344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = history.history['loss']\nacc = history.history['accuracy']\nval_loss = history.history['val_loss']\nval_accuracy = history.history['val_accuracy']","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:04:43.840488Z","iopub.execute_input":"2022-08-12T09:04:43.841255Z","iopub.status.idle":"2022-08-12T09:04:43.852615Z","shell.execute_reply.started":"2022-08-12T09:04:43.841214Z","shell.execute_reply":"2022-08-12T09:04:43.850298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(range(len(loss)),loss)\nplt.plot(range(len(val_loss)),val_loss)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:04:43.854898Z","iopub.execute_input":"2022-08-12T09:04:43.855776Z","iopub.status.idle":"2022-08-12T09:04:44.078886Z","shell.execute_reply.started":"2022-08-12T09:04:43.855735Z","shell.execute_reply":"2022-08-12T09:04:44.077573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(range(len(acc)),acc)\nplt.plot(range(len(val_accuracy)),val_accuracy)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:04:44.081260Z","iopub.execute_input":"2022-08-12T09:04:44.082667Z","iopub.status.idle":"2022-08-12T09:04:44.291315Z","shell.execute_reply.started":"2022-08-12T09:04:44.082627Z","shell.execute_reply":"2022-08-12T09:04:44.290100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test = df_test.values.reshape(-1,28,28,1)\ntestdata = tf.data.Dataset.from_tensor_slices(df_test).batch(32)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:04:44.293105Z","iopub.execute_input":"2022-08-12T09:04:44.293523Z","iopub.status.idle":"2022-08-12T09:04:44.813373Z","shell.execute_reply.started":"2022-08-12T09:04:44.293457Z","shell.execute_reply":"2022-08-12T09:04:44.812063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(testdata)\ny_pred = np.argmax(y_pred,1)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:04:44.815406Z","iopub.execute_input":"2022-08-12T09:04:44.815898Z","iopub.status.idle":"2022-08-12T09:04:47.178031Z","shell.execute_reply.started":"2022-08-12T09:04:44.815855Z","shell.execute_reply":"2022-08-12T09:04:47.176536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame()\ndf['Label'] = y_pred\ndf['ImageId'] = list(range(1, len(y_pred) + 1 ))\ndf.to_csv('submission.csv',index = False)","metadata":{"execution":{"iopub.status.busy":"2022-08-12T09:04:47.180607Z","iopub.execute_input":"2022-08-12T09:04:47.180977Z","iopub.status.idle":"2022-08-12T09:04:47.235885Z","shell.execute_reply.started":"2022-08-12T09:04:47.180947Z","shell.execute_reply":"2022-08-12T09:04:47.234675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred[0]","metadata":{"execution":{"iopub.status.busy":"2022-07-08T13:04:02.280226Z","iopub.execute_input":"2022-07-08T13:04:02.280779Z","iopub.status.idle":"2022-07-08T13:04:02.290038Z","shell.execute_reply.started":"2022-07-08T13:04:02.280730Z","shell.execute_reply":"2022-07-08T13:04:02.288795Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(df_test[0])","metadata":{"execution":{"iopub.status.busy":"2022-07-08T13:05:52.215092Z","iopub.execute_input":"2022-07-08T13:05:52.215577Z","iopub.status.idle":"2022-07-08T13:05:52.403970Z","shell.execute_reply.started":"2022-07-08T13:05:52.215516Z","shell.execute_reply":"2022-07-08T13:05:52.402863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test[0].shape","metadata":{"execution":{"iopub.status.busy":"2022-07-08T13:05:19.977109Z","iopub.execute_input":"2022-07-08T13:05:19.977528Z","iopub.status.idle":"2022-07-08T13:05:19.984424Z","shell.execute_reply.started":"2022-07-08T13:05:19.977498Z","shell.execute_reply":"2022-07-08T13:05:19.983636Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-08T13:16:54.927484Z","iopub.execute_input":"2022-07-08T13:16:54.927993Z","iopub.status.idle":"2022-07-08T13:16:54.965128Z","shell.execute_reply.started":"2022-07-08T13:16:54.927956Z","shell.execute_reply":"2022-07-08T13:16:54.963782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}