{"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":"nvidiaTeslaT4","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30673,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","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","execution":{"iopub.status.busy":"2024-04-09T20:44:03.036845Z","iopub.execute_input":"2024-04-09T20:44:03.037664Z","iopub.status.idle":"2024-04-09T20:44:03.938364Z","shell.execute_reply.started":"2024-04-09T20:44:03.037630Z","shell.execute_reply":"2024-04-09T20:44:03.937439Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow\nfrom tensorflow import keras\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Dense, Flatten","metadata":{"execution":{"iopub.status.busy":"2024-04-09T20:44:03.940421Z","iopub.execute_input":"2024-04-09T20:44:03.940914Z","iopub.status.idle":"2024-04-09T20:44:15.615383Z","shell.execute_reply.started":"2024-04-09T20:44:03.940883Z","shell.execute_reply":"2024-04-09T20:44:15.614638Z"},"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":"2024-04-09T20:44:15.616567Z","iopub.execute_input":"2024-04-09T20:44:15.617078Z","iopub.status.idle":"2024-04-09T20:44:16.104049Z","shell.execute_reply.started":"2024-04-09T20:44:15.617053Z","shell.execute_reply":"2024-04-09T20:44:16.103049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nplt.imshow(X_train[2])","metadata":{"execution":{"iopub.status.busy":"2024-04-09T20:44:16.106175Z","iopub.execute_input":"2024-04-09T20:44:16.106488Z","iopub.status.idle":"2024-04-09T20:44:16.375005Z","shell.execute_reply.started":"2024-04-09T20:44:16.106463Z","shell.execute_reply":"2024-04-09T20:44:16.374175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train/255\nX_test = X_test/255","metadata":{"execution":{"iopub.status.busy":"2024-04-09T20:44:16.375923Z","iopub.execute_input":"2024-04-09T20:44:16.376181Z","iopub.status.idle":"2024-04-09T20:44:16.561276Z","shell.execute_reply.started":"2024-04-09T20:44:16.376159Z","shell.execute_reply":"2024-04-09T20:44:16.560291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Sequential()\nmodel.add(Flatten(input_shape=(28,28)))\nmodel.add(Dense(128,activation='relu'))\nmodel.add(Dense(10,activation='softmax'))","metadata":{"execution":{"iopub.status.busy":"2024-04-09T20:44:16.562417Z","iopub.execute_input":"2024-04-09T20:44:16.562712Z","iopub.status.idle":"2024-04-09T20:44:17.345911Z","shell.execute_reply.started":"2024-04-09T20:44:16.562688Z","shell.execute_reply":"2024-04-09T20:44:17.345000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2024-04-09T20:44:17.347284Z","iopub.execute_input":"2024-04-09T20:44:17.347608Z","iopub.status.idle":"2024-04-09T20:44:17.368194Z","shell.execute_reply.started":"2024-04-09T20:44:17.347581Z","shell.execute_reply":"2024-04-09T20:44:17.367212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.compile(loss='sparse_categorical_crossentropy',optimizer='Adam',metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-04-09T20:44:17.369607Z","iopub.execute_input":"2024-04-09T20:44:17.370008Z","iopub.status.idle":"2024-04-09T20:44:17.384552Z","shell.execute_reply.started":"2024-04-09T20:44:17.369974Z","shell.execute_reply":"2024-04-09T20:44:17.383764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(X_train,y_train,epochs=30,validation_split=0.05)","metadata":{"execution":{"iopub.status.busy":"2024-04-09T21:08:50.798794Z","iopub.execute_input":"2024-04-09T21:08:50.799708Z","iopub.status.idle":"2024-04-09T21:10:20.600548Z","shell.execute_reply.started":"2024-04-09T21:08:50.799669Z","shell.execute_reply":"2024-04-09T21:10:20.599748Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pb = model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2024-04-09T21:10:37.650519Z","iopub.execute_input":"2024-04-09T21:10:37.651320Z","iopub.status.idle":"2024-04-09T21:10:38.228433Z","shell.execute_reply.started":"2024-04-09T21:10:37.651287Z","shell.execute_reply":"2024-04-09T21:10:38.227598Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pd = y_pb.argmax(axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-04-09T21:10:38.230613Z","iopub.execute_input":"2024-04-09T21:10:38.231406Z","iopub.status.idle":"2024-04-09T21:10:38.235612Z","shell.execute_reply.started":"2024-04-09T21:10:38.231378Z","shell.execute_reply":"2024-04-09T21:10:38.234572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\naccuracy_score(y_test,y_pd)","metadata":{"execution":{"iopub.status.busy":"2024-04-09T21:10:38.511657Z","iopub.execute_input":"2024-04-09T21:10:38.511909Z","iopub.status.idle":"2024-04-09T21:10:38.519624Z","shell.execute_reply.started":"2024-04-09T21:10:38.511888Z","shell.execute_reply":"2024-04-09T21:10:38.518620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['accuracy'])\nplt.plot(history.history['val_accuracy'])","metadata":{"execution":{"iopub.status.busy":"2024-04-09T21:10:38.961749Z","iopub.execute_input":"2024-04-09T21:10:38.962007Z","iopub.status.idle":"2024-04-09T21:10:39.202022Z","shell.execute_reply.started":"2024-04-09T21:10:38.961976Z","shell.execute_reply":"2024-04-09T21:10:39.201175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X_test[3])","metadata":{"execution":{"iopub.status.busy":"2024-04-09T21:10:39.412490Z","iopub.execute_input":"2024-04-09T21:10:39.412748Z","iopub.status.idle":"2024-04-09T21:10:39.653006Z","shell.execute_reply.started":"2024-04-09T21:10:39.412726Z","shell.execute_reply":"2024-04-09T21:10:39.652175Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.predict(X_test[3].reshape(1,28,28)).argmax(axis=1)","metadata":{"execution":{"iopub.status.busy":"2024-04-09T21:10:39.885267Z","iopub.execute_input":"2024-04-09T21:10:39.885512Z","iopub.status.idle":"2024-04-09T21:10:39.952386Z","shell.execute_reply.started":"2024-04-09T21:10:39.885491Z","shell.execute_reply":"2024-04-09T21:10:39.951498Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test=pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntest_values = test.values","metadata":{"execution":{"iopub.status.busy":"2024-04-09T21:10:40.393051Z","iopub.execute_input":"2024-04-09T21:10:40.393323Z","iopub.status.idle":"2024-04-09T21:10:41.874428Z","shell.execute_reply.started":"2024-04-09T21:10:40.393301Z","shell.execute_reply":"2024-04-09T21:10:41.873552Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(test_values.shape)\nprint(X_test.shape)","metadata":{"execution":{"iopub.status.busy":"2024-04-09T21:10:41.876037Z","iopub.execute_input":"2024-04-09T21:10:41.876346Z","iopub.status.idle":"2024-04-09T21:10:41.881047Z","shell.execute_reply.started":"2024-04-09T21:10:41.876322Z","shell.execute_reply":"2024-04-09T21:10:41.880120Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_values_reshaped = test_values.reshape(28000, 28, 28)\ntest_values_reshaped.shape","metadata":{"execution":{"iopub.status.busy":"2024-04-09T21:10:41.882228Z","iopub.execute_input":"2024-04-09T21:10:41.882530Z","iopub.status.idle":"2024-04-09T21:10:41.895954Z","shell.execute_reply.started":"2024-04-09T21:10:41.882508Z","shell.execute_reply":"2024-04-09T21:10:41.895073Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_out = model.predict(test_values_reshaped)","metadata":{"execution":{"iopub.status.busy":"2024-04-09T21:10:43.152351Z","iopub.execute_input":"2024-04-09T21:10:43.152638Z","iopub.status.idle":"2024-04-09T21:10:45.056240Z","shell.execute_reply.started":"2024-04-09T21:10:43.152614Z","shell.execute_reply":"2024-04-09T21:10:45.055411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y =test_out.argmax(axis=1)\nsubmission = pd.DataFrame(y, columns=['Label'])\nsubmission.index.name = 'ImageId'\nsubmission.index += 1\nsubmission.to_csv('/kaggle/working/submission.csv')\nprint('Submission completed!')","metadata":{"execution":{"iopub.status.busy":"2024-04-09T21:10:45.559542Z","iopub.execute_input":"2024-04-09T21:10:45.560281Z","iopub.status.idle":"2024-04-09T21:10:45.597796Z","shell.execute_reply.started":"2024-04-09T21:10:45.560254Z","shell.execute_reply":"2024-04-09T21:10:45.596883Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}