{"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-03T13:59:47.060164Z","iopub.execute_input":"2022-12-03T13:59:47.061084Z","iopub.status.idle":"2022-12-03T13:59:47.091551Z","shell.execute_reply.started":"2022-12-03T13:59:47.060983Z","shell.execute_reply":"2022-12-03T13:59:47.090211Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport matplotlib.pyplot as plt","metadata":{"execution":{"iopub.status.busy":"2022-12-03T13:59:53.286397Z","iopub.execute_input":"2022-12-03T13:59:53.286853Z","iopub.status.idle":"2022-12-03T13:59:59.332219Z","shell.execute_reply.started":"2022-12-03T13:59:53.286817Z","shell.execute_reply":"2022-12-03T13:59:59.330797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Import of libraries\nfrom tensorflow import keras\nfrom tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Dense,Flatten","metadata":{"execution":{"iopub.status.busy":"2022-12-03T14:00:01.159925Z","iopub.execute_input":"2022-12-03T14:00:01.160652Z","iopub.status.idle":"2022-12-03T14:00:02.524569Z","shell.execute_reply.started":"2022-12-03T14:00:01.160608Z","shell.execute_reply":"2022-12-03T14:00:02.523494Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Load Dataset\nmnist_train=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"execution":{"iopub.status.busy":"2022-12-03T14:00:12.320657Z","iopub.execute_input":"2022-12-03T14:00:12.321078Z","iopub.status.idle":"2022-12-03T14:00:16.303441Z","shell.execute_reply.started":"2022-12-03T14:00:12.321046Z","shell.execute_reply":"2022-12-03T14:00:16.302074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Converted DataFrame to Array\ny = mnist_train['label'].values  \nX = mnist_train.drop(columns= 'label').values","metadata":{"execution":{"iopub.status.busy":"2022-12-03T14:00:23.512508Z","iopub.execute_input":"2022-12-03T14:00:23.513400Z","iopub.status.idle":"2022-12-03T14:00:23.688288Z","shell.execute_reply.started":"2022-12-03T14:00:23.513323Z","shell.execute_reply":"2022-12-03T14:00:23.686627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X","metadata":{"execution":{"iopub.status.busy":"2022-12-03T14:00:34.305581Z","iopub.execute_input":"2022-12-03T14:00:34.306023Z","iopub.status.idle":"2022-12-03T14:00:34.317391Z","shell.execute_reply.started":"2022-12-03T14:00:34.305981Z","shell.execute_reply":"2022-12-03T14:00:34.315852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Reshaped array to 28x28 array\n\nfrom sklearn.model_selection import train_test_split\nX = X.reshape(-1,28,28,1) # reshape into a format that can be fed into our NN\n\nX = X / 255 # normalize our training data\n\n#Split Data\nX_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=40)","metadata":{"execution":{"iopub.status.busy":"2022-12-03T14:00:47.249556Z","iopub.execute_input":"2022-12-03T14:00:47.250016Z","iopub.status.idle":"2022-12-03T14:00:48.433456Z","shell.execute_reply.started":"2022-12-03T14:00:47.249980Z","shell.execute_reply":"2022-12-03T14:00:48.432239Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Model Building\nmodel = Sequential()","metadata":{"execution":{"iopub.status.busy":"2022-12-03T14:01:03.445372Z","iopub.execute_input":"2022-12-03T14:01:03.445836Z","iopub.status.idle":"2022-12-03T14:01:03.509210Z","shell.execute_reply.started":"2022-12-03T14:01:03.445800Z","shell.execute_reply":"2022-12-03T14:01:03.508264Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Adding layers\nmodel.add(Flatten(input_shape = (28,28)))\nmodel.add(Dense(128, activation = 'relu'))\nmodel.add(Dense(10, activation = 'softmax'))","metadata":{"execution":{"iopub.status.busy":"2022-12-03T14:01:13.962844Z","iopub.execute_input":"2022-12-03T14:01:13.963241Z","iopub.status.idle":"2022-12-03T14:01:14.040883Z","shell.execute_reply.started":"2022-12-03T14:01:13.963211Z","shell.execute_reply":"2022-12-03T14:01:14.039760Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Complied model with optimizer \nmodel.compile(loss = 'sparse_categorical_crossentropy', optimizer = 'rmsprop',metrics= ['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-12-03T14:01:25.743091Z","iopub.execute_input":"2022-12-03T14:01:25.743476Z","iopub.status.idle":"2022-12-03T14:01:25.761051Z","shell.execute_reply.started":"2022-12-03T14:01:25.743446Z","shell.execute_reply":"2022-12-03T14:01:25.759620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Model Fitting\nvalidation_set=(X_val,y_val)\nhistory = model.fit(X_train,y_train,epochs=20,validation_data=validation_set)","metadata":{"execution":{"iopub.status.busy":"2022-12-03T14:01:37.153499Z","iopub.execute_input":"2022-12-03T14:01:37.154429Z","iopub.status.idle":"2022-12-03T14:03:01.559255Z","shell.execute_reply.started":"2022-12-03T14:01:37.154381Z","shell.execute_reply":"2022-12-03T14:03:01.557920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Loading Test Data\n#Did Preprocessing\n\nmnist_test=pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\nX_test = mnist_test.values\nfrom sklearn.model_selection import train_test_split\nX_test = X_test.reshape(-1,28,28,1) # reshape into a format that can be fed into our NN\nX_test = X_test / 255 # normalize our training data","metadata":{"execution":{"iopub.status.busy":"2022-12-03T14:03:10.616806Z","iopub.execute_input":"2022-12-03T14:03:10.617202Z","iopub.status.idle":"2022-12-03T14:03:12.863574Z","shell.execute_reply.started":"2022-12-03T14:03:10.617171Z","shell.execute_reply":"2022-12-03T14:03:12.862156Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test","metadata":{"execution":{"iopub.status.busy":"2022-12-03T14:03:26.634892Z","iopub.execute_input":"2022-12-03T14:03:26.635315Z","iopub.status.idle":"2022-12-03T14:03:26.647202Z","shell.execute_reply.started":"2022-12-03T14:03:26.635279Z","shell.execute_reply":"2022-12-03T14:03:26.645924Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=model.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2022-12-03T14:03:51.153726Z","iopub.execute_input":"2022-12-03T14:03:51.154136Z","iopub.status.idle":"2022-12-03T14:03:52.499040Z","shell.execute_reply.started":"2022-12-03T14:03:51.154105Z","shell.execute_reply":"2022-12-03T14:03:52.497812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Finalizing value with highest Probablity\ny_pred = y_pred.argmax(axis = 1)","metadata":{"execution":{"iopub.status.busy":"2022-12-03T14:04:06.513156Z","iopub.execute_input":"2022-12-03T14:04:06.513582Z","iopub.status.idle":"2022-12-03T14:04:06.519104Z","shell.execute_reply.started":"2022-12-03T14:04:06.513548Z","shell.execute_reply":"2022-12-03T14:04:06.518042Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-03T14:04:16.463275Z","iopub.execute_input":"2022-12-03T14:04:16.463674Z","iopub.status.idle":"2022-12-03T14:04:16.470705Z","shell.execute_reply.started":"2022-12-03T14:04:16.463642Z","shell.execute_reply":"2022-12-03T14:04:16.469793Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-12-03T14:04:31.829361Z","iopub.execute_input":"2022-12-03T14:04:31.829759Z","iopub.status.idle":"2022-12-03T14:04:31.837696Z","shell.execute_reply.started":"2022-12-03T14:04:31.829727Z","shell.execute_reply":"2022-12-03T14:04:31.836486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Creating prediction dataframe\nsubmission=pd.DataFrame()\nsubmission['ImageId'] = list(mnist_test.index)\nsubmission['ImageId'] = submission['ImageId'].apply(lambda x: x+1)\nsubmission['Label'] =y_pred","metadata":{"execution":{"iopub.status.busy":"2022-12-03T14:04:48.388873Z","iopub.execute_input":"2022-12-03T14:04:48.389303Z","iopub.status.idle":"2022-12-03T14:04:48.436285Z","shell.execute_reply.started":"2022-12-03T14:04:48.389266Z","shell.execute_reply":"2022-12-03T14:04:48.435010Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission","metadata":{"execution":{"iopub.status.busy":"2022-12-03T14:04:58.051343Z","iopub.execute_input":"2022-12-03T14:04:58.051881Z","iopub.status.idle":"2022-12-03T14:04:58.070066Z","shell.execute_reply.started":"2022-12-03T14:04:58.051838Z","shell.execute_reply":"2022-12-03T14:04:58.068865Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Saving file\nsubmission.to_csv('submission.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2022-12-03T14:05:11.011382Z","iopub.execute_input":"2022-12-03T14:05:11.011761Z","iopub.status.idle":"2022-12-03T14:05:11.049633Z","shell.execute_reply.started":"2022-12-03T14:05:11.011708Z","shell.execute_reply":"2022-12-03T14:05:11.048197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}