{"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":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30635,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"raw","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"}},{"cell_type":"raw","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"}},{"cell_type":"code","source":"import pandas as pd\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow.keras.utils import to_categorical\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Flatten, Dense, Dropout, BatchNormalization\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint\nfrom tensorflow.keras.preprocessing.image import ImageDataGenerator\nimport numpy as np","metadata":{"execution":{"iopub.status.busy":"2024-01-22T16:11:43.754061Z","iopub.execute_input":"2024-01-22T16:11:43.754512Z","iopub.status.idle":"2024-01-22T16:11:43.762887Z","shell.execute_reply.started":"2024-01-22T16:11:43.754479Z","shell.execute_reply":"2024-01-22T16:11:43.761295Z"},"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-01-22T16:11:43.770685Z","iopub.execute_input":"2024-01-22T16:11:43.771214Z","iopub.status.idle":"2024-01-22T16:11:47.046451Z","shell.execute_reply.started":"2024-01-22T16:11:43.771168Z","shell.execute_reply":"2024-01-22T16:11:47.044420Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Extract features (pixels) and labels\nX_train = train.drop(\"label\", axis=1) / 255.0  # Normalize the images\ny_train = train[\"label\"]\n","metadata":{"execution":{"iopub.status.busy":"2024-01-22T16:11:47.049501Z","iopub.execute_input":"2024-01-22T16:11:47.049953Z","iopub.status.idle":"2024-01-22T16:11:47.161139Z","shell.execute_reply.started":"2024-01-22T16:11:47.049916Z","shell.execute_reply":"2024-01-22T16:11:47.159469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert labels to one-hot encoding\ny_train_one_hot = to_categorical(y_train)\n","metadata":{"execution":{"iopub.status.busy":"2024-01-22T16:11:47.163284Z","iopub.execute_input":"2024-01-22T16:11:47.163734Z","iopub.status.idle":"2024-01-22T16:11:47.171971Z","shell.execute_reply.started":"2024-01-22T16:11:47.163699Z","shell.execute_reply":"2024-01-22T16:11:47.170052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Split into training and validation sets\nX_train, X_val, y_train, y_val = train_test_split(X_train, y_train_one_hot, test_size=0.2, random_state=42)\n","metadata":{"execution":{"iopub.status.busy":"2024-01-22T16:11:47.174084Z","iopub.execute_input":"2024-01-22T16:11:47.174583Z","iopub.status.idle":"2024-01-22T16:11:47.456271Z","shell.execute_reply.started":"2024-01-22T16:11:47.174550Z","shell.execute_reply":"2024-01-22T16:11:47.454061Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Define the model with the correct input shape\nmodel = Sequential([\n    # Input layer modified to expect the shape of (28, 28, 1)\n    Flatten(input_shape=(28, 28, 1)),\n    Dense(256, activation='relu'),\n    BatchNormalization(),\n    Dropout(0.5),\n    Dense(128, activation='relu'),\n    Dense(10, activation='softmax')\n])","metadata":{"execution":{"iopub.status.busy":"2024-01-22T16:11:47.459611Z","iopub.execute_input":"2024-01-22T16:11:47.460167Z","iopub.status.idle":"2024-01-22T16:11:47.563234Z","shell.execute_reply.started":"2024-01-22T16:11:47.460077Z","shell.execute_reply":"2024-01-22T16:11:47.561793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Compile the model\nmodel.compile(optimizer=Adam(learning_rate=0.001), loss='categorical_crossentropy', metrics=['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2024-01-22T16:11:47.565058Z","iopub.execute_input":"2024-01-22T16:11:47.566354Z","iopub.status.idle":"2024-01-22T16:11:47.584438Z","shell.execute_reply.started":"2024-01-22T16:11:47.566257Z","shell.execute_reply":"2024-01-22T16:11:47.582195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Set up callbacks\nearly_stopping = EarlyStopping(monitor='val_accuracy', patience=5, restore_best_weights=True)\nmodel_checkpoint = ModelCheckpoint('/kaggle/working/best_model.h5', save_best_only=True)\n","metadata":{"execution":{"iopub.status.busy":"2024-01-22T16:11:47.586730Z","iopub.execute_input":"2024-01-22T16:11:47.587297Z","iopub.status.idle":"2024-01-22T16:11:47.596623Z","shell.execute_reply.started":"2024-01-22T16:11:47.587253Z","shell.execute_reply":"2024-01-22T16:11:47.594225Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.image import ImageDataGenerator\n\n# Reshape X_train to have the required rank\nX_train_reshaped = X_train.values.reshape(-1, 28, 28, 1)\n\n# Data augmentation\ndatagen = ImageDataGenerator(rotation_range=20, width_shift_range=0.2, height_shift_range=0.2, shear_range=0.2, zoom_range=0.2)\ndatagen.fit(X_train_reshaped)\n\n","metadata":{"execution":{"iopub.status.busy":"2024-01-22T16:11:47.598546Z","iopub.execute_input":"2024-01-22T16:11:47.598926Z","iopub.status.idle":"2024-01-22T16:11:47.661257Z","shell.execute_reply.started":"2024-01-22T16:11:47.598891Z","shell.execute_reply":"2024-01-22T16:11:47.659256Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(datagen.flow(X_train_reshaped, y_train, batch_size=32), epochs=20,\n          validation_data=(X_val.values.reshape(-1, 28, 28, 1), y_val),\n          callbacks=[early_stopping, model_checkpoint])\n","metadata":{"execution":{"iopub.status.busy":"2024-01-22T16:11:47.663696Z","iopub.execute_input":"2024-01-22T16:11:47.664158Z","iopub.status.idle":"2024-01-22T16:18:24.383779Z","shell.execute_reply.started":"2024-01-22T16:11:47.664120Z","shell.execute_reply":"2024-01-22T16:18:24.382065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reshape X_train for evaluation\nX_train_reshaped_eval = X_train.values.reshape(-1, 28, 28, 1)\n\n# Evaluate on training set\ntrain_accuracy = model.evaluate(X_train_reshaped_eval, y_train, verbose=0)[1]\n\n# Reshape X_val for evaluation\nX_val_reshaped_eval = X_val.values.reshape(-1, 28, 28, 1)\n\n# Evaluate on validation set\nval_accuracy = model.evaluate(X_val_reshaped_eval, y_val, verbose=0)[1]\n\nprint(\"Training Accuracy:\", train_accuracy)\nprint(\"Validation Accuracy:\", val_accuracy)\n","metadata":{"execution":{"iopub.status.busy":"2024-01-22T16:19:21.479325Z","iopub.execute_input":"2024-01-22T16:19:21.479756Z","iopub.status.idle":"2024-01-22T16:19:25.929454Z","shell.execute_reply.started":"2024-01-22T16:19:21.479717Z","shell.execute_reply":"2024-01-22T16:19:25.926915Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reshape test data for prediction\ntest_reshaped = test.values.reshape(-1, 28, 28, 1)\n\n# Predictions for the test set\ny_test_pred_prob = model.predict(test_reshaped / 255.0)\ny_test_pred_class = np.argmax(y_test_pred_prob, axis=1)\n\n# Create a DataFrame for submission\nsubmission_df = pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")\nsubmission_df[\"Label\"] = y_test_pred_class\n\n# Save the DataFrame to a CSV file\nsubmission_df.to_csv(\"/kaggle/working/submission.csv\", index=False)\n","metadata":{"execution":{"iopub.status.busy":"2024-01-22T16:22:04.452246Z","iopub.execute_input":"2024-01-22T16:22:04.453016Z","iopub.status.idle":"2024-01-22T16:22:08.187007Z","shell.execute_reply.started":"2024-01-22T16:22:04.452956Z","shell.execute_reply":"2024-01-22T16:22:08.185656Z"},"trusted":true},"execution_count":null,"outputs":[]}]}