{"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":"markdown","source":"**acc=.9955**\n","metadata":{}},{"cell_type":"markdown","source":"**Here we import the tools which we need**","metadata":{}},{"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\nimport tensorflow as tf\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport warnings\nwarnings.filterwarnings('ignore')","metadata":{"execution":{"iopub.status.busy":"2021-10-12T19:16:52.843318Z","iopub.execute_input":"2021-10-12T19:16:52.843709Z","iopub.status.idle":"2021-10-12T19:16:55.582821Z","shell.execute_reply.started":"2021-10-12T19:16:52.843614Z","shell.execute_reply":"2021-10-12T19:16:55.581815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Here we get the data and read it**","metadata":{}},{"cell_type":"code","source":"train_data=pd.read_csv(\"../input/digit-recognizer/train.csv\")\ntest_data=pd.read_csv(\"../input/digit-recognizer/test.csv\")                       ","metadata":{"execution":{"iopub.status.busy":"2021-10-12T19:16:55.585265Z","iopub.execute_input":"2021-10-12T19:16:55.585652Z","iopub.status.idle":"2021-10-12T19:17:01.562691Z","shell.execute_reply.started":"2021-10-12T19:16:55.585605Z","shell.execute_reply":"2021-10-12T19:17:01.561742Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Here we knowe the shape of data**","metadata":{}},{"cell_type":"code","source":"print(\"train data shape is :\",train_data.shape)\nprint(\"test data shape is :\",test_data.shape)","metadata":{"execution":{"iopub.status.busy":"2021-10-12T19:17:01.564243Z","iopub.execute_input":"2021-10-12T19:17:01.566461Z","iopub.status.idle":"2021-10-12T19:17:01.577406Z","shell.execute_reply.started":"2021-10-12T19:17:01.566408Z","shell.execute_reply":"2021-10-12T19:17:01.572763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Here we split the data to train data and test data**","metadata":{}},{"cell_type":"code","source":"y_train=train_data['label']\nx_train=train_data.drop(labels=['label'],axis=1)\nx_test=test_data","metadata":{"execution":{"iopub.status.busy":"2021-10-12T19:17:01.581036Z","iopub.execute_input":"2021-10-12T19:17:01.581671Z","iopub.status.idle":"2021-10-12T19:17:01.687457Z","shell.execute_reply.started":"2021-10-12T19:17:01.581621Z","shell.execute_reply":"2021-10-12T19:17:01.686166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Here we convert the x_train data to array to show the image**","metadata":{}},{"cell_type":"code","source":"new_x_train=np.array(x_train)","metadata":{"execution":{"iopub.status.busy":"2021-10-12T19:17:01.689002Z","iopub.execute_input":"2021-10-12T19:17:01.689403Z","iopub.status.idle":"2021-10-12T19:17:01.802877Z","shell.execute_reply.started":"2021-10-12T19:17:01.689355Z","shell.execute_reply":"2021-10-12T19:17:01.801862Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Here we show the first 25 photo of number and print the number in photo undeer the photo**","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10,10))\nfor i in range(25):\n    plt.subplot(5,5,i+1)\n    plt.xticks([])\n    plt.yticks([])\n   # plt.grid(False)\n    plt.imshow(new_x_train[i].reshape(28,28))\n    plt.xlabel(y_train[i])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-12T19:17:01.804566Z","iopub.execute_input":"2021-10-12T19:17:01.805052Z","iopub.status.idle":"2021-10-12T19:17:03.243680Z","shell.execute_reply.started":"2021-10-12T19:17:01.804990Z","shell.execute_reply":"2021-10-12T19:17:03.242463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Here we scale the data to make the process is faster**\nevery pixel in the photo contain a number from 0 to 255\nSo we divided each pixel by 255","metadata":{}},{"cell_type":"code","source":"new_x_train,x_test,x_train=new_x_train/255,x_test/255,x_train/255","metadata":{"execution":{"iopub.status.busy":"2021-10-12T19:17:03.245235Z","iopub.execute_input":"2021-10-12T19:17:03.246066Z","iopub.status.idle":"2021-10-12T19:17:03.594563Z","shell.execute_reply.started":"2021-10-12T19:17:03.246020Z","shell.execute_reply":"2021-10-12T19:17:03.593497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Here we knowe the label**","metadata":{}},{"cell_type":"code","source":"y_train.unique()","metadata":{"execution":{"iopub.status.busy":"2021-10-12T19:17:03.596493Z","iopub.execute_input":"2021-10-12T19:17:03.596895Z","iopub.status.idle":"2021-10-12T19:17:03.605548Z","shell.execute_reply.started":"2021-10-12T19:17:03.596836Z","shell.execute_reply":"2021-10-12T19:17:03.604430Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Here we built the nueral network**","metadata":{}},{"cell_type":"code","source":"model=tf.keras.models.Sequential([\n    tf.keras.layers.Flatten(input_shape=(28,28)),\n    tf.keras.layers.Dense(256,activation='relu'),\n    tf.keras.layers.Dense(128,activation='relu'),\n    tf.keras.layers.Dense(10)\n])","metadata":{"execution":{"iopub.status.busy":"2021-10-12T19:17:03.607585Z","iopub.execute_input":"2021-10-12T19:17:03.608301Z","iopub.status.idle":"2021-10-12T19:17:04.687144Z","shell.execute_reply.started":"2021-10-12T19:17:03.608252Z","shell.execute_reply":"2021-10-12T19:17:04.686160Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Here we determine the loss function**","metadata":{}},{"cell_type":"code","source":"loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)","metadata":{"execution":{"iopub.status.busy":"2021-10-12T19:17:04.691382Z","iopub.execute_input":"2021-10-12T19:17:04.691692Z","iopub.status.idle":"2021-10-12T19:17:04.697032Z","shell.execute_reply.started":"2021-10-12T19:17:04.691656Z","shell.execute_reply":"2021-10-12T19:17:04.695843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Here we compile the model**","metadata":{}},{"cell_type":"code","source":"model.compile(optimizer='adam',\n             loss=loss_fn,\n              metrics=['accuracy']\n             )","metadata":{"execution":{"iopub.status.busy":"2021-10-12T19:17:04.698881Z","iopub.execute_input":"2021-10-12T19:17:04.699226Z","iopub.status.idle":"2021-10-12T19:17:04.726044Z","shell.execute_reply.started":"2021-10-12T19:17:04.699179Z","shell.execute_reply":"2021-10-12T19:17:04.724807Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Here train the model**","metadata":{}},{"cell_type":"code","source":"model.fit(x_train,y_train,epochs=13)","metadata":{"execution":{"iopub.status.busy":"2021-10-12T19:17:04.730191Z","iopub.execute_input":"2021-10-12T19:17:04.730454Z","iopub.status.idle":"2021-10-12T19:17:52.240452Z","shell.execute_reply.started":"2021-10-12T19:17:04.730425Z","shell.execute_reply":"2021-10-12T19:17:52.239290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\ny_test=pd.read_csv(\"../input/digit-recognizer/sample_submission.csv\") ","metadata":{"execution":{"iopub.status.busy":"2021-10-12T19:17:52.242193Z","iopub.execute_input":"2021-10-12T19:17:52.242853Z","iopub.status.idle":"2021-10-12T19:17:52.259954Z","shell.execute_reply.started":"2021-10-12T19:17:52.242802Z","shell.execute_reply":"2021-10-12T19:17:52.258683Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Here we make predictions**","metadata":{}},{"cell_type":"code","source":"probability_model = tf.keras.Sequential([\n  model,\n  tf.keras.layers.Softmax()\n])","metadata":{"execution":{"iopub.status.busy":"2021-10-12T19:17:52.261569Z","iopub.execute_input":"2021-10-12T19:17:52.262040Z","iopub.status.idle":"2021-10-12T19:17:52.295768Z","shell.execute_reply.started":"2021-10-12T19:17:52.261979Z","shell.execute_reply":"2021-10-12T19:17:52.294751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test=np.array(x_test)\n\npre=probability_model.predict(x_test)\npre.shape","metadata":{"execution":{"iopub.status.busy":"2021-10-12T19:17:52.297620Z","iopub.execute_input":"2021-10-12T19:17:52.298003Z","iopub.status.idle":"2021-10-12T19:17:53.406469Z","shell.execute_reply.started":"2021-10-12T19:17:52.297961Z","shell.execute_reply":"2021-10-12T19:17:53.405386Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Here show the image of test data and show the prediction of every image under the image**","metadata":{}},{"cell_type":"code","source":"plt.figure(figsize=(10,10))\nfor i in range(25):\n    plt.subplot(5,5,i+1)\n    plt.xticks([])\n    plt.yticks([])\n   # plt.grid(False)\n    plt.imshow(x_test[i].reshape(28,28))\n    plt.xlabel(np.argmax(pre[i]))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-10-12T19:17:53.410267Z","iopub.execute_input":"2021-10-12T19:17:53.410517Z","iopub.status.idle":"2021-10-12T19:17:54.968330Z","shell.execute_reply.started":"2021-10-12T19:17:53.410488Z","shell.execute_reply":"2021-10-12T19:17:54.967032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Here we save the prediction**","metadata":{}},{"cell_type":"code","source":"listofprediction=[]\nfor i in range(len(pre)):\n    listofprediction.append(np.argmax(pre[i]))","metadata":{"execution":{"iopub.status.busy":"2021-10-12T19:17:54.970586Z","iopub.execute_input":"2021-10-12T19:17:54.971257Z","iopub.status.idle":"2021-10-12T19:17:55.082018Z","shell.execute_reply.started":"2021-10-12T19:17:54.971209Z","shell.execute_reply":"2021-10-12T19:17:55.080864Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"listofprediction=pd.DataFrame(listofprediction,columns=['Label'])","metadata":{"execution":{"iopub.status.busy":"2021-10-12T19:17:55.083621Z","iopub.execute_input":"2021-10-12T19:17:55.084087Z","iopub.status.idle":"2021-10-12T19:17:55.112876Z","shell.execute_reply.started":"2021-10-12T19:17:55.084028Z","shell.execute_reply":"2021-10-12T19:17:55.111786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test=y_test.drop(labels=['Label'],axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-10-12T19:17:55.115294Z","iopub.execute_input":"2021-10-12T19:17:55.115664Z","iopub.status.idle":"2021-10-12T19:17:55.123564Z","shell.execute_reply.started":"2021-10-12T19:17:55.115618Z","shell.execute_reply":"2021-10-12T19:17:55.122235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission=pd.concat([y_test['ImageId'], listofprediction['Label']], axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-10-12T19:17:55.126123Z","iopub.execute_input":"2021-10-12T19:17:55.126670Z","iopub.status.idle":"2021-10-12T19:17:55.137709Z","shell.execute_reply.started":"2021-10-12T19:17:55.126627Z","shell.execute_reply":"2021-10-12T19:17:55.136533Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2021-10-12T19:17:55.139828Z","iopub.execute_input":"2021-10-12T19:17:55.140270Z","iopub.status.idle":"2021-10-12T19:17:55.158252Z","shell.execute_reply.started":"2021-10-12T19:17:55.140226Z","shell.execute_reply":"2021-10-12T19:17:55.156825Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"mnist_datagen.csv\",index=False)","metadata":{"execution":{"iopub.status.busy":"2021-10-12T19:17:55.160349Z","iopub.execute_input":"2021-10-12T19:17:55.160697Z","iopub.status.idle":"2021-10-12T19:17:55.223259Z","shell.execute_reply.started":"2021-10-12T19:17:55.160654Z","shell.execute_reply":"2021-10-12T19:17:55.222140Z"},"trusted":true},"execution_count":null,"outputs":[]}]}