{"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"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import 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":"2023-09-08T05:57:40.737687Z","iopub.execute_input":"2023-09-08T05:57:40.739331Z","iopub.status.idle":"2023-09-08T05:57:40.749798Z","shell.execute_reply.started":"2023-09-08T05:57:40.739284Z","shell.execute_reply":"2023-09-08T05:57:40.748420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport numpy as np\nfrom tensorflow import keras\nimport matplotlib.pyplot as plt\n%matplotlib inline","metadata":{"execution":{"iopub.status.busy":"2023-09-08T05:57:40.765374Z","iopub.execute_input":"2023-09-08T05:57:40.766034Z","iopub.status.idle":"2023-09-08T05:57:40.773647Z","shell.execute_reply.started":"2023-09-08T05:57:40.766001Z","shell.execute_reply":"2023-09-08T05:57:40.772297Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ndf","metadata":{"execution":{"iopub.status.busy":"2023-09-08T05:57:40.775431Z","iopub.execute_input":"2023-09-08T05:57:40.776303Z","iopub.status.idle":"2023-09-08T05:57:44.779150Z","shell.execute_reply.started":"2023-09-08T05:57:40.776270Z","shell.execute_reply":"2023-09-08T05:57:44.777831Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Assuming df is your DataFrame\npixel_columns = df.columns[1:]  # Exclude the first column if it's not pixel data\n\n# Extract the pixel values as a NumPy array\npixels = df[pixel_columns].to_numpy()\n\n# Reshape the pixels into a 28x28 format\nX_train_main = pixels.reshape(-1, 28, 28)","metadata":{"execution":{"iopub.status.busy":"2023-09-08T05:57:44.781729Z","iopub.execute_input":"2023-09-08T05:57:44.782412Z","iopub.status.idle":"2023-09-08T05:57:44.912164Z","shell.execute_reply.started":"2023-09-08T05:57:44.782380Z","shell.execute_reply":"2023-09-08T05:57:44.910579Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_main[0]","metadata":{"execution":{"iopub.status.busy":"2023-09-08T05:57:44.913710Z","iopub.execute_input":"2023-09-08T05:57:44.914107Z","iopub.status.idle":"2023-09-08T05:57:44.935266Z","shell.execute_reply.started":"2023-09-08T05:57:44.914076Z","shell.execute_reply":"2023-09-08T05:57:44.933655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.matshow(X_train_main[0])","metadata":{"execution":{"iopub.status.busy":"2023-09-08T05:57:44.939282Z","iopub.execute_input":"2023-09-08T05:57:44.940055Z","iopub.status.idle":"2023-09-08T05:57:45.401380Z","shell.execute_reply.started":"2023-09-08T05:57:44.940012Z","shell.execute_reply":"2023-09-08T05:57:45.399984Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\ndf_submission","metadata":{"execution":{"iopub.status.busy":"2023-09-08T05:57:45.402837Z","iopub.execute_input":"2023-09-08T05:57:45.403379Z","iopub.status.idle":"2023-09-08T05:57:45.435224Z","shell.execute_reply.started":"2023-09-08T05:57:45.403349Z","shell.execute_reply":"2023-09-08T05:57:45.432612Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ndf_test","metadata":{"execution":{"iopub.status.busy":"2023-09-08T05:57:45.437812Z","iopub.execute_input":"2023-09-08T05:57:45.439598Z","iopub.status.idle":"2023-09-08T05:57:47.747452Z","shell.execute_reply.started":"2023-09-08T05:57:45.439544Z","shell.execute_reply":"2023-09-08T05:57:47.746065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Assuming df is your DataFrame\npixel_columns = df_test.columns  # Exclude the first column if it's not pixel data\n\n# Extract the pixel values as a NumPy array\npixels = df_test[pixel_columns].to_numpy()\n\n# Reshape the pixels into a 28x28 format\nX_test_main = pixels.reshape(-1, 28, 28)\nX_test_main.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-08T05:57:47.749798Z","iopub.execute_input":"2023-09-08T05:57:47.750331Z","iopub.status.idle":"2023-09-08T05:57:47.884837Z","shell.execute_reply.started":"2023-09-08T05:57:47.750286Z","shell.execute_reply":"2023-09-08T05:57:47.883362Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_main.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-08T05:57:47.886556Z","iopub.execute_input":"2023-09-08T05:57:47.887679Z","iopub.status.idle":"2023-09-08T05:57:47.898077Z","shell.execute_reply.started":"2023-09-08T05:57:47.887631Z","shell.execute_reply":"2023-09-08T05:57:47.896437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_main = X_train_main / 255.0\nX_test_main = X_test_main / 255.0","metadata":{"execution":{"iopub.status.busy":"2023-09-08T05:57:47.899956Z","iopub.execute_input":"2023-09-08T05:57:47.900761Z","iopub.status.idle":"2023-09-08T05:57:48.262622Z","shell.execute_reply.started":"2023-09-08T05:57:47.900717Z","shell.execute_reply":"2023-09-08T05:57:48.261199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_main[0]","metadata":{"execution":{"iopub.status.busy":"2023-09-08T05:57:48.268089Z","iopub.execute_input":"2023-09-08T05:57:48.268460Z","iopub.status.idle":"2023-09-08T05:57:48.286749Z","shell.execute_reply.started":"2023-09-08T05:57:48.268431Z","shell.execute_reply":"2023-09-08T05:57:48.285357Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(X_train, y_train) , (X_test, y_test) = keras.datasets.mnist.load_data()","metadata":{"execution":{"iopub.status.busy":"2023-09-08T05:57:48.291035Z","iopub.execute_input":"2023-09-08T05:57:48.291403Z","iopub.status.idle":"2023-09-08T05:57:48.627512Z","shell.execute_reply.started":"2023-09-08T05:57:48.291375Z","shell.execute_reply":"2023-09-08T05:57:48.626289Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-08T05:57:48.629125Z","iopub.execute_input":"2023-09-08T05:57:48.629575Z","iopub.status.idle":"2023-09-08T05:57:48.638801Z","shell.execute_reply.started":"2023-09-08T05:57:48.629536Z","shell.execute_reply":"2023-09-08T05:57:48.637147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train / 255.0\nX_test = X_test / 255.0","metadata":{"execution":{"iopub.status.busy":"2023-09-08T05:57:48.640372Z","iopub.execute_input":"2023-09-08T05:57:48.640771Z","iopub.status.idle":"2023-09-08T05:57:48.940704Z","shell.execute_reply.started":"2023-09-08T05:57:48.640740Z","shell.execute_reply":"2023-09-08T05:57:48.939376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train[0]","metadata":{"execution":{"iopub.status.busy":"2023-09-08T05:57:48.942764Z","iopub.execute_input":"2023-09-08T05:57:48.943144Z","iopub.status.idle":"2023-09-08T05:57:48.964907Z","shell.execute_reply.started":"2023-09-08T05:57:48.943113Z","shell.execute_reply":"2023-09-08T05:57:48.962938Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_main_flattened = X_train_main.reshape(len(X_train_main), 28*28)\nX_test_main_flattened = X_test_main.reshape(len(X_test_main), 28*28)\nX_train_main_flattened.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-08T05:57:48.966431Z","iopub.execute_input":"2023-09-08T05:57:48.966761Z","iopub.status.idle":"2023-09-08T05:57:48.976993Z","shell.execute_reply.started":"2023-09-08T05:57:48.966732Z","shell.execute_reply":"2023-09-08T05:57:48.975373Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_flattened = X_train.reshape(len(X_train), 28*28)\nX_test_flattened = X_test.reshape(len(X_test), 28*28)\nX_train_flattened.shape","metadata":{"execution":{"iopub.status.busy":"2023-09-08T05:57:48.978340Z","iopub.execute_input":"2023-09-08T05:57:48.978654Z","iopub.status.idle":"2023-09-08T05:57:48.991435Z","shell.execute_reply.started":"2023-09-08T05:57:48.978627Z","shell.execute_reply":"2023-09-08T05:57:48.990111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras import datasets,layers,models\nmodel = models.Sequential([\n    # Convolutional layers\n    layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),\n    layers.MaxPooling2D((2, 2)),\n    \n    layers.Conv2D(64, (3, 3), activation='relu'),\n    layers.MaxPooling2D((2, 2)),\n    \n    layers.Conv2D(64, (3, 3), activation='relu'),\n    \n    # Flatten and fully connected layers\n    layers.Flatten(),\n    layers.Dense(512, activation='relu'),\n    layers.Dropout(0.5),  # Dropout for regularization\n    layers.Dense(128, activation='relu'),\n    layers.Dropout(0.5),\n    \n    # Output layer with 10 units for 10 classes\n    layers.Dense(10, activation='softmax')\n])\n\n# Compile the model\nmodel.compile(optimizer='adam',\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])\n\n# Summary of the model architecture\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-09-08T05:57:48.993512Z","iopub.execute_input":"2023-09-08T05:57:48.993873Z","iopub.status.idle":"2023-09-08T05:57:49.158800Z","shell.execute_reply.started":"2023-09-08T05:57:48.993822Z","shell.execute_reply":"2023-09-08T05:57:49.157887Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer='adam',\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])\n\nmodel.fit(X_train,y_train, epochs=20)","metadata":{"execution":{"iopub.status.busy":"2023-09-08T05:58:11.327121Z","iopub.execute_input":"2023-09-08T05:58:11.327518Z","iopub.status.idle":"2023-09-08T06:14:34.988220Z","shell.execute_reply.started":"2023-09-08T05:58:11.327488Z","shell.execute_reply":"2023-09-08T06:14:34.986949Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train_main_reshaped = X_train_main_flattened.reshape(-1, 28, 28, 1)\n# predicted_train = model.predict(X_train_main_reshaped)\n# predicted_train\nmodel.evaluate(X_train_main_reshaped, df['label'])\n#X_train_main_reshaped = X_train_main_flattened.reshape(-1, 28, 28, 1)\n# predicted_train = model.predict(X_train_main)\n# predicted_train","metadata":{"execution":{"iopub.status.busy":"2023-09-08T06:15:06.278932Z","iopub.execute_input":"2023-09-08T06:15:06.279378Z","iopub.status.idle":"2023-09-08T06:15:17.875651Z","shell.execute_reply.started":"2023-09-08T06:15:06.279347Z","shell.execute_reply":"2023-09-08T06:15:17.873929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_train = model.predict(X_train_main_reshaped)","metadata":{"execution":{"iopub.status.busy":"2023-09-08T06:16:39.069471Z","iopub.execute_input":"2023-09-08T06:16:39.069948Z","iopub.status.idle":"2023-09-08T06:16:51.155517Z","shell.execute_reply.started":"2023-09-08T06:16:39.069912Z","shell.execute_reply":"2023-09-08T06:16:51.154151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_train_labels = predicted_train.argmax(axis=1)\npredicted_train_labels","metadata":{"execution":{"iopub.status.busy":"2023-09-08T06:16:55.057870Z","iopub.execute_input":"2023-09-08T06:16:55.059517Z","iopub.status.idle":"2023-09-08T06:16:55.069440Z","shell.execute_reply.started":"2023-09-08T06:16:55.059469Z","shell.execute_reply":"2023-09-08T06:16:55.068420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(predicted_train_labels[:20])\nprint(df['label'][:20])","metadata":{"execution":{"iopub.status.busy":"2023-09-08T06:16:59.468730Z","iopub.execute_input":"2023-09-08T06:16:59.469237Z","iopub.status.idle":"2023-09-08T06:16:59.478610Z","shell.execute_reply.started":"2023-09-08T06:16:59.469203Z","shell.execute_reply":"2023-09-08T06:16:59.476713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-09-08T05:57:49.226886Z","iopub.status.idle":"2023-09-08T05:57:49.227298Z","shell.execute_reply.started":"2023-09-08T05:57:49.227103Z","shell.execute_reply":"2023-09-08T05:57:49.227122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test_main_reshaped = X_test_main_flattened.reshape(-1, 28, 28, 1)\npredicted_test = model.predict(X_test_main_reshaped)\npredicted_test","metadata":{"execution":{"iopub.status.busy":"2023-09-08T06:17:40.225436Z","iopub.execute_input":"2023-09-08T06:17:40.225982Z","iopub.status.idle":"2023-09-08T06:17:48.109839Z","shell.execute_reply.started":"2023-09-08T06:17:40.225948Z","shell.execute_reply":"2023-09-08T06:17:48.108629Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predicted_test_labels = predicted_test.argmax(axis=1)\npredicted_test_labels","metadata":{"execution":{"iopub.status.busy":"2023-09-08T06:17:52.940760Z","iopub.execute_input":"2023-09-08T06:17:52.941209Z","iopub.status.idle":"2023-09-08T06:17:52.950671Z","shell.execute_reply.started":"2023-09-08T06:17:52.941173Z","shell.execute_reply":"2023-09-08T06:17:52.949195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_submission = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\ndf_submission","metadata":{"execution":{"iopub.status.busy":"2023-09-08T06:18:33.842551Z","iopub.execute_input":"2023-09-08T06:18:33.843049Z","iopub.status.idle":"2023-09-08T06:18:33.868569Z","shell.execute_reply.started":"2023-09-08T06:18:33.843012Z","shell.execute_reply":"2023-09-08T06:18:33.867190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"len(predicted_test_labels)","metadata":{"execution":{"iopub.status.busy":"2023-09-08T06:18:50.137945Z","iopub.execute_input":"2023-09-08T06:18:50.138368Z","iopub.status.idle":"2023-09-08T06:18:50.147459Z","shell.execute_reply.started":"2023-09-08T06:18:50.138337Z","shell.execute_reply":"2023-09-08T06:18:50.145775Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res = pd.DataFrame()\nres['ImageId'] = df_submission['ImageId']\nres['Label'] = predicted_test_labels\nres","metadata":{"execution":{"iopub.status.busy":"2023-09-08T06:19:08.681145Z","iopub.execute_input":"2023-09-08T06:19:08.681628Z","iopub.status.idle":"2023-09-08T06:19:08.703704Z","shell.execute_reply.started":"2023-09-08T06:19:08.681595Z","shell.execute_reply":"2023-09-08T06:19:08.702370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"res.to_csv('sub4.csv',index = False)","metadata":{"execution":{"iopub.status.busy":"2023-09-08T06:19:34.550746Z","iopub.execute_input":"2023-09-08T06:19:34.552394Z","iopub.status.idle":"2023-09-08T06:19:34.625382Z","shell.execute_reply.started":"2023-09-08T06:19:34.552337Z","shell.execute_reply":"2023-09-08T06:19:34.624341Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}],"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}}