{"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\nfrom sklearn.model_selection import train_test_split\nfrom tensorflow import keras\nfrom tensorflow.keras import layers","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-06-22T19:06:25.775374Z","iopub.execute_input":"2024-06-22T19:06:25.775783Z","iopub.status.idle":"2024-06-22T19:06:25.782210Z","shell.execute_reply.started":"2024-06-22T19:06:25.775753Z","shell.execute_reply":"2024-06-22T19:06:25.780382Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = pd.read_csv('../input/digit-recognizer/train.csv')\ny = X.pop('label')\nprint(X.shape)\n\nX_train, X_valid, y_train, y_valid = train_test_split(X, y, stratify=y, train_size=0.75)\n\ninput_shape = [X_train.shape[1]]\nprint(input_shape)","metadata":{"execution":{"iopub.status.busy":"2024-06-22T19:06:25.785154Z","iopub.execute_input":"2024-06-22T19:06:25.785669Z","iopub.status.idle":"2024-06-22T19:06:29.213372Z","shell.execute_reply.started":"2024-06-22T19:06:25.785627Z","shell.execute_reply":"2024-06-22T19:06:29.212201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = keras.Sequential([\n    layers.InputLayer(shape=input_shape),\n    layers.Dense(units=784, activation=\"relu\"),\n    layers.BatchNormalization(),\n    layers.Dropout(rate=0.3),\n    layers.Dense(units=784, activation=\"relu\"),\n    layers.BatchNormalization(),\n    layers.Dropout(rate=0.3),\n    layers.Dense(units=10, activation=\"softmax\")\n])","metadata":{"execution":{"iopub.status.busy":"2024-06-22T19:06:29.214645Z","iopub.execute_input":"2024-06-22T19:06:29.214986Z","iopub.status.idle":"2024-06-22T19:06:29.282671Z","shell.execute_reply.started":"2024-06-22T19:06:29.214956Z","shell.execute_reply":"2024-06-22T19:06:29.281539Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(\n    optimizer=\"adam\",\n    loss=\"sparse_categorical_crossentropy\",\n    metrics=[\"sparse_categorical_accuracy\"]\n)","metadata":{"execution":{"iopub.status.busy":"2024-06-22T19:06:29.283945Z","iopub.execute_input":"2024-06-22T19:06:29.284265Z","iopub.status.idle":"2024-06-22T19:06:29.294831Z","shell.execute_reply.started":"2024-06-22T19:06:29.284237Z","shell.execute_reply":"2024-06-22T19:06:29.293703Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"early_stopping = keras.callbacks.EarlyStopping(\n    patience=5,\n    min_delta=0.001,\n    restore_best_weights=True,\n)\n\nhistory = model.fit(\n    X_train, y_train,\n    validation_data=(X_valid, y_valid),\n    batch_size=512,\n    epochs=200,\n    callbacks=[early_stopping],\n)\n\nhistory_df = pd.DataFrame(history.history)\nhistory_df.loc[:, ['loss', 'val_loss']].plot(title=\"Cross-entropy\")\nhistory_df.loc[:, ['sparse_categorical_accuracy', 'val_sparse_categorical_accuracy']].plot(title=\"Accuracy\")","metadata":{"execution":{"iopub.status.busy":"2024-06-22T19:06:29.301622Z","iopub.execute_input":"2024-06-22T19:06:29.301990Z","iopub.status.idle":"2024-06-22T19:07:05.698502Z","shell.execute_reply.started":"2024-06-22T19:06:29.301962Z","shell.execute_reply":"2024-06-22T19:07:05.697345Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv(\"../input/digit-recognizer/test.csv\")\nX_test = test.values.reshape(test.shape[0], 28, 28, 1)\nX_test_flattened = X_test.reshape(X_test.shape[0], -1)\n\npredictions = model.predict(X_test_flattened, verbose=0)\npredictions_labels = predictions.argmax(axis=1)\n\nsubmissions = pd.DataFrame({\"ImageId\": list(range(1, len(predictions_labels) + 1)), \"Label\": predictions_labels})\nsubmissions.to_csv(\"submission.csv\", index=False, header=True)","metadata":{"execution":{"iopub.status.busy":"2024-06-22T19:08:32.013623Z","iopub.execute_input":"2024-06-22T19:08:32.014106Z","iopub.status.idle":"2024-06-22T19:08:36.870935Z","shell.execute_reply.started":"2024-06-22T19:08:32.014068Z","shell.execute_reply":"2024-06-22T19:08:36.869681Z"},"trusted":true},"execution_count":null,"outputs":[]}]}