{"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":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30732,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Dense, Conv2D, Flatten, MaxPooling2D, Dropout\nfrom tensorflow.keras.utils import to_categorical\nfrom sklearn.model_selection import train_test_split\n\n# Load the data\ntrain = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\n\n# Prepare the training data\nX_train = train.iloc[:, 1:].values\ny_train = train.iloc[:, 0].values\n\n# Normalize the pixel values\nX_train = X_train / 255.0\nX_test = test.values / 255.0  # Convert test DataFrame to numpy array and normalize\n\n# Reshape the data to fit the model\nX_train = X_train.reshape(-1, 28, 28, 1)\nX_test = X_test.reshape(-1, 28, 28, 1)\n\n# One-hot encode the labels\ny_train = to_categorical(y_train)\n\n# Split the training data into training and validation sets\nX_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=42)\n\n# Build the model\nmodel = Sequential([\n    Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)),\n    MaxPooling2D(pool_size=(2, 2)),\n    Dropout(0.25),\n    Conv2D(64, kernel_size=(3, 3), activation='relu'),\n    MaxPooling2D(pool_size=(2, 2)),\n    Dropout(0.25),\n    Flatten(),\n    Dense(128, activation='relu'),\n    Dense(10, activation='softmax')\n])\n\n# Compile the model\nmodel.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])\nmodel.summary()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-07-01T23:21:46.744829Z","iopub.execute_input":"2024-07-01T23:21:46.745259Z","iopub.status.idle":"2024-07-01T23:21:56.088292Z","shell.execute_reply.started":"2024-07-01T23:21:46.745203Z","shell.execute_reply":"2024-07-01T23:21:56.087142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Train the model\nhistory = model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=10, batch_size=200)","metadata":{"execution":{"iopub.status.busy":"2024-07-01T23:22:32.640665Z","iopub.execute_input":"2024-07-01T23:22:32.641171Z","iopub.status.idle":"2024-07-01T23:25:09.451645Z","shell.execute_reply.started":"2024-07-01T23:22:32.641128Z","shell.execute_reply":"2024-07-01T23:25:09.450405Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Evaluate the model\nscore = model.evaluate(X_val, y_val, verbose=0)\nprint('Validation loss:', score[0])\nprint('Validation accuracy:', score[1])","metadata":{"execution":{"iopub.status.busy":"2024-07-01T23:25:28.256547Z","iopub.execute_input":"2024-07-01T23:25:28.257496Z","iopub.status.idle":"2024-07-01T23:25:30.136691Z","shell.execute_reply.started":"2024-07-01T23:25:28.257450Z","shell.execute_reply":"2024-07-01T23:25:30.135331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make predictions on the test set\npredictions = model.predict(X_test)\npredictions = np.argmax(predictions, axis=1)\n\n# Prepare the submission file\nsubmission = pd.DataFrame({'ImageId': np.arange(1, len(predictions) + 1), 'Label': predictions})\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-07-01T23:30:26.871679Z","iopub.execute_input":"2024-07-01T23:30:26.872185Z","iopub.status.idle":"2024-07-01T23:30:32.640949Z","shell.execute_reply.started":"2024-07-01T23:30:26.872143Z","shell.execute_reply":"2024-07-01T23:30:32.639796Z"},"trusted":true},"execution_count":null,"outputs":[]}]}