{"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":30646,"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","execution":{"iopub.status.busy":"2024-02-08T20:47:38.250829Z","iopub.execute_input":"2024-02-08T20:47:38.251177Z","iopub.status.idle":"2024-02-08T20:47:38.259347Z","shell.execute_reply.started":"2024-02-08T20:47:38.251152Z","shell.execute_reply":"2024-02-08T20:47:38.258320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" import matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2024-02-08T20:47:38.261513Z","iopub.execute_input":"2024-02-08T20:47:38.262137Z","iopub.status.idle":"2024-02-08T20:47:38.273526Z","shell.execute_reply.started":"2024-02-08T20:47:38.262098Z","shell.execute_reply":"2024-02-08T20:47:38.272710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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":{"execution":{"iopub.status.busy":"2024-02-08T20:47:38.276231Z","iopub.execute_input":"2024-02-08T20:47:38.276531Z","iopub.status.idle":"2024-02-08T20:47:42.330820Z","shell.execute_reply.started":"2024-02-08T20:47:38.276503Z","shell.execute_reply":"2024-02-08T20:47:42.329818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train = train[\"label\"]\nX_train = train.drop(labels= [\"label\"],axis = 1)","metadata":{"execution":{"iopub.status.busy":"2024-02-08T20:47:42.332161Z","iopub.execute_input":"2024-02-08T20:47:42.332531Z","iopub.status.idle":"2024-02-08T20:47:42.412523Z","shell.execute_reply.started":"2024-02-08T20:47:42.332496Z","shell.execute_reply":"2024-02-08T20:47:42.411726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.values.reshape(-1,28,28,1)\ntest = test.values.reshape(-1,28,28,1)\nprint(\"x_train shape\",X_train.shape)\nprint(\"test shape\",test.shape)","metadata":{"execution":{"iopub.status.busy":"2024-02-08T20:47:42.415792Z","iopub.execute_input":"2024-02-08T20:47:42.416375Z","iopub.status.idle":"2024-02-08T20:47:42.422456Z","shell.execute_reply.started":"2024-02-08T20:47:42.416346Z","shell.execute_reply":"2024-02-08T20:47:42.421459Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.utils import to_categorical ","metadata":{"execution":{"iopub.status.busy":"2024-02-08T20:47:42.423920Z","iopub.execute_input":"2024-02-08T20:47:42.424771Z","iopub.status.idle":"2024-02-08T20:47:42.433224Z","shell.execute_reply.started":"2024-02-08T20:47:42.424734Z","shell.execute_reply":"2024-02-08T20:47:42.432299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train = to_categorical(Y_train,num_classes=10)","metadata":{"execution":{"iopub.status.busy":"2024-02-08T20:47:42.434256Z","iopub.execute_input":"2024-02-08T20:47:42.434513Z","iopub.status.idle":"2024-02-08T20:47:42.444745Z","shell.execute_reply.started":"2024-02-08T20:47:42.434490Z","shell.execute_reply":"2024-02-08T20:47:42.443928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train , X_val, Y_train, Y_val = train_test_split(X_train, Y_train, test_size=0.1,random_state=2)\nprint(\"X_train shape : \",X_train.shape)\nprint(\"X_val shape : \",X_val.shape)\nprint(\"Y_train shape : \",Y_train.shape)\nprint(\"Y_val shape : \",Y_val.shape)","metadata":{"execution":{"iopub.status.busy":"2024-02-08T20:47:42.445877Z","iopub.execute_input":"2024-02-08T20:47:42.446411Z","iopub.status.idle":"2024-02-08T20:47:42.839524Z","shell.execute_reply.started":"2024-02-08T20:47:42.446378Z","shell.execute_reply":"2024-02-08T20:47:42.838612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\nimport itertools\nfrom tensorflow.keras.models import Sequential\nfrom keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPool2D, MaxPooling2D\nfrom keras.optimizers import RMSprop, Adam\nfrom keras.preprocessing.image import ImageDataGenerator\nfrom keras.callbacks import ReduceLROnPlateau","metadata":{"execution":{"iopub.status.busy":"2024-02-08T20:47:42.840786Z","iopub.execute_input":"2024-02-08T20:47:42.841078Z","iopub.status.idle":"2024-02-08T20:47:42.846325Z","shell.execute_reply.started":"2024-02-08T20:47:42.841052Z","shell.execute_reply":"2024-02-08T20:47:42.845457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\n\nmodel.add(Conv2D(filters=64, kernel_size=3, padding='same', activation='relu',input_shape=(28,28,1)))\nmodel.add(Conv2D(filters=128, kernel_size=3, padding='same', activation='relu'))\nmodel.add(MaxPooling2D(pool_size=2)) \nmodel.add(Conv2D(filters=256, kernel_size=3, padding='same', activation='relu')) \nmodel.add(MaxPooling2D(pool_size=2))\nmodel.add(Conv2D(filters=256, kernel_size=5, padding='same', activation='relu')) \nmodel.add(MaxPooling2D(pool_size=2,padding='same'))\nmodel.add(Flatten())\nmodel.add(Dense(units=512, activation='relu'))\nmodel.add(Dense(units=10, activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2024-02-08T20:47:42.847314Z","iopub.execute_input":"2024-02-08T20:47:42.847547Z","iopub.status.idle":"2024-02-08T20:47:42.951049Z","shell.execute_reply.started":"2024-02-08T20:47:42.847526Z","shell.execute_reply":"2024-02-08T20:47:42.950101Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimizer = Adam(learning_rate=0.001, beta_1=0.9, beta_2=0.999)","metadata":{"execution":{"iopub.status.busy":"2024-02-08T20:47:42.952414Z","iopub.execute_input":"2024-02-08T20:47:42.952754Z","iopub.status.idle":"2024-02-08T20:47:42.958944Z","shell.execute_reply.started":"2024-02-08T20:47:42.952719Z","shell.execute_reply":"2024-02-08T20:47:42.957999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.compile(loss='categorical_crossentropy', optimizer=optimizer, metrics=['accuracy'])\nmodel.fit( X_train, Y_train, epochs=30)","metadata":{"execution":{"iopub.status.busy":"2024-02-08T20:47:42.960055Z","iopub.execute_input":"2024-02-08T20:47:42.960404Z","iopub.status.idle":"2024-02-08T20:54:21.995845Z","shell.execute_reply.started":"2024-02-08T20:47:42.960379Z","shell.execute_reply":"2024-02-08T20:54:21.994741Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns\nY_pred = model.predict(X_val)\nY_pred_classes = np.argmax(Y_pred,axis=1)\nY_true = np.argmax(Y_val,axis = 1)\nconfusion_mtx = confusion_matrix(Y_true, Y_pred_classes)\n\nf,ax = plt.subplots(figsize=(8,8))\nsns.heatmap(confusion_mtx,annot=True, linewidths=0.01,cmap = \"inferno\",linecolor=\"gray\",fmt=\".1f\",ax=ax)\nplt.xlabel(\"Predicted Label\")\nplt.ylabel(\"True Label\")\nplt.title(\"Confusion Matrix\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-02-08T20:55:07.087988Z","iopub.execute_input":"2024-02-08T20:55:07.088713Z","iopub.status.idle":"2024-02-08T20:55:08.596318Z","shell.execute_reply.started":"2024-02-08T20:55:07.088669Z","shell.execute_reply":"2024-02-08T20:55:08.595380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions=model.predict(test)\npredicted_labels = np.argmax(predictions, axis=1)\nsubmission = pd.DataFrame({\n    \"ImageId\": range(1, len(predicted_labels)+1),\n    \"Label\": (predicted_labels)\n})\nsubmission.to_csv('submission.csv', index=False)","metadata":{},"execution_count":null,"outputs":[]}]}