{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.11","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":31041,"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)\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","trusted":true,"execution":{"iopub.status.busy":"2025-05-24T06:39:31.521046Z","iopub.execute_input":"2025-05-24T06:39:31.521718Z","iopub.status.idle":"2025-05-24T06:39:31.539359Z","shell.execute_reply.started":"2025-05-24T06:39:31.521690Z","shell.execute_reply":"2025-05-24T06:39:31.538631Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"d1=pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-24T06:39:31.540717Z","iopub.execute_input":"2025-05-24T06:39:31.541056Z","iopub.status.idle":"2025-05-24T06:39:33.593661Z","shell.execute_reply.started":"2025-05-24T06:39:31.541028Z","shell.execute_reply":"2025-05-24T06:39:33.592741Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"d1.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-24T06:39:33.594456Z","iopub.execute_input":"2025-05-24T06:39:33.594661Z","iopub.status.idle":"2025-05-24T06:39:33.606215Z","shell.execute_reply.started":"2025-05-24T06:39:33.594644Z","shell.execute_reply":"2025-05-24T06:39:33.605600Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"d1.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-24T06:39:33.607844Z","iopub.execute_input":"2025-05-24T06:39:33.608125Z","iopub.status.idle":"2025-05-24T06:39:33.644268Z","shell.execute_reply.started":"2025-05-24T06:39:33.608109Z","shell.execute_reply":"2025-05-24T06:39:33.643476Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"d1.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-24T06:39:33.645102Z","iopub.execute_input":"2025-05-24T06:39:33.645294Z","iopub.status.idle":"2025-05-24T06:39:35.109361Z","shell.execute_reply.started":"2025-05-24T06:39:33.645279Z","shell.execute_reply":"2025-05-24T06:39:35.108598Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"d1.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-24T06:39:35.110259Z","iopub.execute_input":"2025-05-24T06:39:35.110509Z","iopub.status.idle":"2025-05-24T06:39:35.147950Z","shell.execute_reply.started":"2025-05-24T06:39:35.110481Z","shell.execute_reply":"2025-05-24T06:39:35.147167Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"d1['label'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-24T06:39:35.148781Z","iopub.execute_input":"2025-05-24T06:39:35.149145Z","iopub.status.idle":"2025-05-24T06:39:35.154733Z","shell.execute_reply.started":"2025-05-24T06:39:35.149122Z","shell.execute_reply":"2025-05-24T06:39:35.154171Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"d1.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-24T06:39:35.155530Z","iopub.execute_input":"2025-05-24T06:39:35.155790Z","iopub.status.idle":"2025-05-24T06:39:35.166051Z","shell.execute_reply.started":"2025-05-24T06:39:35.155766Z","shell.execute_reply":"2025-05-24T06:39:35.165529Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"d2=pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-24T06:39:35.166674Z","iopub.execute_input":"2025-05-24T06:39:35.166937Z","iopub.status.idle":"2025-05-24T06:39:36.269514Z","shell.execute_reply.started":"2025-05-24T06:39:35.166894Z","shell.execute_reply":"2025-05-24T06:39:36.268949Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"d2.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-24T06:39:36.272214Z","iopub.execute_input":"2025-05-24T06:39:36.272644Z","iopub.status.idle":"2025-05-24T06:39:36.283474Z","shell.execute_reply.started":"2025-05-24T06:39:36.272625Z","shell.execute_reply":"2025-05-24T06:39:36.282693Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"d2.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-24T06:39:36.284196Z","iopub.execute_input":"2025-05-24T06:39:36.284380Z","iopub.status.idle":"2025-05-24T06:39:36.315822Z","shell.execute_reply.started":"2025-05-24T06:39:36.284366Z","shell.execute_reply":"2025-05-24T06:39:36.315158Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"d2.describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-24T06:39:36.316754Z","iopub.execute_input":"2025-05-24T06:39:36.316986Z","iopub.status.idle":"2025-05-24T06:39:37.569785Z","shell.execute_reply.started":"2025-05-24T06:39:36.316966Z","shell.execute_reply":"2025-05-24T06:39:37.569185Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"d2.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-24T06:39:37.570450Z","iopub.execute_input":"2025-05-24T06:39:37.570622Z","iopub.status.idle":"2025-05-24T06:39:37.597447Z","shell.execute_reply.started":"2025-05-24T06:39:37.570608Z","shell.execute_reply":"2025-05-24T06:39:37.596863Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X=d1.drop('label',axis=1).values\ny=d1['label'].values\nX_test=d2.values","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-24T06:39:37.598017Z","iopub.execute_input":"2025-05-24T06:39:37.598255Z","iopub.status.idle":"2025-05-24T06:39:37.680262Z","shell.execute_reply.started":"2025-05-24T06:39:37.598227Z","shell.execute_reply":"2025-05-24T06:39:37.679705Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-24T06:39:37.681023Z","iopub.execute_input":"2025-05-24T06:39:37.681261Z","iopub.status.idle":"2025-05-24T06:39:37.686106Z","shell.execute_reply.started":"2025-05-24T06:39:37.681243Z","shell.execute_reply":"2025-05-24T06:39:37.685365Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X=X.reshape(-1,28,28,1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-24T06:39:37.686828Z","iopub.execute_input":"2025-05-24T06:39:37.687132Z","iopub.status.idle":"2025-05-24T06:39:37.698142Z","shell.execute_reply.started":"2025-05-24T06:39:37.687107Z","shell.execute_reply":"2025-05-24T06:39:37.697422Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_test = X_test.reshape(-1, 28, 28, 1)\nX_test.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-24T06:39:37.698863Z","iopub.execute_input":"2025-05-24T06:39:37.699109Z","iopub.status.idle":"2025-05-24T06:39:37.710270Z","shell.execute_reply.started":"2025-05-24T06:39:37.699089Z","shell.execute_reply":"2025-05-24T06:39:37.709664Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X=X/255.0\nX_test=X_test/255.0","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-24T06:39:37.710842Z","iopub.execute_input":"2025-05-24T06:39:37.711094Z","iopub.status.idle":"2025-05-24T06:39:37.889470Z","shell.execute_reply.started":"2025-05-24T06:39:37.711074Z","shell.execute_reply":"2025-05-24T06:39:37.888880Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras \nfrom keras.utils import to_categorical\ny=to_categorical(y,10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-24T06:39:37.890167Z","iopub.execute_input":"2025-05-24T06:39:37.890375Z","iopub.status.idle":"2025-05-24T06:39:37.895164Z","shell.execute_reply.started":"2025-05-24T06:39:37.890359Z","shell.execute_reply":"2025-05-24T06:39:37.894476Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train,X_val,y_train,y_val=train_test_split(X,y,test_size=0.2,random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-24T06:39:37.895825Z","iopub.execute_input":"2025-05-24T06:39:37.896016Z","iopub.status.idle":"2025-05-24T06:39:38.449379Z","shell.execute_reply.started":"2025-05-24T06:39:37.896002Z","shell.execute_reply":"2025-05-24T06:39:38.448802Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\nd3=ImageDataGenerator(rotation_range=10,zoom_range=0.1,width_shift_range=0.1,height_shift_range=0.1,shear_range=0.1,fill_mode='nearest')\nd3","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-24T06:39:38.450038Z","iopub.execute_input":"2025-05-24T06:39:38.450223Z","iopub.status.idle":"2025-05-24T06:39:38.455737Z","shell.execute_reply.started":"2025-05-24T06:39:38.450209Z","shell.execute_reply":"2025-05-24T06:39:38.455101Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from keras.layers import Dense,Dropout,Conv2D,MaxPooling2D,Flatten,BatchNormalization\nfrom keras import Sequential","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-24T06:39:48.340884Z","iopub.execute_input":"2025-05-24T06:39:48.341596Z","iopub.status.idle":"2025-05-24T06:39:48.345403Z","shell.execute_reply.started":"2025-05-24T06:39:48.341568Z","shell.execute_reply":"2025-05-24T06:39:48.344561Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1), padding='same'))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(32, (3, 3), activation='relu', padding='same'))\nmodel.add(Dropout(0.25))\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Conv2D(64, (3, 3), activation='relu', padding='same'))\nmodel.add(BatchNormalization())\nmodel.add(Conv2D(64, (3, 3), activation='relu', padding='same'))\nmodel.add(MaxPooling2D((2, 2)))\nmodel.add(Dropout(0.25))\nmodel.add(Flatten())\nmodel.add(Dense(512, activation='relu'))\nmodel.add(BatchNormalization())\nmodel.add(Dropout(0.5))\nmodel.add(Dense(10, activation='softmax'))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-24T06:39:51.189122Z","iopub.execute_input":"2025-05-24T06:39:51.189381Z","iopub.status.idle":"2025-05-24T06:39:51.324957Z","shell.execute_reply.started":"2025-05-24T06:39:51.189361Z","shell.execute_reply":"2025-05-24T06:39:51.324267Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-24T06:39:53.797986Z","iopub.execute_input":"2025-05-24T06:39:53.798250Z","iopub.status.idle":"2025-05-24T06:39:53.819374Z","shell.execute_reply.started":"2025-05-24T06:39:53.798230Z","shell.execute_reply":"2025-05-24T06:39:53.818655Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from keras.utils import plot_model\nplot_model(model)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-24T06:39:56.483991Z","iopub.execute_input":"2025-05-24T06:39:56.484255Z","iopub.status.idle":"2025-05-24T06:39:56.624765Z","shell.execute_reply.started":"2025-05-24T06:39:56.484236Z","shell.execute_reply":"2025-05-24T06:39:56.624110Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-24T06:40:02.275409Z","iopub.execute_input":"2025-05-24T06:40:02.275791Z","iopub.status.idle":"2025-05-24T06:40:02.286404Z","shell.execute_reply.started":"2025-05-24T06:40:02.275759Z","shell.execute_reply":"2025-05-24T06:40:02.285549Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"h=model.fit(d3.flow(X_train,y_train,batch_size=128),epochs=30,validation_data=(X_val, y_val))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-24T06:40:05.979265Z","iopub.execute_input":"2025-05-24T06:40:05.979757Z","iopub.status.idle":"2025-05-24T06:45:06.893705Z","shell.execute_reply.started":"2025-05-24T06:40:05.979732Z","shell.execute_reply":"2025-05-24T06:45:06.893199Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.plot(h.history['accuracy'],label='Train')\nplt.plot(h.history['val_accuracy'],label='Validation')\nplt.legend()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-24T06:52:37.736615Z","iopub.execute_input":"2025-05-24T06:52:37.736897Z","iopub.status.idle":"2025-05-24T06:52:37.871315Z","shell.execute_reply.started":"2025-05-24T06:52:37.736875Z","shell.execute_reply":"2025-05-24T06:52:37.870585Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.plot(h.history['loss'],label='Train')\nplt.plot(h.history['val_loss'],label='Validation')\nplt.legend()\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-24T06:52:57.801611Z","iopub.execute_input":"2025-05-24T06:52:57.802091Z","iopub.status.idle":"2025-05-24T06:52:57.927217Z","shell.execute_reply.started":"2025-05-24T06:52:57.802066Z","shell.execute_reply":"2025-05-24T06:52:57.926627Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y1= np.argmax(model.predict(X_val), axis=1)\ny1_val = np.argmax(y_val, axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-24T06:54:24.659643Z","iopub.execute_input":"2025-05-24T06:54:24.660388Z","iopub.status.idle":"2025-05-24T06:54:26.256488Z","shell.execute_reply.started":"2025-05-24T06:54:24.660359Z","shell.execute_reply":"2025-05-24T06:54:26.255897Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix,mean_squared_error\nimport seaborn as sns\nc= confusion_matrix(y1_val, y1)\nplt.figure(figsize=(10, 8))\nsns.heatmap(c, annot=True, fmt='d', cmap='Blues')\nplt.title('Confusion Matrix on Validation Set')\nplt.xlabel('Predicted')\nplt.ylabel('True')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-24T06:55:43.440499Z","iopub.execute_input":"2025-05-24T06:55:43.440764Z","iopub.status.idle":"2025-05-24T06:55:44.498375Z","shell.execute_reply.started":"2025-05-24T06:55:43.440744Z","shell.execute_reply":"2025-05-24T06:55:44.497650Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"np.sqrt(mean_squared_error(y1_val,y1))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-24T06:56:09.255294Z","iopub.execute_input":"2025-05-24T06:56:09.256018Z","iopub.status.idle":"2025-05-24T06:56:09.261426Z","shell.execute_reply.started":"2025-05-24T06:56:09.255992Z","shell.execute_reply":"2025-05-24T06:56:09.260716Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"p= model.predict(X_test)\np1= np.argmax(p, axis=1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-24T06:56:34.933542Z","iopub.execute_input":"2025-05-24T06:56:34.934390Z","iopub.status.idle":"2025-05-24T06:56:37.305702Z","shell.execute_reply.started":"2025-05-24T06:56:34.934356Z","shell.execute_reply":"2025-05-24T06:56:37.304848Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission = pd.DataFrame({\n    'ImageId': np.arange(1, len(p1) + 1),\n    'Label': p1\n})\nsubmission.to_csv('submission.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-24T06:57:11.774354Z","iopub.execute_input":"2025-05-24T06:57:11.774620Z","iopub.status.idle":"2025-05-24T06:57:11.798223Z","shell.execute_reply.started":"2025-05-24T06:57:11.774601Z","shell.execute_reply":"2025-05-24T06:57:11.797613Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}