{"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":"# FFNN","metadata":{}},{"cell_type":"code","source":"# Importing necessary libraries\nimport pandas as pd\nimport numpy as np\nfrom tensorflow import keras\nfrom keras.layers import Dense, MaxPool2D, Conv2D, Dropout,Flatten\nfrom keras.models import Sequential\nfrom keras.utils import np_utils\nfrom keras.datasets import mnist\nfrom sklearn import metrics\nimport numpy\nfrom matplotlib import pyplot as plt\n\nimport tensorflow as  tf\nfrom keras.preprocessing.image import ImageDataGenerator","metadata":{"execution":{"iopub.status.busy":"2022-03-12T02:48:30.871216Z","iopub.execute_input":"2022-03-12T02:48:30.871596Z","iopub.status.idle":"2022-03-12T02:48:36.365337Z","shell.execute_reply.started":"2022-03-12T02:48:30.871506Z","shell.execute_reply":"2022-03-12T02:48:36.364588Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset = pd.read_csv('../input/digit-recognizer/train.csv').values\ntest_dataset_X = pd.read_csv('../input/digit-recognizer/test.csv').values\ntest_dataset_y = pd.read_csv('../input/digit-recognizer/sample_submission.csv').values","metadata":{"execution":{"iopub.status.busy":"2022-03-12T02:48:36.367037Z","iopub.execute_input":"2022-03-12T02:48:36.367303Z","iopub.status.idle":"2022-03-12T02:48:41.09476Z","shell.execute_reply.started":"2022-03-12T02:48:36.367267Z","shell.execute_reply":"2022-03-12T02:48:41.094071Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_dataset","metadata":{"execution":{"iopub.status.busy":"2022-03-12T02:48:41.09617Z","iopub.execute_input":"2022-03-12T02:48:41.096426Z","iopub.status.idle":"2022-03-12T02:48:41.105745Z","shell.execute_reply.started":"2022-03-12T02:48:41.09639Z","shell.execute_reply":"2022-03-12T02:48:41.104998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset_X","metadata":{"execution":{"iopub.status.busy":"2022-03-12T02:48:41.108399Z","iopub.execute_input":"2022-03-12T02:48:41.109054Z","iopub.status.idle":"2022-03-12T02:48:41.115939Z","shell.execute_reply.started":"2022-03-12T02:48:41.109017Z","shell.execute_reply":"2022-03-12T02:48:41.115081Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset_y","metadata":{"execution":{"iopub.status.busy":"2022-03-12T02:48:41.117177Z","iopub.execute_input":"2022-03-12T02:48:41.117497Z","iopub.status.idle":"2022-03-12T02:48:41.125014Z","shell.execute_reply.started":"2022-03-12T02:48:41.117461Z","shell.execute_reply":"2022-03-12T02:48:41.124165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_dataset.shape)\nprint(test_dataset_X.shape)\nprint(test_dataset_y.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-12T02:48:41.126493Z","iopub.execute_input":"2022-03-12T02:48:41.126754Z","iopub.status.idle":"2022-03-12T02:48:41.133575Z","shell.execute_reply.started":"2022-03-12T02:48:41.126719Z","shell.execute_reply":"2022-03-12T02:48:41.132854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"col = test_dataset_y[:,1]\ncol.shape = (len(col),1)","metadata":{"execution":{"iopub.status.busy":"2022-03-12T02:48:41.134909Z","iopub.execute_input":"2022-03-12T02:48:41.135378Z","iopub.status.idle":"2022-03-12T02:48:41.141604Z","shell.execute_reply.started":"2022-03-12T02:48:41.135343Z","shell.execute_reply":"2022-03-12T02:48:41.140818Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_dataset = np.hstack((test_dataset_X[:,:1], col, test_dataset_X[:,1:]))","metadata":{"execution":{"iopub.status.busy":"2022-03-12T02:48:41.143069Z","iopub.execute_input":"2022-03-12T02:48:41.14334Z","iopub.status.idle":"2022-03-12T02:48:41.212607Z","shell.execute_reply.started":"2022-03-12T02:48:41.143305Z","shell.execute_reply":"2022-03-12T02:48:41.211826Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = train_dataset[:, 1:].reshape(train_dataset.shape[0],28,28, 1).astype( 'float32' )\nX_train = X_train / 255.0\n\ny_train = train_dataset[:,0]\n\nX_test = test_dataset[:, 1:].reshape(test_dataset.shape[0],28,28, 1).astype( 'float32' )\nX_test = X_test / 255.0\n\ny_test = test_dataset[:,0]","metadata":{"execution":{"iopub.status.busy":"2022-03-12T02:48:41.214139Z","iopub.execute_input":"2022-03-12T02:48:41.214396Z","iopub.status.idle":"2022-03-12T02:48:41.381226Z","shell.execute_reply.started":"2022-03-12T02:48:41.214358Z","shell.execute_reply":"2022-03-12T02:48:41.380477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(X_train.shape)\nprint(X_test.shape)\nprint(y_train.shape)\nprint(y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-12T02:48:41.383937Z","iopub.execute_input":"2022-03-12T02:48:41.384236Z","iopub.status.idle":"2022-03-12T02:48:41.390493Z","shell.execute_reply.started":"2022-03-12T02:48:41.384199Z","shell.execute_reply":"2022-03-12T02:48:41.389802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# y_train = np_utils.to_categorical(y_train)\n# y_test_copy = y_test                        #for future use\n# y_test = np_utils.to_categorical(y_test, num_classes = 10)\n# print(y_train.shape)\n# print(y_test.shape)","metadata":{"execution":{"iopub.status.busy":"2022-03-12T02:48:41.391925Z","iopub.execute_input":"2022-03-12T02:48:41.392414Z","iopub.status.idle":"2022-03-12T02:48:41.397596Z","shell.execute_reply.started":"2022-03-12T02:48:41.392378Z","shell.execute_reply":"2022-03-12T02:48:41.396858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=tf.keras.models.Sequential()\nmodel.add(tf.keras.layers.Flatten(input_shape=(28,28,1)))\nmodel.add(tf.keras.layers.Dense(512,activation='relu'))\nmodel.add(tf.keras.layers.Dense(256,activation='relu'))\nmodel.add(tf.keras.layers.Dense(128,activation='relu'))\nmodel.add(tf.keras.layers.Dense(64,activation='relu'))\nmodel.add(tf.keras.layers.Dense(32,activation='relu'))\nmodel.add(tf.keras.layers.Dense(16,activation='relu'))\nmodel.add(tf.keras.layers.Dense(10,activation='softmax'))\nmodel.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])\nmodel.fit(x=X_train,y=y_train,batch_size=20,epochs=100)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2022-03-12T02:48:41.398886Z","iopub.execute_input":"2022-03-12T02:48:41.399204Z","iopub.status.idle":"2022-03-12T02:57:06.67809Z","shell.execute_reply.started":"2022-03-12T02:48:41.399168Z","shell.execute_reply":"2022-03-12T02:57:06.677389Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(X_test)\ny_pred = y_pred.argmax(axis=-1)\ny_pred","metadata":{"execution":{"iopub.status.busy":"2022-03-12T02:57:06.67948Z","iopub.execute_input":"2022-03-12T02:57:06.679847Z","iopub.status.idle":"2022-03-12T02:57:08.254688Z","shell.execute_reply.started":"2022-03-12T02:57:06.679801Z","shell.execute_reply":"2022-03-12T02:57:08.254012Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#SUBMITTING PREDICTIONS\nsub=pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv', header='infer')\nsub[\"Label\"]=y_pred\nsub.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-03-12T02:57:08.255835Z","iopub.execute_input":"2022-03-12T02:57:08.256089Z","iopub.status.idle":"2022-03-12T02:57:08.322927Z","shell.execute_reply.started":"2022-03-12T02:57:08.256051Z","shell.execute_reply":"2022-03-12T02:57:08.322219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# prediction = model.predict(X_test)\n# prediction = prediction.argmax(axis=-1)\n# prediction","metadata":{"execution":{"iopub.status.busy":"2022-03-12T02:57:08.324119Z","iopub.execute_input":"2022-03-12T02:57:08.324369Z","iopub.status.idle":"2022-03-12T02:57:08.329708Z","shell.execute_reply.started":"2022-03-12T02:57:08.324333Z","shell.execute_reply":"2022-03-12T02:57:08.328249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# print(\"accuracy score for the MNIST classification of the dataset is :\",metrics.accuracy_score(y_test, prediction, normalize = True))\n# print(metrics.classification_report(y_test, prediction))\n# print(metrics.confusion_matrix(y_test, prediction))","metadata":{"execution":{"iopub.status.busy":"2022-03-12T02:57:08.331035Z","iopub.execute_input":"2022-03-12T02:57:08.331337Z","iopub.status.idle":"2022-03-12T02:57:08.338664Z","shell.execute_reply.started":"2022-03-12T02:57:08.331298Z","shell.execute_reply":"2022-03-12T02:57:08.33794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# output = pd.DataFrame({\"ImageId\": [i for i in range(1,len(prediction)+1)], \"Label\": prediction})\n# print(output)\n# output.to_csv('19BCE245_DL_FFNN.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-03-12T02:57:08.339922Z","iopub.execute_input":"2022-03-12T02:57:08.340423Z","iopub.status.idle":"2022-03-12T02:57:08.347949Z","shell.execute_reply.started":"2022-03-12T02:57:08.340387Z","shell.execute_reply":"2022-03-12T02:57:08.347128Z"},"trusted":true},"execution_count":null,"outputs":[]}]}