{"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":"2023-02-16T04:41:57.493317Z","iopub.execute_input":"2023-02-16T04:41:57.493637Z","iopub.status.idle":"2023-02-16T04:41:57.528623Z","shell.execute_reply.started":"2023-02-16T04:41:57.493570Z","shell.execute_reply":"2023-02-16T04:41:57.527698Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.layers import Dense\nfrom tensorflow.keras.layers import Dropout\nfrom tensorflow.keras.layers import Flatten\nfrom tensorflow.keras.layers import Conv2D\nfrom tensorflow.keras.layers import MaxPooling2D, BatchNormalization\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras import Model\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2023-02-16T04:42:08.070293Z","iopub.execute_input":"2023-02-16T04:42:08.071018Z","iopub.status.idle":"2023-02-16T04:42:15.823311Z","shell.execute_reply.started":"2023-02-16T04:42:08.070979Z","shell.execute_reply":"2023-02-16T04:42:15.822279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train=pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ndf_test=pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-02-16T04:42:15.825481Z","iopub.execute_input":"2023-02-16T04:42:15.826218Z","iopub.status.idle":"2023-02-16T04:42:20.532860Z","shell.execute_reply.started":"2023-02-16T04:42:15.826178Z","shell.execute_reply":"2023-02-16T04:42:20.531826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-16T04:42:20.534079Z","iopub.execute_input":"2023-02-16T04:42:20.534436Z","iopub.status.idle":"2023-02-16T04:42:20.561336Z","shell.execute_reply.started":"2023-02-16T04:42:20.534384Z","shell.execute_reply":"2023-02-16T04:42:20.560385Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test","metadata":{"execution":{"iopub.status.busy":"2023-02-16T04:42:24.720383Z","iopub.execute_input":"2023-02-16T04:42:24.720766Z","iopub.status.idle":"2023-02-16T04:42:24.744420Z","shell.execute_reply.started":"2023-02-16T04:42:24.720736Z","shell.execute_reply":"2023-02-16T04:42:24.743535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pixel_data=df_train.iloc[:,1:785] #from col 1 to 785\nlabel=df_train[\"label\"]\n\nnumber=np.array(pixel_data)\nnumber=number.reshape(len(pixel_data),28,28,1)\n\n# X_train, X_test, y_train, y_test= train_test_split(number, label, test_size=0.2, random_state=42)\ny_train = label\nX_train = number\n","metadata":{"execution":{"iopub.status.busy":"2023-02-16T04:44:11.956745Z","iopub.execute_input":"2023-02-16T04:44:11.957094Z","iopub.status.idle":"2023-02-16T04:44:12.058402Z","shell.execute_reply.started":"2023-02-16T04:44:11.957066Z","shell.execute_reply":"2023-02-16T04:44:12.057437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-02-16T04:44:40.396854Z","iopub.execute_input":"2023-02-16T04:44:40.397323Z","iopub.status.idle":"2023-02-16T04:44:40.403664Z","shell.execute_reply.started":"2023-02-16T04:44:40.397291Z","shell.execute_reply":"2023-02-16T04:44:40.402481Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = to_categorical(y_train)\n# y_test = to_categorical(y_test)\nnum_classes = y_train.shape[1]\nnum_classes \n\n\n","metadata":{"execution":{"iopub.status.busy":"2023-02-16T04:44:41.851710Z","iopub.execute_input":"2023-02-16T04:44:41.852112Z","iopub.status.idle":"2023-02-16T04:44:41.860933Z","shell.execute_reply.started":"2023-02-16T04:44:41.852077Z","shell.execute_reply":"2023-02-16T04:44:41.859874Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-02-16T04:44:51.719143Z","iopub.execute_input":"2023-02-16T04:44:51.719517Z","iopub.status.idle":"2023-02-16T04:44:51.726182Z","shell.execute_reply.started":"2023-02-16T04:44:51.719486Z","shell.execute_reply":"2023-02-16T04:44:51.725068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Layers for Model Class API\n\ninputs = tf.keras.Input(shape=(28,28,1))\nconv1 = Conv2D(32, (5, 5), input_shape=(28, 28, 1), activation='relu')(inputs)\nbatchnorm1 = BatchNormalization()(conv1)\nmax1 = MaxPooling2D(pool_size=(2, 2))(batchnorm1)\n# drop1 = Dropout(0.2)(max1)\nconv2 = Conv2D(16, (5, 5), activation='relu')(max1)\nbatchnorm2 = BatchNormalization()(conv2)\nflat = Flatten()(batchnorm2)\ndense1 = Dense(128, activation='relu')(flat)\noutput = Dense(num_classes, activation='softmax')(dense1)","metadata":{"execution":{"iopub.status.busy":"2023-02-16T04:53:09.487477Z","iopub.execute_input":"2023-02-16T04:53:09.487863Z","iopub.status.idle":"2023-02-16T04:53:09.553300Z","shell.execute_reply.started":"2023-02-16T04:53:09.487834Z","shell.execute_reply":"2023-02-16T04:53:09.552443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Model(inputs=inputs, outputs=output)","metadata":{"execution":{"iopub.status.busy":"2023-02-16T04:53:10.600762Z","iopub.execute_input":"2023-02-16T04:53:10.601133Z","iopub.status.idle":"2023-02-16T04:53:10.610722Z","shell.execute_reply.started":"2023-02-16T04:53:10.601102Z","shell.execute_reply":"2023-02-16T04:53:10.609437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2023-02-16T04:53:24.344735Z","iopub.execute_input":"2023-02-16T04:53:24.345087Z","iopub.status.idle":"2023-02-16T04:53:24.356223Z","shell.execute_reply.started":"2023-02-16T04:53:24.345052Z","shell.execute_reply":"2023-02-16T04:53:24.355303Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = model.fit(X_train, y_train , epochs=20, batch_size=200)","metadata":{"execution":{"iopub.status.busy":"2023-02-16T04:53:25.889614Z","iopub.execute_input":"2023-02-16T04:53:25.890592Z","iopub.status.idle":"2023-02-16T04:54:08.154996Z","shell.execute_reply.started":"2023-02-16T04:53:25.890540Z","shell.execute_reply":"2023-02-16T04:54:08.153848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Testing\n\ndf_test = np.array(df_test)\ndf_test = df_test.reshape(len(df_test),28,28,1)\n\nresult = model.predict(df_test)\n\npredicted_val=[]\nfor i in range(len(result)):\n    predicted_val.append(np.argmax(result[i]))\n    ","metadata":{"execution":{"iopub.status.busy":"2023-02-16T04:54:08.156981Z","iopub.execute_input":"2023-02-16T04:54:08.157470Z","iopub.status.idle":"2023-02-16T04:54:10.926866Z","shell.execute_reply.started":"2023-02-16T04:54:08.157433Z","shell.execute_reply":"2023-02-16T04:54:10.925842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission=pd.DataFrame()\n\nimgid=[]\nfor i in range(1, len(predicted_val)+1):\n    imgid.append(i)\n    \nsubmission[\"Imageid\"]=imgid\nsubmission[\"Label\"]=predicted_val","metadata":{"execution":{"iopub.status.busy":"2023-02-16T04:54:10.928620Z","iopub.execute_input":"2023-02-16T04:54:10.929008Z","iopub.status.idle":"2023-02-16T04:54:10.964827Z","shell.execute_reply.started":"2023-02-16T04:54:10.928971Z","shell.execute_reply":"2023-02-16T04:54:10.963959Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2023-02-16T04:54:10.966926Z","iopub.execute_input":"2023-02-16T04:54:10.967358Z","iopub.status.idle":"2023-02-16T04:54:10.999907Z","shell.execute_reply.started":"2023-02-16T04:54:10.967319Z","shell.execute_reply":"2023-02-16T04:54:10.999077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}