{"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":"import tensorflow as tf\nfrom tensorflow import keras\nimport pandas as pd\nimport numpy as np\n%matplotlib inline\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:07:10.514850Z","iopub.execute_input":"2021-08-11T11:07:10.515209Z","iopub.status.idle":"2021-08-11T11:07:16.744957Z","shell.execute_reply.started":"2021-08-11T11:07:10.515131Z","shell.execute_reply":"2021-08-11T11:07:16.743989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/digit-recognizer/train.csv')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:07:16.746374Z","iopub.execute_input":"2021-08-11T11:07:16.746675Z","iopub.status.idle":"2021-08-11T11:07:19.929162Z","shell.execute_reply.started":"2021-08-11T11:07:16.746646Z","shell.execute_reply":"2021-08-11T11:07:19.928267Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = pd.read_csv('../input/digit-recognizer/test.csv')\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:07:19.931032Z","iopub.execute_input":"2021-08-11T11:07:19.931428Z","iopub.status.idle":"2021-08-11T11:07:21.633872Z","shell.execute_reply.started":"2021-08-11T11:07:19.931389Z","shell.execute_reply":"2021-08-11T11:07:21.632754Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:07:21.635339Z","iopub.execute_input":"2021-08-11T11:07:21.635622Z","iopub.status.idle":"2021-08-11T11:07:21.641661Z","shell.execute_reply.started":"2021-08-11T11:07:21.635594Z","shell.execute_reply":"2021-08-11T11:07:21.640603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:07:21.643331Z","iopub.execute_input":"2021-08-11T11:07:21.644088Z","iopub.status.idle":"2021-08-11T11:07:21.654519Z","shell.execute_reply.started":"2021-08-11T11:07:21.644034Z","shell.execute_reply":"2021-08-11T11:07:21.653380Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = train.drop(['label'],axis=1)\nX = np.array(x).reshape(len(train),28*28) / 255\nX.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:07:21.655910Z","iopub.execute_input":"2021-08-11T11:07:21.656199Z","iopub.status.idle":"2021-08-11T11:07:22.016596Z","shell.execute_reply.started":"2021-08-11T11:07:21.656171Z","shell.execute_reply":"2021-08-11T11:07:22.015646Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = train['label']\ny.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:07:22.017655Z","iopub.execute_input":"2021-08-11T11:07:22.017953Z","iopub.status.idle":"2021-08-11T11:07:22.026593Z","shell.execute_reply.started":"2021-08-11T11:07:22.017927Z","shell.execute_reply":"2021-08-11T11:07:22.025199Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test = np.array(test).reshape(len(test),28*28) / 255\nx_test.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:07:22.029049Z","iopub.execute_input":"2021-08-11T11:07:22.029328Z","iopub.status.idle":"2021-08-11T11:07:22.179421Z","shell.execute_reply.started":"2021-08-11T11:07:22.029301Z","shell.execute_reply":"2021-08-11T11:07:22.178477Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train, X_val, y_train, y_val = train_test_split(X,y,test_size=0.15,random_state=42)","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:07:22.182773Z","iopub.execute_input":"2021-08-11T11:07:22.183045Z","iopub.status.idle":"2021-08-11T11:07:22.747983Z","shell.execute_reply.started":"2021-08-11T11:07:22.183020Z","shell.execute_reply":"2021-08-11T11:07:22.747049Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape, y_train.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:07:22.749141Z","iopub.execute_input":"2021-08-11T11:07:22.749395Z","iopub.status.idle":"2021-08-11T11:07:22.755352Z","shell.execute_reply.started":"2021-08-11T11:07:22.749363Z","shell.execute_reply":"2021-08-11T11:07:22.754411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_val.shape, y_val.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:07:22.756514Z","iopub.execute_input":"2021-08-11T11:07:22.756894Z","iopub.status.idle":"2021-08-11T11:07:22.769159Z","shell.execute_reply.started":"2021-08-11T11:07:22.756866Z","shell.execute_reply":"2021-08-11T11:07:22.768056Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X[2].reshape(28,28))","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:07:22.770627Z","iopub.execute_input":"2021-08-11T11:07:22.770935Z","iopub.status.idle":"2021-08-11T11:07:22.934900Z","shell.execute_reply.started":"2021-08-11T11:07:22.770904Z","shell.execute_reply":"2021-08-11T11:07:22.933769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y[0]","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:07:22.936051Z","iopub.execute_input":"2021-08-11T11:07:22.936309Z","iopub.status.idle":"2021-08-11T11:07:22.942062Z","shell.execute_reply.started":"2021-08-11T11:07:22.936275Z","shell.execute_reply":"2021-08-11T11:07:22.941085Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Simple neural network with no hidden layers","metadata":{}},{"cell_type":"code","source":"ann =keras.Sequential([\n     keras.layers.Dense(10, input_shape=(784,),activation='sigmoid')\n])\nann.compile(\n             optimizer='adam',\n             loss='sparse_categorical_crossentropy',\n             metrics=['accuracy'])\nann.fit(X_train,y_train,epochs=5)","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:07:22.943326Z","iopub.execute_input":"2021-08-11T11:07:22.943683Z","iopub.status.idle":"2021-08-11T11:07:29.536212Z","shell.execute_reply.started":"2021-08-11T11:07:22.943654Z","shell.execute_reply":"2021-08-11T11:07:29.535346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ann.evaluate(X_val,y_val)","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:07:29.537291Z","iopub.execute_input":"2021-08-11T11:07:29.537546Z","iopub.status.idle":"2021-08-11T11:07:29.852136Z","shell.execute_reply.started":"2021-08-11T11:07:29.537509Z","shell.execute_reply":"2021-08-11T11:07:29.851410Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_predict = ann.predict(X_val)","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:07:29.853135Z","iopub.execute_input":"2021-08-11T11:07:29.853590Z","iopub.status.idle":"2021-08-11T11:07:30.056364Z","shell.execute_reply.started":"2021-08-11T11:07:29.853559Z","shell.execute_reply":"2021-08-11T11:07:30.055547Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_predict","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:07:30.057477Z","iopub.execute_input":"2021-08-11T11:07:30.057763Z","iopub.status.idle":"2021-08-11T11:07:30.063780Z","shell.execute_reply.started":"2021-08-11T11:07:30.057737Z","shell.execute_reply":"2021-08-11T11:07:30.063154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_predicted_labels = [np.argmax(i) for i in y_predict]\ny_predicted_labels[0]","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:07:30.064635Z","iopub.execute_input":"2021-08-11T11:07:30.064971Z","iopub.status.idle":"2021-08-11T11:07:30.084540Z","shell.execute_reply.started":"2021-08-11T11:07:30.064946Z","shell.execute_reply":"2021-08-11T11:07:30.083787Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cn = tf.math.confusion_matrix(y_val,y_predicted_labels)\nplt.figure(figsize=(8,6))\nsns.heatmap(cn,annot=True,fmt='d')\nplt.xlabel('Predicted')\nplt.ylabel('Truth')","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:07:30.085520Z","iopub.execute_input":"2021-08-11T11:07:30.085771Z","iopub.status.idle":"2021-08-11T11:07:30.727246Z","shell.execute_reply.started":"2021-08-11T11:07:30.085748Z","shell.execute_reply":"2021-08-11T11:07:30.726354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Using hidden layer","metadata":{}},{"cell_type":"code","source":"ann_hidden =keras.Sequential([\n    keras.layers.Dense(100, input_shape=(784,), activation='relu'),\n    keras.layers.Dense(10,activation='sigmoid')\n])\nann_hidden.compile(\n             optimizer='adam',\n             loss='sparse_categorical_crossentropy',\n             metrics=['accuracy'])\nann_hidden.fit(X_train,y_train,epochs=5)","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:07:30.728523Z","iopub.execute_input":"2021-08-11T11:07:30.728890Z","iopub.status.idle":"2021-08-11T11:07:39.557846Z","shell.execute_reply.started":"2021-08-11T11:07:30.728859Z","shell.execute_reply":"2021-08-11T11:07:39.556901Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ann_hidden.evaluate(X_val,y_val)","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:07:39.558993Z","iopub.execute_input":"2021-08-11T11:07:39.559251Z","iopub.status.idle":"2021-08-11T11:07:39.898452Z","shell.execute_reply.started":"2021-08-11T11:07:39.559227Z","shell.execute_reply":"2021-08-11T11:07:39.897554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_predict = ann_hidden.predict(X_val)\ny_predict","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:07:39.899729Z","iopub.execute_input":"2021-08-11T11:07:39.900099Z","iopub.status.idle":"2021-08-11T11:07:40.108242Z","shell.execute_reply.started":"2021-08-11T11:07:39.900062Z","shell.execute_reply":"2021-08-11T11:07:40.107166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_predicted_labels = [np.argmax(i) for i in y_predict]\ny_predicted_labels[2]","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:07:40.111067Z","iopub.execute_input":"2021-08-11T11:07:40.111357Z","iopub.status.idle":"2021-08-11T11:07:40.131736Z","shell.execute_reply.started":"2021-08-11T11:07:40.111328Z","shell.execute_reply":"2021-08-11T11:07:40.130908Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cn = tf.math.confusion_matrix(y_val,y_predicted_labels)\nplt.figure(figsize=(8,6))\nsns.heatmap(cn, annot=True, fmt='d')\nplt.xlabel('Predicted')\nplt.ylabel('Truth')","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:07:40.133104Z","iopub.execute_input":"2021-08-11T11:07:40.133352Z","iopub.status.idle":"2021-08-11T11:07:40.763145Z","shell.execute_reply.started":"2021-08-11T11:07:40.133328Z","shell.execute_reply":"2021-08-11T11:07:40.762219Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Using Flatten Layer","metadata":{}},{"cell_type":"code","source":"ann_flat = keras.Sequential([\n           keras.layers.Flatten(input_shape=(28,28)),\n           keras.layers.Dense(100, activation='relu'),\n           keras.layers.Dense(10, activation='sigmoid')\n])\nann_flat.compile(\n             optimizer='adam',\n             loss='sparse_categorical_crossentropy',\n             metrics=['accuracy'])\nann_flat.fit(X_train,y_train,epochs=10)","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:07:40.764331Z","iopub.execute_input":"2021-08-11T11:07:40.764617Z","iopub.status.idle":"2021-08-11T11:07:58.420946Z","shell.execute_reply.started":"2021-08-11T11:07:40.764589Z","shell.execute_reply":"2021-08-11T11:07:58.420052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ann_flat.evaluate(X_val,y_val)","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:07:58.421968Z","iopub.execute_input":"2021-08-11T11:07:58.422226Z","iopub.status.idle":"2021-08-11T11:07:58.770034Z","shell.execute_reply.started":"2021-08-11T11:07:58.422201Z","shell.execute_reply":"2021-08-11T11:07:58.769166Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_predict = ann_hidden.predict(x_test)\ny_predict","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:07:58.771312Z","iopub.execute_input":"2021-08-11T11:07:58.771602Z","iopub.status.idle":"2021-08-11T11:07:59.863407Z","shell.execute_reply.started":"2021-08-11T11:07:58.771569Z","shell.execute_reply":"2021-08-11T11:07:59.862452Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_predicted_labels = [np.argmax(i) for i in y_predict]\ny_predicted_labels[2]","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:07:59.864585Z","iopub.execute_input":"2021-08-11T11:07:59.864860Z","iopub.status.idle":"2021-08-11T11:07:59.926038Z","shell.execute_reply.started":"2021-08-11T11:07:59.864833Z","shell.execute_reply":"2021-08-11T11:07:59.925062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Using CNN","metadata":{}},{"cell_type":"code","source":"cnn = keras.Sequential([\n    keras.layers.Conv2D(30, (3,3), activation='relu', input_shape=(28, 28, 1)),\n    keras.layers.MaxPooling2D((2,2)),\n \n    keras.layers.Flatten(),\n    keras.layers.Dense(100, activation='relu'),\n    keras.layers.Dense(10, activation='sigmoid')\n])","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:07:59.927318Z","iopub.execute_input":"2021-08-11T11:07:59.927734Z","iopub.status.idle":"2021-08-11T11:07:59.978769Z","shell.execute_reply.started":"2021-08-11T11:07:59.927695Z","shell.execute_reply":"2021-08-11T11:07:59.977679Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn.compile(\n             optimizer='adam',\n             loss='sparse_categorical_crossentropy',\n             metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:07:59.980466Z","iopub.execute_input":"2021-08-11T11:07:59.980929Z","iopub.status.idle":"2021-08-11T11:07:59.992169Z","shell.execute_reply.started":"2021-08-11T11:07:59.980893Z","shell.execute_reply":"2021-08-11T11:07:59.991154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = X_train.reshape(-1,28,28,1)\nX_train.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:07:59.993682Z","iopub.execute_input":"2021-08-11T11:07:59.994094Z","iopub.status.idle":"2021-08-11T11:08:00.006022Z","shell.execute_reply.started":"2021-08-11T11:07:59.994053Z","shell.execute_reply":"2021-08-11T11:08:00.004945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test = x_test.reshape(-1,28,28,1)\nx_test.shape","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:08:00.007467Z","iopub.execute_input":"2021-08-11T11:08:00.007777Z","iopub.status.idle":"2021-08-11T11:08:00.017945Z","shell.execute_reply.started":"2021-08-11T11:08:00.007741Z","shell.execute_reply":"2021-08-11T11:08:00.017039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn.fit(X_train, y_train, epochs=10)","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:09:11.506436Z","iopub.execute_input":"2021-08-11T11:09:11.506983Z","iopub.status.idle":"2021-08-11T11:10:58.231028Z","shell.execute_reply.started":"2021-08-11T11:09:11.506950Z","shell.execute_reply":"2021-08-11T11:10:58.230268Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_predict_final = cnn.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:11:02.700583Z","iopub.execute_input":"2021-08-11T11:11:02.700903Z","iopub.status.idle":"2021-08-11T11:11:05.786056Z","shell.execute_reply.started":"2021-08-11T11:11:02.700875Z","shell.execute_reply":"2021-08-11T11:11:05.785144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_predicted_labels_final =[np.argmax(i) for i in y_predict_final]","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:11:07.655640Z","iopub.execute_input":"2021-08-11T11:11:07.655983Z","iopub.status.idle":"2021-08-11T11:11:07.715132Z","shell.execute_reply.started":"2021-08-11T11:11:07.655954Z","shell.execute_reply":"2021-08-11T11:11:07.714144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_id = range(1, y_predict_final.shape[0] + 1)\nsubmissions = pd.DataFrame({'ImageId':image_id, 'Label':y_predicted_labels_final})\nsubmissions.to_csv('digit_recognizer_submission_final',index=False)","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:11:10.020386Z","iopub.execute_input":"2021-08-11T11:11:10.020756Z","iopub.status.idle":"2021-08-11T11:11:10.068268Z","shell.execute_reply.started":"2021-08-11T11:11:10.020724Z","shell.execute_reply":"2021-08-11T11:11:10.067513Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submissions.head()","metadata":{"execution":{"iopub.status.busy":"2021-08-11T11:11:12.895584Z","iopub.execute_input":"2021-08-11T11:11:12.896083Z","iopub.status.idle":"2021-08-11T11:11:12.905130Z","shell.execute_reply.started":"2021-08-11T11:11:12.896037Z","shell.execute_reply":"2021-08-11T11:11:12.904419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}