{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30733,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"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)\nfrom matplotlib import pyplot as plt\nimport seaborn as sns\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Normalization, Dense, Dropout, Conv2D, Flatten, MaxPooling2D, Input\nfrom sklearn.metrics import accuracy_score\nfrom sklearn.preprocessing import StandardScaler\nimport math\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-06-09T12:35:37.405390Z","iopub.execute_input":"2024-06-09T12:35:37.406117Z","iopub.status.idle":"2024-06-09T12:35:37.415150Z","shell.execute_reply.started":"2024-06-09T12:35:37.406087Z","shell.execute_reply":"2024-06-09T12:35:37.414308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_df = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-06-09T12:35:38.348302Z","iopub.execute_input":"2024-06-09T12:35:38.349069Z","iopub.status.idle":"2024-06-09T12:35:42.784660Z","shell.execute_reply.started":"2024-06-09T12:35:38.349039Z","shell.execute_reply":"2024-06-09T12:35:42.783641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-06-09T12:35:42.787481Z","iopub.execute_input":"2024-06-09T12:35:42.787843Z","iopub.status.idle":"2024-06-09T12:35:42.810139Z","shell.execute_reply.started":"2024-06-09T12:35:42.787816Z","shell.execute_reply":"2024-06-09T12:35:42.809120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = train_df['label']\ny","metadata":{"execution":{"iopub.status.busy":"2024-06-09T12:35:42.811593Z","iopub.execute_input":"2024-06-09T12:35:42.812547Z","iopub.status.idle":"2024-06-09T12:35:42.824034Z","shell.execute_reply.started":"2024-06-09T12:35:42.812492Z","shell.execute_reply":"2024-06-09T12:35:42.822938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = train_df.drop(['label'], axis=1)\nX_train.head()","metadata":{"execution":{"iopub.status.busy":"2024-06-09T12:35:46.271848Z","iopub.execute_input":"2024-06-09T12:35:46.272506Z","iopub.status.idle":"2024-06-09T12:35:46.371086Z","shell.execute_reply.started":"2024-06-09T12:35:46.272477Z","shell.execute_reply":"2024-06-09T12:35:46.370045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaler = StandardScaler()","metadata":{"execution":{"iopub.status.busy":"2024-06-09T12:35:47.276169Z","iopub.execute_input":"2024-06-09T12:35:47.277006Z","iopub.status.idle":"2024-06-09T12:35:47.280834Z","shell.execute_reply.started":"2024-06-09T12:35:47.276974Z","shell.execute_reply":"2024-06-09T12:35:47.279851Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-09T12:35:49.468154Z","iopub.execute_input":"2024-06-09T12:35:49.468540Z","iopub.status.idle":"2024-06-09T12:35:49.475741Z","shell.execute_reply.started":"2024-06-09T12:35:49.468497Z","shell.execute_reply":"2024-06-09T12:35:49.474735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-09T12:35:49.739800Z","iopub.execute_input":"2024-06-09T12:35:49.740400Z","iopub.status.idle":"2024-06-09T12:35:49.746355Z","shell.execute_reply.started":"2024-06-09T12:35:49.740369Z","shell.execute_reply":"2024-06-09T12:35:49.745275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_n = scaler.fit_transform(X_train)","metadata":{"execution":{"iopub.status.busy":"2024-06-09T12:35:49.939924Z","iopub.execute_input":"2024-06-09T12:35:49.940202Z","iopub.status.idle":"2024-06-09T12:35:50.450793Z","shell.execute_reply.started":"2024-06-09T12:35:49.940178Z","shell.execute_reply":"2024-06-09T12:35:50.449847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_t = scaler.transform(test_df)","metadata":{"execution":{"iopub.status.busy":"2024-06-09T12:35:50.452534Z","iopub.execute_input":"2024-06-09T12:35:50.452850Z","iopub.status.idle":"2024-06-09T12:35:50.578902Z","shell.execute_reply.started":"2024-06-09T12:35:50.452825Z","shell.execute_reply":"2024-06-09T12:35:50.577978Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_n.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-09T12:35:50.580212Z","iopub.execute_input":"2024-06-09T12:35:50.580943Z","iopub.status.idle":"2024-06-09T12:35:50.586895Z","shell.execute_reply.started":"2024-06-09T12:35:50.580910Z","shell.execute_reply":"2024-06-09T12:35:50.585998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_n = pd.DataFrame(X_n)\nX_t = pd.DataFrame(X_t)","metadata":{"execution":{"iopub.status.busy":"2024-06-09T12:35:50.588979Z","iopub.execute_input":"2024-06-09T12:35:50.589445Z","iopub.status.idle":"2024-06-09T12:35:50.595464Z","shell.execute_reply.started":"2024-06-09T12:35:50.589410Z","shell.execute_reply":"2024-06-09T12:35:50.594520Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ratio = int(math.sqrt(X_n.shape[1]))\n\nimage_first=X_n.iloc[11]\nimage_first=np.array(image_first).reshape(ratio,ratio)\n\nplt.imshow(image_first,cmap=plt.cm.binary_r)","metadata":{"execution":{"iopub.status.busy":"2024-06-09T12:35:51.848189Z","iopub.execute_input":"2024-06-09T12:35:51.849019Z","iopub.status.idle":"2024-06-09T12:35:52.144728Z","shell.execute_reply.started":"2024-06-09T12:35:51.848987Z","shell.execute_reply":"2024-06-09T12:35:52.143644Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_n.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-09T12:35:53.164427Z","iopub.execute_input":"2024-06-09T12:35:53.165377Z","iopub.status.idle":"2024-06-09T12:35:53.171361Z","shell.execute_reply.started":"2024-06-09T12:35:53.165345Z","shell.execute_reply":"2024-06-09T12:35:53.170282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"IMG_SIZE = 28\nx_ntrain = np.array(X_n).reshape(-1,IMG_SIZE,IMG_SIZE,1)\nx_ntest = np.array(X_t).reshape(-1,IMG_SIZE,IMG_SIZE,1)\nx_ntrain.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-09T12:35:53.467856Z","iopub.execute_input":"2024-06-09T12:35:53.468187Z","iopub.status.idle":"2024-06-09T12:35:53.644933Z","shell.execute_reply.started":"2024-06-09T12:35:53.468162Z","shell.execute_reply":"2024-06-09T12:35:53.643453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()","metadata":{"execution":{"iopub.status.busy":"2024-06-09T12:36:00.071987Z","iopub.execute_input":"2024-06-09T12:36:00.072893Z","iopub.status.idle":"2024-06-09T12:36:00.078384Z","shell.execute_reply.started":"2024-06-09T12:36:00.072861Z","shell.execute_reply":"2024-06-09T12:36:00.077423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(Conv2D(filters = 64, kernel_size = (3,3), activation ='relu', input_shape=x_ntrain.shape[1:]))","metadata":{"execution":{"iopub.status.busy":"2024-06-09T12:36:00.532146Z","iopub.execute_input":"2024-06-09T12:36:00.532879Z","iopub.status.idle":"2024-06-09T12:36:00.548615Z","shell.execute_reply.started":"2024-06-09T12:36:00.532846Z","shell.execute_reply":"2024-06-09T12:36:00.547738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(MaxPooling2D(pool_size=(2,2)))","metadata":{"execution":{"iopub.status.busy":"2024-06-09T12:36:01.380500Z","iopub.execute_input":"2024-06-09T12:36:01.380907Z","iopub.status.idle":"2024-06-09T12:36:01.390343Z","shell.execute_reply.started":"2024-06-09T12:36:01.380879Z","shell.execute_reply":"2024-06-09T12:36:01.389293Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(Conv2D(64,(3,3),activation='relu'))\nmodel.add(MaxPooling2D(pool_size=(2,2)))","metadata":{"execution":{"iopub.status.busy":"2024-06-09T12:36:01.887950Z","iopub.execute_input":"2024-06-09T12:36:01.888378Z","iopub.status.idle":"2024-06-09T12:36:01.911280Z","shell.execute_reply.started":"2024-06-09T12:36:01.888349Z","shell.execute_reply":"2024-06-09T12:36:01.910489Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(Flatten())","metadata":{"execution":{"iopub.status.busy":"2024-06-09T12:38:52.645435Z","iopub.execute_input":"2024-06-09T12:38:52.646574Z","iopub.status.idle":"2024-06-09T12:38:52.656759Z","shell.execute_reply.started":"2024-06-09T12:38:52.646511Z","shell.execute_reply":"2024-06-09T12:38:52.655840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(Dense(64, activation='relu'))\nmodel.add(Dense(32, activation='relu'))","metadata":{"execution":{"iopub.status.busy":"2024-06-09T12:39:27.172490Z","iopub.execute_input":"2024-06-09T12:39:27.172944Z","iopub.status.idle":"2024-06-09T12:39:27.207550Z","shell.execute_reply.started":"2024-06-09T12:39:27.172915Z","shell.execute_reply":"2024-06-09T12:39:27.206587Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.add(Dense(10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2024-06-09T12:39:49.981446Z","iopub.execute_input":"2024-06-09T12:39:49.981837Z","iopub.status.idle":"2024-06-09T12:39:50.004062Z","shell.execute_reply.started":"2024-06-09T12:39:49.981811Z","shell.execute_reply":"2024-06-09T12:39:50.003244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-06-09T12:39:54.977991Z","iopub.execute_input":"2024-06-09T12:39:54.978356Z","iopub.status.idle":"2024-06-09T12:39:55.004961Z","shell.execute_reply.started":"2024-06-09T12:39:54.978330Z","shell.execute_reply":"2024-06-09T12:39:55.002511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-06-09T12:41:20.281220Z","iopub.execute_input":"2024-06-09T12:41:20.282068Z","iopub.status.idle":"2024-06-09T12:41:20.292425Z","shell.execute_reply.started":"2024-06-09T12:41:20.282032Z","shell.execute_reply":"2024-06-09T12:41:20.291597Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-06-09T12:41:25.834879Z","iopub.execute_input":"2024-06-09T12:41:25.835319Z","iopub.status.idle":"2024-06-09T12:41:25.863060Z","shell.execute_reply.started":"2024-06-09T12:41:25.835285Z","shell.execute_reply":"2024-06-09T12:41:25.862214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y","metadata":{"execution":{"iopub.status.busy":"2024-06-09T12:42:08.567963Z","iopub.execute_input":"2024-06-09T12:42:08.568819Z","iopub.status.idle":"2024-06-09T12:42:08.576234Z","shell.execute_reply.started":"2024-06-09T12:42:08.568787Z","shell.execute_reply":"2024-06-09T12:42:08.575153Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(x_ntrain, y, epochs=10)","metadata":{"execution":{"iopub.status.busy":"2024-06-09T12:42:33.152254Z","iopub.execute_input":"2024-06-09T12:42:33.153034Z","iopub.status.idle":"2024-06-09T12:43:10.679146Z","shell.execute_reply.started":"2024-06-09T12:42:33.153002Z","shell.execute_reply":"2024-06-09T12:43:10.678286Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(x_ntest)\ny_pred","metadata":{"execution":{"iopub.status.busy":"2024-06-09T12:45:26.141659Z","iopub.execute_input":"2024-06-09T12:45:26.142028Z","iopub.status.idle":"2024-06-09T12:45:28.506186Z","shell.execute_reply.started":"2024-06-09T12:45:26.142003Z","shell.execute_reply":"2024-06-09T12:45:28.505118Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def pred(x):\n    return np.argmax(x)\n\nnew_y_pred = np.zeros(y_pred.shape[0])","metadata":{"execution":{"iopub.status.busy":"2024-06-09T13:01:35.465178Z","iopub.execute_input":"2024-06-09T13:01:35.465655Z","iopub.status.idle":"2024-06-09T13:01:35.471021Z","shell.execute_reply.started":"2024-06-09T13:01:35.465623Z","shell.execute_reply":"2024-06-09T13:01:35.469986Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(y_pred.shape[0]):\n    new_y_pred[i] = pred(y_pred[i])\nnew_y_pred","metadata":{"execution":{"iopub.status.busy":"2024-06-09T13:03:09.214588Z","iopub.execute_input":"2024-06-09T13:03:09.215630Z","iopub.status.idle":"2024-06-09T13:03:09.350275Z","shell.execute_reply.started":"2024-06-09T13:03:09.215591Z","shell.execute_reply":"2024-06-09T13:03:09.349244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\nsubmit","metadata":{"execution":{"iopub.status.busy":"2024-06-09T13:03:11.554263Z","iopub.execute_input":"2024-06-09T13:03:11.554953Z","iopub.status.idle":"2024-06-09T13:03:11.574516Z","shell.execute_reply.started":"2024-06-09T13:03:11.554919Z","shell.execute_reply":"2024-06-09T13:03:11.573569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit['Label'] = new_y_pred.astype(int)\nsubmit","metadata":{"execution":{"iopub.status.busy":"2024-06-09T13:03:26.098242Z","iopub.execute_input":"2024-06-09T13:03:26.098637Z","iopub.status.idle":"2024-06-09T13:03:26.110988Z","shell.execute_reply.started":"2024-06-09T13:03:26.098606Z","shell.execute_reply":"2024-06-09T13:03:26.110015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submit.to_csv('/kaggle/working/submission_1.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-06-09T13:06:49.816877Z","iopub.execute_input":"2024-06-09T13:06:49.817707Z","iopub.status.idle":"2024-06-09T13:06:49.858286Z","shell.execute_reply.started":"2024-06-09T13:06:49.817674Z","shell.execute_reply":"2024-06-09T13:06:49.857569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}