{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.14","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":30786,"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-11-17T21:15:52.154192Z","iopub.execute_input":"2024-11-17T21:15:52.154655Z","iopub.status.idle":"2024-11-17T21:15:52.163439Z","shell.execute_reply.started":"2024-11-17T21:15:52.154615Z","shell.execute_reply":"2024-11-17T21:15:52.162288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\ntrain_data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\nprint(train_data.shape)\ntrain_data.head()\n","metadata":{"execution":{"iopub.status.busy":"2024-11-17T21:15:55.022258Z","iopub.execute_input":"2024-11-17T21:15:55.022730Z","iopub.status.idle":"2024-11-17T21:15:57.547360Z","shell.execute_reply.started":"2024-11-17T21:15:55.022684Z","shell.execute_reply":"2024-11-17T21:15:57.546170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\nprint(test_data.shape)\ntest_data.head()","metadata":{"execution":{"iopub.status.busy":"2024-11-17T21:16:03.111714Z","iopub.execute_input":"2024-11-17T21:16:03.112126Z","iopub.status.idle":"2024-11-17T21:16:04.691860Z","shell.execute_reply.started":"2024-11-17T21:16:03.112081Z","shell.execute_reply":"2024-11-17T21:16:04.690771Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = (train_data.iloc[:,1:].values)\nX_train","metadata":{"execution":{"iopub.status.busy":"2024-11-17T21:16:44.092823Z","iopub.execute_input":"2024-11-17T21:16:44.093853Z","iopub.status.idle":"2024-11-17T21:16:44.101888Z","shell.execute_reply.started":"2024-11-17T21:16:44.093801Z","shell.execute_reply":"2024-11-17T21:16:44.100693Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = train_data.iloc[:,0].values","metadata":{"execution":{"iopub.status.busy":"2024-11-17T21:16:47.369269Z","iopub.execute_input":"2024-11-17T21:16:47.369709Z","iopub.status.idle":"2024-11-17T21:16:47.375410Z","shell.execute_reply.started":"2024-11-17T21:16:47.369668Z","shell.execute_reply":"2024-11-17T21:16:47.374227Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train","metadata":{"execution":{"iopub.status.busy":"2024-11-17T21:16:50.431591Z","iopub.execute_input":"2024-11-17T21:16:50.432371Z","iopub.status.idle":"2024-11-17T21:16:50.439290Z","shell.execute_reply.started":"2024-11-17T21:16:50.432327Z","shell.execute_reply":"2024-11-17T21:16:50.438135Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = test_data.values\nX_test","metadata":{"execution":{"iopub.status.busy":"2024-11-17T21:02:57.288220Z","iopub.execute_input":"2024-11-17T21:02:57.288692Z","iopub.status.idle":"2024-11-17T21:02:57.296779Z","shell.execute_reply.started":"2024-11-17T21:02:57.288652Z","shell.execute_reply":"2024-11-17T21:02:57.295506Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Convert train datset to (num_images, img_rows, img_cols) format \nX_train = X_train.reshape(X_train.shape[0], 28, 28)\nprint(X_train.shape)\n\nimport matplotlib.pyplot as plt\nfor i in range(6, 9):\n    plt.subplot(330 + (i+1))\n    plt.imshow(X_train[i], cmap=plt.get_cmap('gray'))\n    plt.title(y_train[i]);","metadata":{"execution":{"iopub.status.busy":"2024-11-17T21:17:24.967255Z","iopub.execute_input":"2024-11-17T21:17:24.968379Z","iopub.status.idle":"2024-11-17T21:17:25.414655Z","shell.execute_reply.started":"2024-11-17T21:17:24.968331Z","shell.execute_reply":"2024-11-17T21:17:25.413462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#expand 1 more dimention as 1 for colour channel gray\nX_train = X_train.reshape(X_train.shape[0], 28, 28,1)\nX_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-11-17T21:17:56.094575Z","iopub.execute_input":"2024-11-17T21:17:56.095006Z","iopub.status.idle":"2024-11-17T21:17:56.103007Z","shell.execute_reply.started":"2024-11-17T21:17:56.094966Z","shell.execute_reply":"2024-11-17T21:17:56.101715Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test = X_test.reshape(X_test.shape[0], 28, 28,1)\nX_test.shape","metadata":{"execution":{"iopub.status.busy":"2024-11-17T21:17:59.577733Z","iopub.execute_input":"2024-11-17T21:17:59.578151Z","iopub.status.idle":"2024-11-17T21:17:59.585788Z","shell.execute_reply.started":"2024-11-17T21:17:59.578113Z","shell.execute_reply":"2024-11-17T21:17:59.584564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"mean_px = X_train.mean().astype(np.float32)\nstd_px = X_train.std().astype(np.float32)\n\ndef standardize(x): \n    return (x-mean_px)/std_px","metadata":{"execution":{"iopub.status.busy":"2024-11-17T21:18:23.319051Z","iopub.execute_input":"2024-11-17T21:18:23.319796Z","iopub.status.idle":"2024-11-17T21:18:23.600837Z","shell.execute_reply.started":"2024-11-17T21:18:23.319752Z","shell.execute_reply":"2024-11-17T21:18:23.599581Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils import to_categorical\ny_train= to_categorical(y_train)\nnum_classes = y_train.shape[1]\nnum_classes","metadata":{"execution":{"iopub.status.busy":"2024-11-17T21:18:43.735562Z","iopub.execute_input":"2024-11-17T21:18:43.736591Z","iopub.status.idle":"2024-11-17T21:18:43.747384Z","shell.execute_reply.started":"2024-11-17T21:18:43.736540Z","shell.execute_reply":"2024-11-17T21:18:43.746284Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Designing Neural Architecture","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Lambda, Dense, Flatten\n\nmodel = Sequential()","metadata":{"execution":{"iopub.status.busy":"2024-11-17T21:19:02.735088Z","iopub.execute_input":"2024-11-17T21:19:02.735553Z","iopub.status.idle":"2024-11-17T21:19:02.742555Z","shell.execute_reply.started":"2024-11-17T21:19:02.735510Z","shell.execute_reply":"2024-11-17T21:19:02.741068Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(Lambda(standardize,input_shape=(28,28,1)))\nmodel.add(Flatten())\nmodel.add(Dense(10,activation='softmax'))\nprint(\"input shape \",model.input_shape)\nprint(\"output shape \",model.output_shape)","metadata":{"execution":{"iopub.status.busy":"2024-11-17T21:19:35.930468Z","iopub.execute_input":"2024-11-17T21:19:35.931435Z","iopub.status.idle":"2024-11-17T21:19:35.988872Z","shell.execute_reply.started":"2024-11-17T21:19:35.931392Z","shell.execute_reply":"2024-11-17T21:19:35.987713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-11-17T21:19:45.758851Z","iopub.execute_input":"2024-11-17T21:19:45.759276Z","iopub.status.idle":"2024-11-17T21:19:45.785331Z","shell.execute_reply.started":"2024-11-17T21:19:45.759219Z","shell.execute_reply":"2024-11-17T21:19:45.784266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy',optimizer='Adam')","metadata":{"execution":{"iopub.status.busy":"2024-11-17T21:20:20.926462Z","iopub.execute_input":"2024-11-17T21:20:20.927551Z","iopub.status.idle":"2024-11-17T21:20:20.938464Z","shell.execute_reply.started":"2024-11-17T21:20:20.927501Z","shell.execute_reply":"2024-11-17T21:20:20.937106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X_train,y_train,epochs=50,validation_split=0.2)","metadata":{"execution":{"iopub.status.busy":"2024-11-17T21:20:23.409906Z","iopub.execute_input":"2024-11-17T21:20:23.410770Z","iopub.status.idle":"2024-11-17T21:22:56.149339Z","shell.execute_reply.started":"2024-11-17T21:20:23.410722Z","shell.execute_reply":"2024-11-17T21:22:56.147929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_prob = model.predict(X_test,verbose=0)","metadata":{"execution":{"iopub.status.busy":"2024-11-17T21:28:44.428632Z","iopub.execute_input":"2024-11-17T21:28:44.429098Z","iopub.status.idle":"2024-11-17T21:28:46.172848Z","shell.execute_reply.started":"2024-11-17T21:28:44.429061Z","shell.execute_reply":"2024-11-17T21:28:46.171492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_prob","metadata":{"execution":{"iopub.status.busy":"2024-11-17T21:28:48.854610Z","iopub.execute_input":"2024-11-17T21:28:48.855569Z","iopub.status.idle":"2024-11-17T21:28:48.862828Z","shell.execute_reply.started":"2024-11-17T21:28:48.855523Z","shell.execute_reply":"2024-11-17T21:28:48.861788Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = y_prob.argmax(axis=1)\ny_pred","metadata":{"execution":{"iopub.status.busy":"2024-11-17T21:30:51.529940Z","iopub.execute_input":"2024-11-17T21:30:51.530380Z","iopub.status.idle":"2024-11-17T21:30:51.538725Z","shell.execute_reply.started":"2024-11-17T21:30:51.530338Z","shell.execute_reply":"2024-11-17T21:30:51.537647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submissions=pd.DataFrame({\"ImageId\": list(range(1,len(y_pred)+1)),\n                         \"Label\": y_pred})\nsubmissions.to_csv(\"DR.csv\", index=False, header=True)","metadata":{"execution":{"iopub.status.busy":"2024-11-17T21:31:01.245224Z","iopub.execute_input":"2024-11-17T21:31:01.245679Z","iopub.status.idle":"2024-11-17T21:31:01.288105Z","shell.execute_reply.started":"2024-11-17T21:31:01.245638Z","shell.execute_reply":"2024-11-17T21:31:01.286887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}