{"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 numpy as np \nimport pandas as pd \nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\nimport tensorflow as tf\nfrom tensorflow import keras\n%matplotlib inline\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2021-09-02T13:20:56.647926Z","iopub.execute_input":"2021-09-02T13:20:56.648390Z","iopub.status.idle":"2021-09-02T13:21:03.267968Z","shell.execute_reply.started":"2021-09-02T13:20:56.648292Z","shell.execute_reply":"2021-09-02T13:21:03.266713Z"},"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-09-02T13:21:11.551606Z","iopub.execute_input":"2021-09-02T13:21:11.551984Z","iopub.status.idle":"2021-09-02T13:21:15.312956Z","shell.execute_reply.started":"2021-09-02T13:21:11.551949Z","shell.execute_reply":"2021-09-02T13:21:15.312148Z"},"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-09-02T13:21:22.948963Z","iopub.execute_input":"2021-09-02T13:21:22.949485Z","iopub.status.idle":"2021-09-02T13:21:25.135508Z","shell.execute_reply.started":"2021-09-02T13:21:22.949439Z","shell.execute_reply":"2021-09-02T13:21:25.134733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-02T13:21:33.468223Z","iopub.execute_input":"2021-09-02T13:21:33.468583Z","iopub.status.idle":"2021-09-02T13:21:33.474604Z","shell.execute_reply.started":"2021-09-02T13:21:33.468554Z","shell.execute_reply":"2021-09-02T13:21:33.473549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-02T13:21:50.288657Z","iopub.execute_input":"2021-09-02T13:21:50.289151Z","iopub.status.idle":"2021-09-02T13:21:50.294965Z","shell.execute_reply.started":"2021-09-02T13:21:50.289112Z","shell.execute_reply":"2021-09-02T13:21:50.294031Z"},"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-09-02T13:22:00.882085Z","iopub.execute_input":"2021-09-02T13:22:00.882460Z","iopub.status.idle":"2021-09-02T13:22:01.173305Z","shell.execute_reply.started":"2021-09-02T13:22:00.882426Z","shell.execute_reply":"2021-09-02T13:22:01.172312Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = train['label']\ny.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-02T13:22:11.410841Z","iopub.execute_input":"2021-09-02T13:22:11.411256Z","iopub.status.idle":"2021-09-02T13:22:11.417621Z","shell.execute_reply.started":"2021-09-02T13:22:11.411222Z","shell.execute_reply":"2021-09-02T13:22:11.416865Z"},"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-09-02T13:22:29.299657Z","iopub.execute_input":"2021-09-02T13:22:29.300270Z","iopub.status.idle":"2021-09-02T13:22:29.440766Z","shell.execute_reply.started":"2021-09-02T13:22:29.300219Z","shell.execute_reply":"2021-09-02T13:22:29.439778Z"},"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-09-02T13:23:07.954649Z","iopub.execute_input":"2021-09-02T13:23:07.955096Z","iopub.status.idle":"2021-09-02T13:23:08.533663Z","shell.execute_reply.started":"2021-09-02T13:23:07.955060Z","shell.execute_reply":"2021-09-02T13:23:08.532692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape, y_train.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-02T13:23:19.898775Z","iopub.execute_input":"2021-09-02T13:23:19.899140Z","iopub.status.idle":"2021-09-02T13:23:19.905348Z","shell.execute_reply.started":"2021-09-02T13:23:19.899109Z","shell.execute_reply":"2021-09-02T13:23:19.904138Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_val.shape, y_val.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-02T13:23:27.936418Z","iopub.execute_input":"2021-09-02T13:23:27.937116Z","iopub.status.idle":"2021-09-02T13:23:27.943233Z","shell.execute_reply.started":"2021-09-02T13:23:27.937078Z","shell.execute_reply":"2021-09-02T13:23:27.941985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X[2].reshape(28,28))\n","metadata":{"execution":{"iopub.status.busy":"2021-09-02T13:23:40.862036Z","iopub.execute_input":"2021-09-02T13:23:40.862439Z","iopub.status.idle":"2021-09-02T13:23:41.050595Z","shell.execute_reply.started":"2021-09-02T13:23:40.862408Z","shell.execute_reply":"2021-09-02T13:23:41.049674Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y[2]","metadata":{"execution":{"iopub.status.busy":"2021-09-02T13:23:54.429179Z","iopub.execute_input":"2021-09-02T13:23:54.429537Z","iopub.status.idle":"2021-09-02T13:23:54.435593Z","shell.execute_reply.started":"2021-09-02T13:23:54.429507Z","shell.execute_reply":"2021-09-02T13:23:54.434664Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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=20)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T13:24:06.784571Z","iopub.execute_input":"2021-09-02T13:24:06.785100Z","iopub.status.idle":"2021-09-02T13:24:33.038017Z","shell.execute_reply.started":"2021-09-02T13:24:06.785063Z","shell.execute_reply":"2021-09-02T13:24:33.037137Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ann.evaluate(X_val,y_val)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T13:25:08.910645Z","iopub.execute_input":"2021-09-02T13:25:08.911040Z","iopub.status.idle":"2021-09-02T13:25:09.261438Z","shell.execute_reply.started":"2021-09-02T13:25:08.911009Z","shell.execute_reply":"2021-09-02T13:25:09.260511Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_predict = ann.predict(X_val)\ny_predict","metadata":{"execution":{"iopub.status.busy":"2021-09-02T13:25:19.818696Z","iopub.execute_input":"2021-09-02T13:25:19.819073Z","iopub.status.idle":"2021-09-02T13:25:20.052156Z","shell.execute_reply.started":"2021-09-02T13:25:19.819025Z","shell.execute_reply":"2021-09-02T13:25:20.051100Z"},"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-09-02T13:25:29.965734Z","iopub.execute_input":"2021-09-02T13:25:29.966175Z","iopub.status.idle":"2021-09-02T13:25:29.997447Z","shell.execute_reply.started":"2021-09-02T13:25:29.966136Z","shell.execute_reply":"2021-09-02T13:25:29.996314Z"},"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-09-02T13:25:39.658120Z","iopub.execute_input":"2021-09-02T13:25:39.658489Z","iopub.status.idle":"2021-09-02T13:25:40.388267Z","shell.execute_reply.started":"2021-09-02T13:25:39.658454Z","shell.execute_reply":"2021-09-02T13:25:40.387036Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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=20)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T13:25:52.179016Z","iopub.execute_input":"2021-09-02T13:25:52.179431Z","iopub.status.idle":"2021-09-02T13:26:32.126152Z","shell.execute_reply.started":"2021-09-02T13:25:52.179395Z","shell.execute_reply":"2021-09-02T13:26:32.125162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ann_hidden.evaluate(X_val,y_val)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T13:30:04.441349Z","iopub.execute_input":"2021-09-02T13:30:04.441769Z","iopub.status.idle":"2021-09-02T13:30:04.824301Z","shell.execute_reply.started":"2021-09-02T13:30:04.441737Z","shell.execute_reply":"2021-09-02T13:30:04.823387Z"},"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-09-02T13:30:16.060515Z","iopub.execute_input":"2021-09-02T13:30:16.060891Z","iopub.status.idle":"2021-09-02T13:30:16.310027Z","shell.execute_reply.started":"2021-09-02T13:30:16.060859Z","shell.execute_reply":"2021-09-02T13:30:16.309077Z"},"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-09-02T13:30:26.366764Z","iopub.execute_input":"2021-09-02T13:30:26.367194Z","iopub.status.idle":"2021-09-02T13:30:26.398108Z","shell.execute_reply.started":"2021-09-02T13:30:26.367158Z","shell.execute_reply":"2021-09-02T13:30:26.397094Z"},"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-09-02T13:30:38.386533Z","iopub.execute_input":"2021-09-02T13:30:38.386915Z","iopub.status.idle":"2021-09-02T13:30:39.099991Z","shell.execute_reply.started":"2021-09-02T13:30:38.386881Z","shell.execute_reply":"2021-09-02T13:30:39.098815Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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=20)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T13:30:54.957546Z","iopub.execute_input":"2021-09-02T13:30:54.958459Z","iopub.status.idle":"2021-09-02T13:31:34.185780Z","shell.execute_reply.started":"2021-09-02T13:30:54.958407Z","shell.execute_reply":"2021-09-02T13:31:34.184659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ann_flat.evaluate(X_val,y_val)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T13:33:13.839040Z","iopub.execute_input":"2021-09-02T13:33:13.839495Z","iopub.status.idle":"2021-09-02T13:33:14.231485Z","shell.execute_reply.started":"2021-09-02T13:33:13.839458Z","shell.execute_reply":"2021-09-02T13:33:14.230622Z"},"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-09-02T13:33:17.509282Z","iopub.execute_input":"2021-09-02T13:33:17.509856Z","iopub.status.idle":"2021-09-02T13:33:18.726130Z","shell.execute_reply.started":"2021-09-02T13:33:17.509808Z","shell.execute_reply":"2021-09-02T13:33:18.725154Z"},"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-09-02T13:33:21.353155Z","iopub.execute_input":"2021-09-02T13:33:21.353513Z","iopub.status.idle":"2021-09-02T13:33:21.446538Z","shell.execute_reply.started":"2021-09-02T13:33:21.353482Z","shell.execute_reply":"2021-09-02T13:33:21.445488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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-09-02T13:33:24.294704Z","iopub.execute_input":"2021-09-02T13:33:24.295131Z","iopub.status.idle":"2021-09-02T13:33:24.346948Z","shell.execute_reply.started":"2021-09-02T13:33:24.295090Z","shell.execute_reply":"2021-09-02T13:33:24.345980Z"},"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-09-02T13:33:27.280917Z","iopub.execute_input":"2021-09-02T13:33:27.281319Z","iopub.status.idle":"2021-09-02T13:33:27.295594Z","shell.execute_reply.started":"2021-09-02T13:33:27.281284Z","shell.execute_reply":"2021-09-02T13:33:27.294489Z"},"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-09-02T13:33:30.085208Z","iopub.execute_input":"2021-09-02T13:33:30.085656Z","iopub.status.idle":"2021-09-02T13:33:30.092505Z","shell.execute_reply.started":"2021-09-02T13:33:30.085619Z","shell.execute_reply":"2021-09-02T13:33:30.091367Z"},"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-09-02T13:33:32.321592Z","iopub.execute_input":"2021-09-02T13:33:32.321945Z","iopub.status.idle":"2021-09-02T13:33:32.329328Z","shell.execute_reply.started":"2021-09-02T13:33:32.321916Z","shell.execute_reply":"2021-09-02T13:33:32.328168Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn.fit(X_train, y_train, epochs=20)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T13:33:35.005706Z","iopub.execute_input":"2021-09-02T13:33:35.006266Z","iopub.status.idle":"2021-09-02T13:37:22.806924Z","shell.execute_reply.started":"2021-09-02T13:33:35.006231Z","shell.execute_reply":"2021-09-02T13:37:22.805922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_predict_final = cnn.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T13:54:16.397655Z","iopub.execute_input":"2021-09-02T13:54:16.398082Z","iopub.status.idle":"2021-09-02T13:54:19.464274Z","shell.execute_reply.started":"2021-09-02T13:54:16.398023Z","shell.execute_reply":"2021-09-02T13:54:19.463172Z"},"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-09-02T13:54:41.450353Z","iopub.execute_input":"2021-09-02T13:54:41.450752Z","iopub.status.idle":"2021-09-02T13:54:41.543153Z","shell.execute_reply.started":"2021-09-02T13:54:41.450722Z","shell.execute_reply":"2021-09-02T13:54:41.542125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"image_id = range(1, y_predict_final.shape[0] + 1)\nsubmission_file = pd.DataFrame({'ImageId':image_id, 'Label':y_predicted_labels_final})\nsubmission_file.to_csv('digit_recognizer_submission_final_cnn_ann.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T13:54:46.792861Z","iopub.execute_input":"2021-09-02T13:54:46.793257Z","iopub.status.idle":"2021-09-02T13:54:46.874022Z","shell.execute_reply.started":"2021-09-02T13:54:46.793208Z","shell.execute_reply":"2021-09-02T13:54:46.873167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_file.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-02T13:54:50.056031Z","iopub.execute_input":"2021-09-02T13:54:50.056433Z","iopub.status.idle":"2021-09-02T13:54:50.066754Z","shell.execute_reply.started":"2021-09-02T13:54:50.056403Z","shell.execute_reply":"2021-09-02T13:54:50.065648Z"},"trusted":true},"execution_count":null,"outputs":[]}]}