{"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":30626,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"raw","source":"","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"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 tensorflow as tf\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Dense\nfrom tensorflow.keras.optimizers import Adam\nimport numpy as np\nimport pandas as pd\nfrom sklearn.preprocessing import MinMaxScaler\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2024-01-04T19:40:21.021244Z","iopub.execute_input":"2024-01-04T19:40:21.021936Z","iopub.status.idle":"2024-01-04T19:40:21.029806Z","shell.execute_reply.started":"2024-01-04T19:40:21.021891Z","shell.execute_reply":"2024-01-04T19:40:21.028376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2024-01-04T19:40:21.032562Z","iopub.execute_input":"2024-01-04T19:40:21.033157Z","iopub.status.idle":"2024-01-04T19:40:26.586959Z","shell.execute_reply.started":"2024-01-04T19:40:21.033106Z","shell.execute_reply":"2024-01-04T19:40:26.585628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()","metadata":{"execution":{"iopub.status.busy":"2024-01-04T19:40:26.589338Z","iopub.execute_input":"2024-01-04T19:40:26.590323Z","iopub.status.idle":"2024-01-04T19:40:26.612787Z","shell.execute_reply.started":"2024-01-04T19:40:26.590277Z","shell.execute_reply":"2024-01-04T19:40:26.611483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = train_data['label']\nX_train = train_data.drop('label', axis=1)\n\n# Convert to numpy arrays\ny_train = y_train.values\nX_train = X_train.values\nX_test = test_data.values\n","metadata":{"execution":{"iopub.status.busy":"2024-01-04T19:40:26.614770Z","iopub.execute_input":"2024-01-04T19:40:26.616319Z","iopub.status.idle":"2024-01-04T19:40:26.714403Z","shell.execute_reply.started":"2024-01-04T19:40:26.616263Z","shell.execute_reply":"2024-01-04T19:40:26.713058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train","metadata":{"execution":{"iopub.status.busy":"2024-01-04T19:46:43.663151Z","iopub.execute_input":"2024-01-04T19:46:43.663713Z","iopub.status.idle":"2024-01-04T19:46:43.673193Z","shell.execute_reply.started":"2024-01-04T19:46:43.663672Z","shell.execute_reply":"2024-01-04T19:46:43.671746Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"scaler = MinMaxScaler()\nX_train = scaler.fit_transform(X_train)\nX_test = scaler.transform(X_test)\n","metadata":{"execution":{"iopub.status.busy":"2024-01-04T19:40:26.731939Z","iopub.execute_input":"2024-01-04T19:40:26.732317Z","iopub.status.idle":"2024-01-04T19:40:27.182086Z","shell.execute_reply.started":"2024-01-04T19:40:26.732282Z","shell.execute_reply":"2024-01-04T19:40:27.180651Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2024-01-04T19:40:27.183559Z","iopub.execute_input":"2024-01-04T19:40:27.183941Z","iopub.status.idle":"2024-01-04T19:40:27.627345Z","shell.execute_reply.started":"2024-01-04T19:40:27.183906Z","shell.execute_reply":"2024-01-04T19:40:27.626083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential([\n    Dense(units=10, activation='relu', input_shape=(784,)),\n    Dense(units=10, activation='softmax')])","metadata":{"execution":{"iopub.status.busy":"2024-01-04T19:40:56.544359Z","iopub.execute_input":"2024-01-04T19:40:56.544931Z","iopub.status.idle":"2024-01-04T19:40:56.591003Z","shell.execute_reply.started":"2024-01-04T19:40:56.544894Z","shell.execute_reply":"2024-01-04T19:40:56.589577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-01-04T19:41:00.915630Z","iopub.execute_input":"2024-01-04T19:41:00.916041Z","iopub.status.idle":"2024-01-04T19:41:00.937477Z","shell.execute_reply.started":"2024-01-04T19:41:00.916007Z","shell.execute_reply":"2024-01-04T19:41:00.936313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(optimizer=Adam(learning_rate=0.001), loss='sparse_categorical_crossentropy', metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-01-04T19:41:03.777208Z","iopub.execute_input":"2024-01-04T19:41:03.777726Z","iopub.status.idle":"2024-01-04T19:41:03.793457Z","shell.execute_reply.started":"2024-01-04T19:41:03.777681Z","shell.execute_reply":"2024-01-04T19:41:03.792354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(x=X_train, y=y_train, batch_size=10, epochs=10, shuffle=True, verbose=2, validation_data=(X_val, y_val))","metadata":{"execution":{"iopub.status.busy":"2024-01-04T19:41:07.485441Z","iopub.execute_input":"2024-01-04T19:41:07.486209Z","iopub.status.idle":"2024-01-04T19:42:16.206658Z","shell.execute_reply.started":"2024-01-04T19:41:07.486155Z","shell.execute_reply":"2024-01-04T19:42:16.205298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Make predictions on the test data\ny_pred = model.predict(X_test)\n\n# Convert the predictions to integers\ny_pred = np.argmax(y_pred, axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-01-04T19:42:37.177247Z","iopub.execute_input":"2024-01-04T19:42:37.177751Z","iopub.status.idle":"2024-01-04T19:42:39.228243Z","shell.execute_reply.started":"2024-01-04T19:42:37.177710Z","shell.execute_reply":"2024-01-04T19:42:39.226873Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'ImageId': range(1, len(y_pred) + 1), 'Label': y_pred})\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2024-01-04T19:42:56.397469Z","iopub.execute_input":"2024-01-04T19:42:56.397920Z","iopub.status.idle":"2024-01-04T19:42:56.472099Z","shell.execute_reply.started":"2024-01-04T19:42:56.397885Z","shell.execute_reply":"2024-01-04T19:42:56.470821Z"},"trusted":true},"execution_count":null,"outputs":[]}]}