{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"gpu","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30919,"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-11T11:16:31.945055Z","iopub.execute_input":"2025-05-11T11:16:31.945337Z","iopub.status.idle":"2025-05-11T11:16:32.269588Z","shell.execute_reply.started":"2025-05-11T11:16:31.945305Z","shell.execute_reply":"2025-05-11T11:16:32.268693Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 1-Import Necessary Libraries","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport tensorflow\nfrom tensorflow import keras \nfrom tensorflow.keras import layers\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport matplotlib.image as mpimg\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.metrics import confusion_matrix\nimport itertools\nfrom tensorflow.keras.utils import to_categorical\nfrom keras.optimizers import RMSprop\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ReduceLROnPlateau\n\n\nsns.set(style='white', context='notebook', palette='deep')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T11:16:32.270557Z","iopub.execute_input":"2025-05-11T11:16:32.270915Z","iopub.status.idle":"2025-05-11T11:16:45.050839Z","shell.execute_reply.started":"2025-05-11T11:16:32.270883Z","shell.execute_reply":"2025-05-11T11:16:45.049902Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest=pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T11:16:45.051711Z","iopub.execute_input":"2025-05-11T11:16:45.052463Z","iopub.status.idle":"2025-05-11T11:16:49.245124Z","shell.execute_reply.started":"2025-05-11T11:16:45.052438Z","shell.execute_reply":"2025-05-11T11:16:49.244458Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T11:16:49.247012Z","iopub.execute_input":"2025-05-11T11:16:49.247276Z","iopub.status.idle":"2025-05-11T11:16:49.279137Z","shell.execute_reply.started":"2025-05-11T11:16:49.247254Z","shell.execute_reply":"2025-05-11T11:16:49.278311Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train=train.drop('label',axis=1)\ny_train=train['label']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T11:16:49.280363Z","iopub.execute_input":"2025-05-11T11:16:49.280596Z","iopub.status.idle":"2025-05-11T11:16:49.391201Z","shell.execute_reply.started":"2025-05-11T11:16:49.280576Z","shell.execute_reply":"2025-05-11T11:16:49.390555Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_train.value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T11:16:49.392044Z","iopub.execute_input":"2025-05-11T11:16:49.392295Z","iopub.status.idle":"2025-05-11T11:16:49.403541Z","shell.execute_reply.started":"2025-05-11T11:16:49.392273Z","shell.execute_reply":"2025-05-11T11:16:49.402748Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"g=sns.countplot(y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T11:16:49.404260Z","iopub.execute_input":"2025-05-11T11:16:49.404493Z","iopub.status.idle":"2025-05-11T11:16:49.615767Z","shell.execute_reply.started":"2025-05-11T11:16:49.404472Z","shell.execute_reply":"2025-05-11T11:16:49.614908Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train.isnull().any().describe()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T11:16:49.616689Z","iopub.execute_input":"2025-05-11T11:16:49.617003Z","iopub.status.idle":"2025-05-11T11:16:49.633683Z","shell.execute_reply.started":"2025-05-11T11:16:49.616971Z","shell.execute_reply":"2025-05-11T11:16:49.632782Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 2-Normalize","metadata":{}},{"cell_type":"code","source":"X_train=X_train/255.0\ntest=test/255.0","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T11:16:49.634545Z","iopub.execute_input":"2025-05-11T11:16:49.634822Z","iopub.status.idle":"2025-05-11T11:16:49.733520Z","shell.execute_reply.started":"2025-05-11T11:16:49.634789Z","shell.execute_reply":"2025-05-11T11:16:49.732590Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#y_train = to_categorical(y_train, num_classes = 10)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T11:16:49.734416Z","iopub.execute_input":"2025-05-11T11:16:49.734671Z","iopub.status.idle":"2025-05-11T11:16:49.738350Z","shell.execute_reply.started":"2025-05-11T11:16:49.734650Z","shell.execute_reply":"2025-05-11T11:16:49.737436Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 2-2-Reshape","metadata":{}},{"cell_type":"code","source":"#one channel because it's on gray scale\nX_train=X_train.values.reshape(-1,28,28,1)\ntest=test.values.reshape(-1,28,28,1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T11:16:49.739279Z","iopub.execute_input":"2025-05-11T11:16:49.739518Z","iopub.status.idle":"2025-05-11T11:16:49.752969Z","shell.execute_reply.started":"2025-05-11T11:16:49.739498Z","shell.execute_reply":"2025-05-11T11:16:49.752216Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train,X_val,y_train,y_val=train_test_split(X_train,y_train,stratify=y_train,random_state=32)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T11:16:49.753708Z","iopub.execute_input":"2025-05-11T11:16:49.753934Z","iopub.status.idle":"2025-05-11T11:16:50.165189Z","shell.execute_reply.started":"2025-05-11T11:16:49.753915Z","shell.execute_reply":"2025-05-11T11:16:50.164275Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"g=plt.imshow(X_train[0],cmap=plt.cm.binary)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T11:16:50.168171Z","iopub.execute_input":"2025-05-11T11:16:50.168467Z","iopub.status.idle":"2025-05-11T11:16:50.397327Z","shell.execute_reply.started":"2025-05-11T11:16:50.168442Z","shell.execute_reply":"2025-05-11T11:16:50.396567Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# 3-Model Building(CNN)","metadata":{}},{"cell_type":"code","source":"model=keras.Sequential([\n    layers.Conv2D(filters=32,kernel_size=(5,5),padding='Same',activation='relu',input_shape=(28,28,1)),\n    #layers.Conv2D(filters=32,kernel_size=(5,5),padding='Same',activation='relu'),\n    layers.MaxPool2D(pool_size=(2,2)),\n    layers.Dropout(0.25),\n\n\n    layers.Conv2D(filters=64,kernel_size=(5,5),padding='Same',activation='relu'),\n    layers.Conv2D(filters=64,kernel_size=(5,5),padding='Same',activation='relu'),\n    layers.MaxPool2D(pool_size=(2,2),strides=(2,2)),\n    layers.Dropout(0.25),\n\n    layers.Flatten(),\n    layers.Dense(256,activation='relu'),\n    layers.Dropout(0.5),\n    layers.Dense(10,activation='softmax')\n])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T11:16:50.398699Z","iopub.execute_input":"2025-05-11T11:16:50.399013Z","iopub.status.idle":"2025-05-11T11:16:52.520788Z","shell.execute_reply.started":"2025-05-11T11:16:50.398989Z","shell.execute_reply":"2025-05-11T11:16:52.520074Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T11:16:52.521711Z","iopub.execute_input":"2025-05-11T11:16:52.522007Z","iopub.status.idle":"2025-05-11T11:16:52.533215Z","shell.execute_reply.started":"2025-05-11T11:16:52.521975Z","shell.execute_reply":"2025-05-11T11:16:52.532595Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = model.fit(X_train, y_train, batch_size = 128, epochs = 4, \n          validation_data = (X_val, y_val), verbose = 2)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T11:16:52.533994Z","iopub.execute_input":"2025-05-11T11:16:52.534286Z","iopub.status.idle":"2025-05-11T11:17:08.488367Z","shell.execute_reply.started":"2025-05-11T11:16:52.534254Z","shell.execute_reply":"2025-05-11T11:17:08.487699Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#datagen.fit(X_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T11:17:08.489347Z","iopub.execute_input":"2025-05-11T11:17:08.489600Z","iopub.status.idle":"2025-05-11T11:17:08.492877Z","shell.execute_reply.started":"2025-05-11T11:17:08.489579Z","shell.execute_reply":"2025-05-11T11:17:08.491900Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"#history = model.fit(X_train,y_train,batch_size=128,\n                             # epochs = 4, validation_data = (X_val,y_val),\n                              #verbose = 2, steps_per_epoch=X_train.shape[0] // batch_size )","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T11:17:08.493648Z","iopub.execute_input":"2025-05-11T11:17:08.493926Z","iopub.status.idle":"2025-05-11T11:17:08.511069Z","shell.execute_reply.started":"2025-05-11T11:17:08.493903Z","shell.execute_reply":"2025-05-11T11:17:08.510242Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions=model.predict(test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T11:17:08.511821Z","iopub.execute_input":"2025-05-11T11:17:08.512019Z","iopub.status.idle":"2025-05-11T11:17:10.767348Z","shell.execute_reply.started":"2025-05-11T11:17:08.512001Z","shell.execute_reply":"2025-05-11T11:17:10.766435Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predictions","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T11:17:10.768493Z","iopub.execute_input":"2025-05-11T11:17:10.768796Z","iopub.status.idle":"2025-05-11T11:17:10.775217Z","shell.execute_reply.started":"2025-05-11T11:17:10.768774Z","shell.execute_reply":"2025-05-11T11:17:10.774351Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T11:17:10.776199Z","iopub.execute_input":"2025-05-11T11:17:10.776472Z","iopub.status.idle":"2025-05-11T11:17:10.837627Z","shell.execute_reply.started":"2025-05-11T11:17:10.776438Z","shell.execute_reply":"2025-05-11T11:17:10.836846Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"output=pd.DataFrame({'ImageId':range(1,28001),'Label':np.argmax(predictions,axis=1)})","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T11:17:10.838328Z","iopub.execute_input":"2025-05-11T11:17:10.838626Z","iopub.status.idle":"2025-05-11T11:17:10.852519Z","shell.execute_reply.started":"2025-05-11T11:17:10.838603Z","shell.execute_reply":"2025-05-11T11:17:10.851898Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"output","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T11:17:10.853224Z","iopub.execute_input":"2025-05-11T11:17:10.853448Z","iopub.status.idle":"2025-05-11T11:17:10.870724Z","shell.execute_reply.started":"2025-05-11T11:17:10.853427Z","shell.execute_reply":"2025-05-11T11:17:10.869977Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"submission=output.to_csv('submission.csv',index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-05-11T11:17:10.871583Z","iopub.execute_input":"2025-05-11T11:17:10.871879Z","iopub.status.idle":"2025-05-11T11:17:10.906187Z","shell.execute_reply.started":"2025-05-11T11:17:10.871847Z","shell.execute_reply":"2025-05-11T11:17:10.905305Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}