{"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":"ID:181-35-2475","metadata":{}},{"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":{"execution":{"iopub.status.busy":"2021-09-02T17:31:18.584093Z","iopub.execute_input":"2021-09-02T17:31:18.584644Z","iopub.status.idle":"2021-09-02T17:31:26.085456Z","shell.execute_reply.started":"2021-09-02T17:31:18.584558Z","shell.execute_reply":"2021-09-02T17:31:26.084335Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train data\ntrain = pd.read_csv('../input/digit-recognizer/train.csv')\ntrain.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-02T17:31:26.086914Z","iopub.execute_input":"2021-09-02T17:31:26.087236Z","iopub.status.idle":"2021-09-02T17:31:29.985754Z","shell.execute_reply.started":"2021-09-02T17:31:26.087207Z","shell.execute_reply":"2021-09-02T17:31:29.984625Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test data\ntest = pd.read_csv('../input/digit-recognizer/test.csv')\ntest.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-02T17:31:29.987790Z","iopub.execute_input":"2021-09-02T17:31:29.988475Z","iopub.status.idle":"2021-09-02T17:31:32.199094Z","shell.execute_reply.started":"2021-09-02T17:31:29.988427Z","shell.execute_reply":"2021-09-02T17:31:32.197989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#train shape\ntrain.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-02T17:31:32.200933Z","iopub.execute_input":"2021-09-02T17:31:32.201254Z","iopub.status.idle":"2021-09-02T17:31:32.206583Z","shell.execute_reply.started":"2021-09-02T17:31:32.201224Z","shell.execute_reply":"2021-09-02T17:31:32.205666Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#test shape\ntest.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-02T17:31:32.207820Z","iopub.execute_input":"2021-09-02T17:31:32.208165Z","iopub.status.idle":"2021-09-02T17:31:32.224450Z","shell.execute_reply.started":"2021-09-02T17:31:32.208134Z","shell.execute_reply":"2021-09-02T17:31:32.223610Z"},"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-02T17:31:32.225887Z","iopub.execute_input":"2021-09-02T17:31:32.226479Z","iopub.status.idle":"2021-09-02T17:31:32.594500Z","shell.execute_reply.started":"2021-09-02T17:31:32.226436Z","shell.execute_reply":"2021-09-02T17:31:32.593412Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y = train['label']\ny.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-02T17:31:32.595730Z","iopub.execute_input":"2021-09-02T17:31:32.596061Z","iopub.status.idle":"2021-09-02T17:31:32.601943Z","shell.execute_reply.started":"2021-09-02T17:31:32.596017Z","shell.execute_reply":"2021-09-02T17:31:32.601213Z"},"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-02T17:31:32.604256Z","iopub.execute_input":"2021-09-02T17:31:32.604734Z","iopub.status.idle":"2021-09-02T17:31:32.761957Z","shell.execute_reply.started":"2021-09-02T17:31:32.604703Z","shell.execute_reply":"2021-09-02T17:31:32.761155Z"},"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-02T17:31:32.763509Z","iopub.execute_input":"2021-09-02T17:31:32.763910Z","iopub.status.idle":"2021-09-02T17:31:33.384483Z","shell.execute_reply.started":"2021-09-02T17:31:32.763880Z","shell.execute_reply":"2021-09-02T17:31:33.383283Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape, y_train.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-02T17:31:33.385745Z","iopub.execute_input":"2021-09-02T17:31:33.386061Z","iopub.status.idle":"2021-09-02T17:31:33.392509Z","shell.execute_reply.started":"2021-09-02T17:31:33.386017Z","shell.execute_reply":"2021-09-02T17:31:33.391543Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_val.shape, y_val.shape","metadata":{"execution":{"iopub.status.busy":"2021-09-02T17:31:33.393931Z","iopub.execute_input":"2021-09-02T17:31:33.394279Z","iopub.status.idle":"2021-09-02T17:31:33.407827Z","shell.execute_reply.started":"2021-09-02T17:31:33.394248Z","shell.execute_reply":"2021-09-02T17:31:33.406440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(X[2].reshape(28,28))","metadata":{"execution":{"iopub.status.busy":"2021-09-02T17:31:33.408983Z","iopub.execute_input":"2021-09-02T17:31:33.409474Z","iopub.status.idle":"2021-09-02T17:31:33.607661Z","shell.execute_reply.started":"2021-09-02T17:31:33.409440Z","shell.execute_reply":"2021-09-02T17:31:33.606423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y[2]","metadata":{"execution":{"iopub.status.busy":"2021-09-02T17:31:33.609470Z","iopub.execute_input":"2021-09-02T17:31:33.609919Z","iopub.status.idle":"2021-09-02T17:31:33.617674Z","shell.execute_reply.started":"2021-09-02T17:31:33.609863Z","shell.execute_reply":"2021-09-02T17:31:33.616571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Simple neural network with no hidden layers\nann =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-02T17:31:33.619438Z","iopub.execute_input":"2021-09-02T17:31:33.619933Z","iopub.status.idle":"2021-09-02T17:32:02.458342Z","shell.execute_reply.started":"2021-09-02T17:31:33.619876Z","shell.execute_reply":"2021-09-02T17:32:02.457361Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ann.evaluate(X_val,y_val)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T17:32:02.459737Z","iopub.execute_input":"2021-09-02T17:32:02.460409Z","iopub.status.idle":"2021-09-02T17:32:02.844756Z","shell.execute_reply.started":"2021-09-02T17:32:02.460361Z","shell.execute_reply":"2021-09-02T17:32:02.843576Z"},"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-02T17:32:02.848421Z","iopub.execute_input":"2021-09-02T17:32:02.848745Z","iopub.status.idle":"2021-09-02T17:32:03.092960Z","shell.execute_reply.started":"2021-09-02T17:32:02.848717Z","shell.execute_reply":"2021-09-02T17:32:03.091978Z"},"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-02T17:32:03.094166Z","iopub.execute_input":"2021-09-02T17:32:03.094438Z","iopub.status.idle":"2021-09-02T17:32:03.121911Z","shell.execute_reply.started":"2021-09-02T17:32:03.094411Z","shell.execute_reply":"2021-09-02T17:32:03.121081Z"},"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-02T17:32:03.123232Z","iopub.execute_input":"2021-09-02T17:32:03.123736Z","iopub.status.idle":"2021-09-02T17:32:03.864203Z","shell.execute_reply.started":"2021-09-02T17:32:03.123702Z","shell.execute_reply":"2021-09-02T17:32:03.862930Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Using hidden layer\nann_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-02T17:32:03.865922Z","iopub.execute_input":"2021-09-02T17:32:03.866361Z","iopub.status.idle":"2021-09-02T17:32:45.621818Z","shell.execute_reply.started":"2021-09-02T17:32:03.866317Z","shell.execute_reply":"2021-09-02T17:32:45.620724Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ann_hidden.evaluate(X_val,y_val)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T17:32:45.623427Z","iopub.execute_input":"2021-09-02T17:32:45.623842Z","iopub.status.idle":"2021-09-02T17:32:46.038495Z","shell.execute_reply.started":"2021-09-02T17:32:45.623798Z","shell.execute_reply":"2021-09-02T17:32:46.037280Z"},"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-02T17:32:46.042089Z","iopub.execute_input":"2021-09-02T17:32:46.042589Z","iopub.status.idle":"2021-09-02T17:32:46.302490Z","shell.execute_reply.started":"2021-09-02T17:32:46.042552Z","shell.execute_reply":"2021-09-02T17:32:46.301521Z"},"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-02T17:32:46.303720Z","iopub.execute_input":"2021-09-02T17:32:46.304011Z","iopub.status.idle":"2021-09-02T17:32:46.336112Z","shell.execute_reply.started":"2021-09-02T17:32:46.303984Z","shell.execute_reply":"2021-09-02T17:32:46.335151Z"},"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-02T17:32:46.340860Z","iopub.execute_input":"2021-09-02T17:32:46.341195Z","iopub.status.idle":"2021-09-02T17:32:47.039093Z","shell.execute_reply.started":"2021-09-02T17:32:46.341165Z","shell.execute_reply":"2021-09-02T17:32:47.037812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Flatten Layer\nann_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-02T17:32:47.041260Z","iopub.execute_input":"2021-09-02T17:32:47.041719Z","iopub.status.idle":"2021-09-02T17:33:29.907308Z","shell.execute_reply.started":"2021-09-02T17:32:47.041672Z","shell.execute_reply":"2021-09-02T17:33:29.906438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ann_flat.evaluate(X_val,y_val)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T17:33:29.908824Z","iopub.execute_input":"2021-09-02T17:33:29.909511Z","iopub.status.idle":"2021-09-02T17:33:30.309699Z","shell.execute_reply.started":"2021-09-02T17:33:29.909457Z","shell.execute_reply":"2021-09-02T17:33:30.308887Z"},"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-02T17:33:30.310754Z","iopub.execute_input":"2021-09-02T17:33:30.311204Z","iopub.status.idle":"2021-09-02T17:33:31.501289Z","shell.execute_reply.started":"2021-09-02T17:33:30.311171Z","shell.execute_reply":"2021-09-02T17:33:31.500149Z"},"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-02T17:33:31.502542Z","iopub.execute_input":"2021-09-02T17:33:31.502850Z","iopub.status.idle":"2021-09-02T17:33:31.604189Z","shell.execute_reply.started":"2021-09-02T17:33:31.502820Z","shell.execute_reply":"2021-09-02T17:33:31.603149Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Using CNN\ncnn = 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-02T17:33:31.605609Z","iopub.execute_input":"2021-09-02T17:33:31.605896Z","iopub.status.idle":"2021-09-02T17:33:31.655428Z","shell.execute_reply.started":"2021-09-02T17:33:31.605869Z","shell.execute_reply":"2021-09-02T17:33:31.654349Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn.compile(\n             optimizer='adam',\n             loss='sparse_categorical_crossentropy',\n             metrics=['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2021-09-02T17:33:31.656796Z","iopub.execute_input":"2021-09-02T17:33:31.657202Z","iopub.status.idle":"2021-09-02T17:33:31.671352Z","shell.execute_reply.started":"2021-09-02T17:33:31.657159Z","shell.execute_reply":"2021-09-02T17:33:31.670154Z"},"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-02T17:33:31.672905Z","iopub.execute_input":"2021-09-02T17:33:31.673316Z","iopub.status.idle":"2021-09-02T17:33:31.683920Z","shell.execute_reply.started":"2021-09-02T17:33:31.673283Z","shell.execute_reply":"2021-09-02T17:33:31.682848Z"},"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-02T17:33:31.685195Z","iopub.execute_input":"2021-09-02T17:33:31.685468Z","iopub.status.idle":"2021-09-02T17:33:31.697855Z","shell.execute_reply.started":"2021-09-02T17:33:31.685442Z","shell.execute_reply":"2021-09-02T17:33:31.696804Z"},"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-02T17:33:31.699285Z","iopub.execute_input":"2021-09-02T17:33:31.699682Z","iopub.status.idle":"2021-09-02T17:37:31.254351Z","shell.execute_reply.started":"2021-09-02T17:33:31.699640Z","shell.execute_reply":"2021-09-02T17:37:31.253497Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_predict_final = cnn.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2021-09-02T17:37:31.255346Z","iopub.execute_input":"2021-09-02T17:37:31.255619Z","iopub.status.idle":"2021-09-02T17:37:34.695670Z","shell.execute_reply.started":"2021-09-02T17:37:31.255592Z","shell.execute_reply":"2021-09-02T17:37:34.694576Z"},"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-02T17:37:34.697436Z","iopub.execute_input":"2021-09-02T17:37:34.697905Z","iopub.status.idle":"2021-09-02T17:37:34.791013Z","shell.execute_reply.started":"2021-09-02T17:37:34.697848Z","shell.execute_reply":"2021-09-02T17:37:34.789866Z"},"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-02T17:37:34.792791Z","iopub.execute_input":"2021-09-02T17:37:34.793142Z","iopub.status.idle":"2021-09-02T17:37:34.870377Z","shell.execute_reply.started":"2021-09-02T17:37:34.793109Z","shell.execute_reply":"2021-09-02T17:37:34.869567Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_file.head()","metadata":{"execution":{"iopub.status.busy":"2021-09-02T17:37:34.871363Z","iopub.execute_input":"2021-09-02T17:37:34.871767Z","iopub.status.idle":"2021-09-02T17:37:34.881433Z","shell.execute_reply.started":"2021-09-02T17:37:34.871735Z","shell.execute_reply":"2021-09-02T17:37:34.880660Z"},"trusted":true},"execution_count":null,"outputs":[]}]}