{"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":"# digital recognition with tensorflow","metadata":{}},{"cell_type":"markdown","source":"###  **Please upvote if you like my approach or if you learned something from this notebook. Your support gives me motivation to create interesting stuff. Thank you.** ","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# 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":"2021-12-17T15:18:07.311393Z","iopub.execute_input":"2021-12-17T15:18:07.311667Z","iopub.status.idle":"2021-12-17T15:18:07.321610Z","shell.execute_reply.started":"2021-12-17T15:18:07.311636Z","shell.execute_reply":"2021-12-17T15:18:07.321022Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# importing the libraries ","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.layers import LeakyReLU, Dense","metadata":{"execution":{"iopub.status.busy":"2021-12-17T15:18:07.323254Z","iopub.execute_input":"2021-12-17T15:18:07.323947Z","iopub.status.idle":"2021-12-17T15:18:07.331259Z","shell.execute_reply.started":"2021-12-17T15:18:07.323912Z","shell.execute_reply":"2021-12-17T15:18:07.330403Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Handwritten digits classification using neural network\nIn this notebook we will classify handwritten digits using a simple neural network which has only input and output layers. We will than add a hidden layer and see how the performance of the model improves\n\nafter reading the data we are going to normalize it by divid each pixel with 255","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')\ntrain.head()\ny = train.label\nx = train.drop(columns= 'label')\nx = x/255\nx.head()","metadata":{"execution":{"iopub.status.busy":"2021-12-17T15:18:07.332555Z","iopub.execute_input":"2021-12-17T15:18:07.332932Z","iopub.status.idle":"2021-12-17T15:18:11.934103Z","shell.execute_reply.started":"2021-12-17T15:18:07.332900Z","shell.execute_reply":"2021-12-17T15:18:11.933443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# train validate split ","metadata":{}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split \nx_train, x_val , y_train, y_val = train_test_split(\n    x,\n    y,\n    test_size=0.15,\n    random_state=42 )","metadata":{"execution":{"iopub.status.busy":"2021-12-17T15:18:11.935312Z","iopub.execute_input":"2021-12-17T15:18:11.935702Z","iopub.status.idle":"2021-12-17T15:18:12.147668Z","shell.execute_reply.started":"2021-12-17T15:18:11.935671Z","shell.execute_reply":"2021-12-17T15:18:12.146857Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"here we are going to use sklearn models to fit the data into a NN model that have **64 nuroal** in the frist hidden layer, **32 nuroal** in the second hidden layer, and **10 output** layers\n\n","metadata":{}},{"cell_type":"markdown","source":" <img src= \"https://camo.githubusercontent.com/521578c8e820dc0f662ea03366138a6e0468bf8cb1a4b2286024a0d0b524d54b/68747470733a2f2f6d6c34612e6769746875622e696f2f696d616765732f666967757265732f6d6e6973742d696e7075742e706e67\" alt =\"nuroal network \"  align=\"center\" style='width: 500px;' >\n","metadata":{}},{"cell_type":"markdown","source":"# creating the model ","metadata":{}},{"cell_type":"code","source":"\nmodel = keras.Sequential([\n    Dense(64, input_shape=(784,), activation='sigmoid'),\n    Dense(32, activation=LeakyReLU(alpha=0.3)),\n    Dense(32, activation=LeakyReLU(alpha=0.3)),\n\n    Dense(10, activation='softmax')\n\n])\n\nmodel.compile(optimizer='adam',\n              loss='sparse_categorical_crossentropy',\n              metrics=['accuracy'])\n\n","metadata":{"execution":{"iopub.status.busy":"2021-12-17T15:18:12.150151Z","iopub.execute_input":"2021-12-17T15:18:12.150383Z","iopub.status.idle":"2021-12-17T15:18:12.196101Z","shell.execute_reply.started":"2021-12-17T15:18:12.150354Z","shell.execute_reply":"2021-12-17T15:18:12.195336Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# model summary \n","metadata":{}},{"cell_type":"code","source":"model.summary()","metadata":{"execution":{"iopub.status.busy":"2021-12-17T15:18:12.197335Z","iopub.execute_input":"2021-12-17T15:18:12.197980Z","iopub.status.idle":"2021-12-17T15:18:12.206374Z","shell.execute_reply.started":"2021-12-17T15:18:12.197941Z","shell.execute_reply":"2021-12-17T15:18:12.205557Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# ploting the model ","metadata":{}},{"cell_type":"code","source":"keras.utils.plot_model(model, show_shapes=True)\n","metadata":{"execution":{"iopub.status.busy":"2021-12-17T15:18:12.207749Z","iopub.execute_input":"2021-12-17T15:18:12.208222Z","iopub.status.idle":"2021-12-17T15:18:12.346690Z","shell.execute_reply.started":"2021-12-17T15:18:12.208188Z","shell.execute_reply":"2021-12-17T15:18:12.346045Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# training the model using ","metadata":{}},{"cell_type":"code","source":"model.fit(\n    x= x_train,\n    y= y_train,\n    validation_data=(x_val, y_val),\n    workers=10,\n    epochs=10)","metadata":{"execution":{"iopub.status.busy":"2021-12-17T15:18:12.348297Z","iopub.execute_input":"2021-12-17T15:18:12.348740Z","iopub.status.idle":"2021-12-17T15:18:43.743659Z","shell.execute_reply.started":"2021-12-17T15:18:12.348707Z","shell.execute_reply":"2021-12-17T15:18:43.742878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# predicting the test values  ","metadata":{}},{"cell_type":"markdown","source":"","metadata":{}},{"cell_type":"code","source":"xtest = test/255\nypre= model.predict(xtest)\nypre","metadata":{"execution":{"iopub.status.busy":"2021-12-17T15:18:43.744797Z","iopub.execute_input":"2021-12-17T15:18:43.745070Z","iopub.status.idle":"2021-12-17T15:18:45.115191Z","shell.execute_reply.started":"2021-12-17T15:18:43.745039Z","shell.execute_reply":"2021-12-17T15:18:45.114352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# reading the submission file ","metadata":{}},{"cell_type":"code","source":"sample = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\nsample1 = sample.drop(columns= 'ImageId')\nsample1[:5]","metadata":{"execution":{"iopub.status.busy":"2021-12-17T15:18:45.116602Z","iopub.execute_input":"2021-12-17T15:18:45.116989Z","iopub.status.idle":"2021-12-17T15:18:45.137140Z","shell.execute_reply.started":"2021-12-17T15:18:45.116945Z","shell.execute_reply":"2021-12-17T15:18:45.136331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# transform the prediction matrix into values ","metadata":{}},{"cell_type":"code","source":"y_predicted = [np.argmax(i) for i in ypre]\ny_predicted[:5]","metadata":{"execution":{"iopub.status.busy":"2021-12-17T15:18:45.138566Z","iopub.execute_input":"2021-12-17T15:18:45.139106Z","iopub.status.idle":"2021-12-17T15:18:45.234038Z","shell.execute_reply.started":"2021-12-17T15:18:45.139061Z","shell.execute_reply":"2021-12-17T15:18:45.233182Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(len(sample1),'<-- sample prediction-->  ' ,  len(y_predicted))\n","metadata":{"execution":{"iopub.status.busy":"2021-12-17T15:18:45.235115Z","iopub.execute_input":"2021-12-17T15:18:45.235407Z","iopub.status.idle":"2021-12-17T15:18:45.240786Z","shell.execute_reply.started":"2021-12-17T15:18:45.235376Z","shell.execute_reply":"2021-12-17T15:18:45.239880Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# saving the prediction values ","metadata":{}},{"cell_type":"code","source":"y_predicted = pd.DataFrame(y_predicted)\nnewY = pd.concat([sample.drop(columns='Label'), y_predicted], axis = 1)","metadata":{"execution":{"iopub.status.busy":"2021-12-17T15:20:49.751336Z","iopub.execute_input":"2021-12-17T15:20:49.751646Z","iopub.status.idle":"2021-12-17T15:20:49.758272Z","shell.execute_reply.started":"2021-12-17T15:20:49.751616Z","shell.execute_reply":"2021-12-17T15:20:49.757472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"newY.columns=['ImageId', 'Label']\nnewY","metadata":{"execution":{"iopub.status.busy":"2021-12-17T15:18:45.278105Z","iopub.execute_input":"2021-12-17T15:18:45.278730Z","iopub.status.idle":"2021-12-17T15:18:45.289542Z","shell.execute_reply.started":"2021-12-17T15:18:45.278694Z","shell.execute_reply":"2021-12-17T15:18:45.288876Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# evaluate the model in test data","metadata":{}},{"cell_type":"code","source":"model.evaluate(xtest,y_predicted)\n","metadata":{"execution":{"iopub.status.busy":"2021-12-17T15:18:45.290787Z","iopub.execute_input":"2021-12-17T15:18:45.291231Z","iopub.status.idle":"2021-12-17T15:18:47.446332Z","shell.execute_reply.started":"2021-12-17T15:18:45.291199Z","shell.execute_reply":"2021-12-17T15:18:47.445535Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# save the values in CSV file","metadata":{}},{"cell_type":"code","source":"newY.to_csv('my_submission.csv', index= False)\n","metadata":{"execution":{"iopub.status.busy":"2021-12-17T15:18:47.448080Z","iopub.execute_input":"2021-12-17T15:18:47.448378Z","iopub.status.idle":"2021-12-17T15:18:47.506593Z","shell.execute_reply.started":"2021-12-17T15:18:47.448336Z","shell.execute_reply":"2021-12-17T15:18:47.505882Z"},"trusted":true},"execution_count":null,"outputs":[]}]}