{"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":"gpu","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30648,"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)\nimport keras\nimport tensorflow as tf\n\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-11T12:26:21.868306Z","iopub.execute_input":"2024-02-11T12:26:21.868697Z","iopub.status.idle":"2024-02-11T12:26:34.465021Z","shell.execute_reply.started":"2024-02-11T12:26:21.868668Z","shell.execute_reply":"2024-02-11T12:26:34.464096Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Loading Data","metadata":{}},{"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-11T12:41:44.824095Z","iopub.execute_input":"2024-02-11T12:41:44.824707Z","iopub.status.idle":"2024-02-11T12:41:49.306365Z","shell.execute_reply.started":"2024-02-11T12:41:44.824676Z","shell.execute_reply":"2024-02-11T12:41:49.305567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-11T12:26:39.812332Z","iopub.execute_input":"2024-02-11T12:26:39.812629Z","iopub.status.idle":"2024-02-11T12:26:39.836854Z","shell.execute_reply.started":"2024-02-11T12:26:39.812603Z","shell.execute_reply":"2024-02-11T12:26:39.835790Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-11T12:26:39.837855Z","iopub.execute_input":"2024-02-11T12:26:39.838085Z","iopub.status.idle":"2024-02-11T12:26:39.931538Z","shell.execute_reply.started":"2024-02-11T12:26:39.838064Z","shell.execute_reply":"2024-02-11T12:26:39.930575Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"markdown","source":"# EDA","metadata":{}},{"cell_type":"code","source":"from keras.datasets import mnist\n(X_train,y_train) ,(X_test,y_test) = mnist.load_data()","metadata":{"execution":{"iopub.status.busy":"2024-02-11T12:26:39.934473Z","iopub.execute_input":"2024-02-11T12:26:39.935072Z","iopub.status.idle":"2024-02-11T12:26:42.132985Z","shell.execute_reply.started":"2024-02-11T12:26:39.935046Z","shell.execute_reply":"2024-02-11T12:26:42.132169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\n#no of digits = dispaly \nn=10\nplt.figure(figsize=(20,4))\nfor i in range(n):\n    ax = plt.subplot(2,n,i+1)\n    plt.imshow(X_test[i].reshape(28,28))\n    plt.gray()\n    ax.get_xaxis().set_visible(False)\n    ax.get_yaxis().set_visible(False)\n\nplt.show()\nplt.close()\n    ","metadata":{"execution":{"iopub.status.busy":"2024-02-11T12:26:42.134024Z","iopub.execute_input":"2024-02-11T12:26:42.134312Z","iopub.status.idle":"2024-02-11T12:26:42.641530Z","shell.execute_reply.started":"2024-02-11T12:26:42.134287Z","shell.execute_reply":"2024-02-11T12:26:42.640494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"x_train Shape :\", X_train.shape)\nprint(\"y_train Shape :\", y_train.shape)\nprint(\"x_test Shape :\", X_test.shape)\nprint(\"y_test Shape :\", y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2024-02-11T12:26:42.642643Z","iopub.execute_input":"2024-02-11T12:26:42.642981Z","iopub.status.idle":"2024-02-11T12:26:42.648454Z","shell.execute_reply.started":"2024-02-11T12:26:42.642953Z","shell.execute_reply":"2024-02-11T12:26:42.647561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reshaping the data to make it compatible with tensorflow\nX_train = X_train.reshape(X_train.shape[0],28,28,1)\nX_test = X_test.reshape(X_test.shape[0],28,28,1)","metadata":{"execution":{"iopub.status.busy":"2024-02-11T12:26:42.649543Z","iopub.execute_input":"2024-02-11T12:26:42.649845Z","iopub.status.idle":"2024-02-11T12:26:42.661630Z","shell.execute_reply.started":"2024-02-11T12:26:42.649821Z","shell.execute_reply":"2024-02-11T12:26:42.660880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Normalizing the Data using Min Max Scaling","metadata":{}},{"cell_type":"code","source":"#Since the maximum value of pixel is 255, so we divide each column with 255\nX_train = X_train.astype(\"float32\")\nX_test = X_test.astype(\"float32\")\nX_train = X_train/255\nX_test = X_test/255","metadata":{"execution":{"iopub.status.busy":"2024-02-11T12:26:42.662784Z","iopub.execute_input":"2024-02-11T12:26:42.663237Z","iopub.status.idle":"2024-02-11T12:26:42.781956Z","shell.execute_reply.started":"2024-02-11T12:26:42.663206Z","shell.execute_reply":"2024-02-11T12:26:42.781110Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Dealing with Categorical Values","metadata":{}},{"cell_type":"code","source":"from keras.utils import to_categorical\ny_train = to_categorical(y_train, num_classes=10)\ny_test = to_categorical(y_test, num_classes=10)\n","metadata":{"execution":{"iopub.status.busy":"2024-02-11T12:26:42.783323Z","iopub.execute_input":"2024-02-11T12:26:42.783654Z","iopub.status.idle":"2024-02-11T12:26:42.789679Z","shell.execute_reply.started":"2024-02-11T12:26:42.783624Z","shell.execute_reply":"2024-02-11T12:26:42.788767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"x_train Shape :\", X_train.shape)\nprint(\"y_train Shape :\", y_train.shape)\nprint(\"x_test Shape :\", X_test.shape)\nprint(\"y_test Shape :\", y_test.shape)\n","metadata":{"execution":{"iopub.status.busy":"2024-02-11T12:26:42.791058Z","iopub.execute_input":"2024-02-11T12:26:42.791443Z","iopub.status.idle":"2024-02-11T12:26:42.800372Z","shell.execute_reply.started":"2024-02-11T12:26:42.791408Z","shell.execute_reply":"2024-02-11T12:26:42.799519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Modeling","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras import models, layers \nfrom tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Flatten","metadata":{"execution":{"iopub.status.busy":"2024-02-11T12:26:42.801444Z","iopub.execute_input":"2024-02-11T12:26:42.801735Z","iopub.status.idle":"2024-02-11T12:26:42.905442Z","shell.execute_reply.started":"2024-02-11T12:26:42.801711Z","shell.execute_reply":"2024-02-11T12:26:42.904780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"img_rows, img_cols, channels = 28, 28, 1 \nfilters = [6, 32, 80 ,120] \nclasses = 10","metadata":{"execution":{"iopub.status.busy":"2024-02-11T12:26:42.906430Z","iopub.execute_input":"2024-02-11T12:26:42.906787Z","iopub.status.idle":"2024-02-11T12:26:42.911953Z","shell.execute_reply.started":"2024-02-11T12:26:42.906752Z","shell.execute_reply":"2024-02-11T12:26:42.910859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = tf.keras.Sequential([\n    Conv2D(filters = 64, kernel_size=(5,5),strides=(1,1),padding='same',activation='relu' ),\n    MaxPooling2D(pool_size=(2,2), strides=(2,2)),\n    Conv2D(filters = 32, kernel_size=(5,5),strides=(1,1),activation='relu'),\n    MaxPooling2D(pool_size=(2,2), strides=(2,2)),\n    Flatten(),\n    Dense(10, activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2024-02-11T12:26:42.915008Z","iopub.execute_input":"2024-02-11T12:26:42.915314Z","iopub.status.idle":"2024-02-11T12:26:43.542254Z","shell.execute_reply.started":"2024-02-11T12:26:42.915291Z","shell.execute_reply":"2024-02-11T12:26:43.541462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-02-11T12:26:43.543244Z","iopub.execute_input":"2024-02-11T12:26:43.543523Z","iopub.status.idle":"2024-02-11T12:26:43.569928Z","shell.execute_reply.started":"2024-02-11T12:26:43.543499Z","shell.execute_reply":"2024-02-11T12:26:43.569082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X_train,y_train,\n         validation_split=0.2,\n         epochs=15,\n         batch_size=64)","metadata":{"execution":{"iopub.status.busy":"2024-02-11T12:26:43.570988Z","iopub.execute_input":"2024-02-11T12:26:43.571242Z","iopub.status.idle":"2024-02-11T12:27:25.503207Z","shell.execute_reply.started":"2024-02-11T12:26:43.571219Z","shell.execute_reply":"2024-02-11T12:27:25.502159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\ny_predictions = model.predict(X_test)\ny_pred = np.where(y_predictions>0.5,1,0)\nacc = accuracy_score(y_pred,y_test)\nprint(acc)","metadata":{"execution":{"iopub.status.busy":"2024-02-11T12:32:03.219474Z","iopub.execute_input":"2024-02-11T12:32:03.219854Z","iopub.status.idle":"2024-02-11T12:32:04.349155Z","shell.execute_reply.started":"2024-02-11T12:32:03.219825Z","shell.execute_reply":"2024-02-11T12:32:04.348129Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test1 = np.array(test)\ntest1 = test1.reshape(test1.shape[0],28,28,1)\ntest1 = test1.astype(\"float32\")\ntest1 /=255.0","metadata":{"execution":{"iopub.status.busy":"2024-02-11T12:44:54.048026Z","iopub.execute_input":"2024-02-11T12:44:54.048845Z","iopub.status.idle":"2024-02-11T12:44:54.165153Z","shell.execute_reply.started":"2024-02-11T12:44:54.048814Z","shell.execute_reply":"2024-02-11T12:44:54.164099Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test1.shape","metadata":{"execution":{"iopub.status.busy":"2024-02-11T12:44:56.626297Z","iopub.execute_input":"2024-02-11T12:44:56.626928Z","iopub.status.idle":"2024-02-11T12:44:56.632825Z","shell.execute_reply.started":"2024-02-11T12:44:56.626896Z","shell.execute_reply":"2024-02-11T12:44:56.631849Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = model.predict(test1)","metadata":{"execution":{"iopub.status.busy":"2024-02-11T12:45:46.043657Z","iopub.execute_input":"2024-02-11T12:45:46.044470Z","iopub.status.idle":"2024-02-11T12:45:48.051138Z","shell.execute_reply.started":"2024-02-11T12:45:46.044436Z","shell.execute_reply":"2024-02-11T12:45:48.050231Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = np.argmax(predictions,axis = 1)\npredictions = pd.Series(predictions,name = \"Label\")","metadata":{"execution":{"iopub.status.busy":"2024-02-11T12:45:48.703587Z","iopub.execute_input":"2024-02-11T12:45:48.703982Z","iopub.status.idle":"2024-02-11T12:45:48.709339Z","shell.execute_reply.started":"2024-02-11T12:45:48.703949Z","shell.execute_reply":"2024-02-11T12:45:48.708411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-11T12:45:55.923868Z","iopub.execute_input":"2024-02-11T12:45:55.924511Z","iopub.status.idle":"2024-02-11T12:45:55.931177Z","shell.execute_reply.started":"2024-02-11T12:45:55.924478Z","shell.execute_reply":"2024-02-11T12:45:55.930081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.concat([pd.Series(range(1,28001), name = \"ImageId\"),predictions],axis = 1)\nsubmission.to_csv(\"s.csv\", index = False)","metadata":{"execution":{"iopub.status.busy":"2024-02-11T12:46:16.878094Z","iopub.execute_input":"2024-02-11T12:46:16.878893Z","iopub.status.idle":"2024-02-11T12:46:16.917954Z","shell.execute_reply.started":"2024-02-11T12:46:16.878861Z","shell.execute_reply":"2024-02-11T12:46:16.917017Z"},"trusted":true},"execution_count":null,"outputs":[]}]}