{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"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":"2022-12-12T01:36:04.445867Z","iopub.execute_input":"2022-12-12T01:36:04.446944Z","iopub.status.idle":"2022-12-12T01:36:04.456784Z","shell.execute_reply.started":"2022-12-12T01:36:04.446862Z","shell.execute_reply":"2022-12-12T01:36:04.455320Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"load data .csv file ","metadata":{}},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\n","metadata":{"execution":{"iopub.status.busy":"2022-12-12T01:36:04.459765Z","iopub.execute_input":"2022-12-12T01:36:04.460426Z","iopub.status.idle":"2022-12-12T01:36:08.595995Z","shell.execute_reply.started":"2022-12-12T01:36:04.460389Z","shell.execute_reply":"2022-12-12T01:36:08.595069Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-12T01:36:08.597174Z","iopub.execute_input":"2022-12-12T01:36:08.597939Z","iopub.status.idle":"2022-12-12T01:36:08.615084Z","shell.execute_reply.started":"2022-12-12T01:36:08.597897Z","shell.execute_reply":"2022-12-12T01:36:08.614228Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"set up the base data needed","metadata":{}},{"cell_type":"code","source":"train_y = train[['label']]\ntrain_y.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-12T01:36:08.617169Z","iopub.execute_input":"2022-12-12T01:36:08.617814Z","iopub.status.idle":"2022-12-12T01:36:08.631637Z","shell.execute_reply.started":"2022-12-12T01:36:08.617779Z","shell.execute_reply":"2022-12-12T01:36:08.630661Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x = train.drop('label',axis=1)\ntrain_x.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-12T01:49:59.963937Z","iopub.execute_input":"2022-12-12T01:49:59.964468Z","iopub.status.idle":"2022-12-12T01:50:00.078383Z","shell.execute_reply.started":"2022-12-12T01:49:59.964423Z","shell.execute_reply":"2022-12-12T01:50:00.077191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"set up the dataset","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split \nx_train,x_test,y_train,y_test = train_test_split(train_x,train_y,test_size=0.2)\nprint(x_train.shape,y_train.shape,x_test.shape,y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-12-12T01:36:08.632966Z","iopub.execute_input":"2022-12-12T01:36:08.633295Z","iopub.status.idle":"2022-12-12T01:36:08.948587Z","shell.execute_reply.started":"2022-12-12T01:36:08.633267Z","shell.execute_reply":"2022-12-12T01:36:08.947229Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# x_train = x_train/255.0 \n# x_test = x_test/255.0 \ninput_shape = train_x.shape[1]\nprint(input_shape)","metadata":{"execution":{"iopub.status.busy":"2022-12-12T01:36:08.950044Z","iopub.execute_input":"2022-12-12T01:36:08.950371Z","iopub.status.idle":"2022-12-12T01:36:08.957540Z","shell.execute_reply.started":"2022-12-12T01:36:08.950342Z","shell.execute_reply":"2022-12-12T01:36:08.956041Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"build the simple dense model","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf \nfrom tensorflow.keras.layers import Input,Dense,Dropout\nfrom tensorflow.keras.models import Sequential\n\n\nmodel = Sequential()\nmodel.add(Input(input_shape))\nmodel.add(Dense(128,activation='relu'))\nmodel.add(Dense(128,activation='relu'))\nmodel.add(Dense(10,activation='softmax'))\nmodel.compile(optimizer='adam', loss=\"sparse_categorical_crossentropy\",metrics=['accuracy'])\nmodel.summary()\n","metadata":{"execution":{"iopub.status.busy":"2022-12-12T01:36:08.958905Z","iopub.execute_input":"2022-12-12T01:36:08.959235Z","iopub.status.idle":"2022-12-12T01:36:09.007076Z","shell.execute_reply.started":"2022-12-12T01:36:08.959207Z","shell.execute_reply":"2022-12-12T01:36:09.005928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history = model.fit(x_train,y_train,epochs=10,validation_data=(x_test,y_test))","metadata":{"execution":{"iopub.status.busy":"2022-12-12T01:36:09.008235Z","iopub.execute_input":"2022-12-12T01:36:09.008540Z","iopub.status.idle":"2022-12-12T01:36:47.475941Z","shell.execute_reply.started":"2022-12-12T01:36:09.008512Z","shell.execute_reply":"2022-12-12T01:36:47.474642Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt \nplt.plot(history.history['accuracy'])\nplt.plot(history.history['loss'])\nplt.title('model accuracy')\nplt.ylabel('accuracy')\nplt.xlabel('epoch')\nplt.legend(['accuracy', 'loss'], loc='upper left')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-12-12T01:36:47.479845Z","iopub.execute_input":"2022-12-12T01:36:47.480261Z","iopub.status.idle":"2022-12-12T01:36:47.687836Z","shell.execute_reply.started":"2022-12-12T01:36:47.480227Z","shell.execute_reply":"2022-12-12T01:36:47.686483Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"set up the output .csv ","metadata":{}},{"cell_type":"code","source":"predictions = model.predict(test)","metadata":{"execution":{"iopub.status.busy":"2022-12-12T01:36:47.689617Z","iopub.execute_input":"2022-12-12T01:36:47.690127Z","iopub.status.idle":"2022-12-12T01:36:49.053367Z","shell.execute_reply.started":"2022-12-12T01:36:47.690079Z","shell.execute_reply":"2022-12-12T01:36:49.052158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred= [np.argmax(i) for i in predictions]","metadata":{"execution":{"iopub.status.busy":"2022-12-12T01:36:49.055201Z","iopub.execute_input":"2022-12-12T01:36:49.055718Z","iopub.status.idle":"2022-12-12T01:36:49.115636Z","shell.execute_reply.started":"2022-12-12T01:36:49.055667Z","shell.execute_reply":"2022-12-12T01:36:49.114411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test['label'] = y_pred\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-12T01:36:49.116984Z","iopub.execute_input":"2022-12-12T01:36:49.117335Z","iopub.status.idle":"2022-12-12T01:36:49.151601Z","shell.execute_reply.started":"2022-12-12T01:36:49.117303Z","shell.execute_reply":"2022-12-12T01:36:49.150495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub = test[['label']]\nsub = sub.reset_index()\nsub.columns = ['ImageId','Label']\nsub['ImageId'] = sub['ImageId'] + 1\nsub.head()","metadata":{"execution":{"iopub.status.busy":"2022-12-12T01:36:49.153115Z","iopub.execute_input":"2022-12-12T01:36:49.153445Z","iopub.status.idle":"2022-12-12T01:36:49.301264Z","shell.execute_reply.started":"2022-12-12T01:36:49.153415Z","shell.execute_reply":"2022-12-12T01:36:49.300111Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2022-12-12T01:36:49.302743Z","iopub.execute_input":"2022-12-12T01:36:49.303221Z","iopub.status.idle":"2022-12-12T01:36:49.339618Z","shell.execute_reply.started":"2022-12-12T01:36:49.303168Z","shell.execute_reply":"2022-12-12T01:36:49.338602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}