{"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":"2021-07-04T04:41:32.884062Z","iopub.execute_input":"2021-07-04T04:41:32.884419Z","iopub.status.idle":"2021-07-04T04:41:32.893814Z","shell.execute_reply.started":"2021-07-04T04:41:32.884385Z","shell.execute_reply":"2021-07-04T04:41:32.892634Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_set=pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_set=pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\nsubmission=pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\ntraining_set.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-04T04:41:32.895831Z","iopub.execute_input":"2021-07-04T04:41:32.896518Z","iopub.status.idle":"2021-07-04T04:41:37.435243Z","shell.execute_reply.started":"2021-07-04T04:41:32.896468Z","shell.execute_reply":"2021-07-04T04:41:37.434159Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport matplotlib.pyplot as plt\nimport numpy as np\nfrom tensorflow import keras","metadata":{"execution":{"iopub.status.busy":"2021-07-04T04:41:37.436817Z","iopub.execute_input":"2021-07-04T04:41:37.437081Z","iopub.status.idle":"2021-07-04T04:41:37.440978Z","shell.execute_reply.started":"2021-07-04T04:41:37.437055Z","shell.execute_reply":"2021-07-04T04:41:37.440080Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"training_set.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2021-07-04T04:41:37.442403Z","iopub.execute_input":"2021-07-04T04:41:37.442701Z","iopub.status.idle":"2021-07-04T04:41:37.505659Z","shell.execute_reply.started":"2021-07-04T04:41:37.442664Z","shell.execute_reply":"2021-07-04T04:41:37.504890Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=training_set.groupby('label').size()\ndf","metadata":{"execution":{"iopub.status.busy":"2021-07-04T04:41:37.506920Z","iopub.execute_input":"2021-07-04T04:41:37.507184Z","iopub.status.idle":"2021-07-04T04:41:37.514825Z","shell.execute_reply.started":"2021-07-04T04:41:37.507159Z","shell.execute_reply":"2021-07-04T04:41:37.513885Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label='0','1','2','3','4','5','6','7','8','9'\nmy_explode=(0.1, 0.1, 0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1,)\nplt.figure(figsize=(7,7))\nplt.pie(df,labels=label, autopct='%1.1f%%',startangle=15, shadow=True ,explode=my_explode)","metadata":{"execution":{"iopub.status.busy":"2021-07-04T04:41:37.516153Z","iopub.execute_input":"2021-07-04T04:41:37.516437Z","iopub.status.idle":"2021-07-04T04:41:37.723277Z","shell.execute_reply.started":"2021-07-04T04:41:37.516411Z","shell.execute_reply":"2021-07-04T04:41:37.722317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(7,7))\ncolors=['#d580ff','#b3b3ff','#9fff80','#002699','#cc80ff','#70db70','#ffd24d','#ff6666','#b3b3ff','#b3b300']\nplt.bar(label,df,color=colors)","metadata":{"execution":{"iopub.status.busy":"2021-07-04T04:41:37.724504Z","iopub.execute_input":"2021-07-04T04:41:37.724814Z","iopub.status.idle":"2021-07-04T04:41:37.879138Z","shell.execute_reply.started":"2021-07-04T04:41:37.724787Z","shell.execute_reply":"2021-07-04T04:41:37.877854Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y=training_set['label']","metadata":{"execution":{"iopub.status.busy":"2021-07-04T04:41:37.882426Z","iopub.execute_input":"2021-07-04T04:41:37.882858Z","iopub.status.idle":"2021-07-04T04:41:37.887981Z","shell.execute_reply.started":"2021-07-04T04:41:37.882817Z","shell.execute_reply":"2021-07-04T04:41:37.886764Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.utils.np_utils import to_categorical \ny= to_categorical(y)","metadata":{"execution":{"iopub.status.busy":"2021-07-04T04:41:37.890385Z","iopub.execute_input":"2021-07-04T04:41:37.890725Z","iopub.status.idle":"2021-07-04T04:41:37.901602Z","shell.execute_reply.started":"2021-07-04T04:41:37.890664Z","shell.execute_reply":"2021-07-04T04:41:37.900592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x=training_set.drop(labels='label',axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-07-04T04:41:37.903028Z","iopub.execute_input":"2021-07-04T04:41:37.903423Z","iopub.status.idle":"2021-07-04T04:41:38.004726Z","shell.execute_reply.started":"2021-07-04T04:41:37.903366Z","shell.execute_reply":"2021-07-04T04:41:38.003705Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(0,8):\n    digit1=(x.iloc[i:i+1,:].to_numpy()).reshape(28,28,1)\n    plt.figure(figsize=(1.5,1.5))\n    plt.imshow(digit1)","metadata":{"execution":{"iopub.status.busy":"2021-07-04T04:41:38.005997Z","iopub.execute_input":"2021-07-04T04:41:38.006290Z","iopub.status.idle":"2021-07-04T04:41:38.712156Z","shell.execute_reply.started":"2021-07-04T04:41:38.006261Z","shell.execute_reply":"2021-07-04T04:41:38.711108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nx_train, x_test, y_train, y_test=train_test_split(x,y,test_size=0.2,random_state=0)","metadata":{"execution":{"iopub.status.busy":"2021-07-04T04:41:38.713328Z","iopub.execute_input":"2021-07-04T04:41:38.713598Z","iopub.status.idle":"2021-07-04T04:41:38.964705Z","shell.execute_reply.started":"2021-07-04T04:41:38.713565Z","shell.execute_reply":"2021-07-04T04:41:38.963763Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train.shape\n","metadata":{"execution":{"iopub.status.busy":"2021-07-04T04:41:38.966257Z","iopub.execute_input":"2021-07-04T04:41:38.966663Z","iopub.status.idle":"2021-07-04T04:41:38.971944Z","shell.execute_reply.started":"2021-07-04T04:41:38.966621Z","shell.execute_reply":"2021-07-04T04:41:38.971252Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train=x_train.values.reshape(len(x_train),28,28,1)","metadata":{"execution":{"iopub.status.busy":"2021-07-04T04:41:38.972878Z","iopub.execute_input":"2021-07-04T04:41:38.973143Z","iopub.status.idle":"2021-07-04T04:41:38.985407Z","shell.execute_reply.started":"2021-07-04T04:41:38.973118Z","shell.execute_reply":"2021-07-04T04:41:38.984453Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test=x_test.values.reshape(len(x_test),28,28,1)","metadata":{"execution":{"iopub.status.busy":"2021-07-04T04:41:38.987161Z","iopub.execute_input":"2021-07-04T04:41:38.987721Z","iopub.status.idle":"2021-07-04T04:41:38.998134Z","shell.execute_reply.started":"2021-07-04T04:41:38.987517Z","shell.execute_reply":"2021-07-04T04:41:38.997282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y_test)","metadata":{"execution":{"iopub.status.busy":"2021-07-04T04:41:38.999343Z","iopub.execute_input":"2021-07-04T04:41:38.999731Z","iopub.status.idle":"2021-07-04T04:41:39.011207Z","shell.execute_reply.started":"2021-07-04T04:41:38.999702Z","shell.execute_reply":"2021-07-04T04:41:39.010280Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-04T04:41:39.012497Z","iopub.execute_input":"2021-07-04T04:41:39.012847Z","iopub.status.idle":"2021-07-04T04:41:39.021569Z","shell.execute_reply.started":"2021-07-04T04:41:39.012817Z","shell.execute_reply":"2021-07-04T04:41:39.020878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape","metadata":{"execution":{"iopub.status.busy":"2021-07-04T04:41:39.022434Z","iopub.execute_input":"2021-07-04T04:41:39.022669Z","iopub.status.idle":"2021-07-04T04:41:39.034454Z","shell.execute_reply.started":"2021-07-04T04:41:39.022646Z","shell.execute_reply":"2021-07-04T04:41:39.033542Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y_train)","metadata":{"execution":{"iopub.status.busy":"2021-07-04T04:41:39.035821Z","iopub.execute_input":"2021-07-04T04:41:39.036116Z","iopub.status.idle":"2021-07-04T04:41:39.049525Z","shell.execute_reply.started":"2021-07-04T04:41:39.036088Z","shell.execute_reply":"2021-07-04T04:41:39.048582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf","metadata":{"execution":{"iopub.status.busy":"2021-07-04T04:41:39.050647Z","iopub.execute_input":"2021-07-04T04:41:39.051082Z","iopub.status.idle":"2021-07-04T04:41:39.060429Z","shell.execute_reply.started":"2021-07-04T04:41:39.051053Z","shell.execute_reply":"2021-07-04T04:41:39.059752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn=tf.keras.models.Sequential()","metadata":{"execution":{"iopub.status.busy":"2021-07-04T04:41:39.061364Z","iopub.execute_input":"2021-07-04T04:41:39.061768Z","iopub.status.idle":"2021-07-04T04:41:39.076286Z","shell.execute_reply.started":"2021-07-04T04:41:39.061739Z","shell.execute_reply":"2021-07-04T04:41:39.075339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from keras.layers import BatchNormalization\ncnn.add(tf.keras.layers.Conv2D(filters=32,kernel_size=(3,3), activation='relu', input_shape=(28, 28, 1)))\ncnn.add(tf.keras.layers.MaxPool2D(pool_size=2,strides=2))\ncnn.add(tf.keras.layers.BatchNormalization())  # added BatchNormalization\n\n\ncnn.add(tf.keras.layers.Conv2D(filters=32, kernel_size=(3,3), activation='relu', input_shape=(28, 28, 1)))\ncnn.add(tf.keras.layers.MaxPool2D(pool_size=2,strides=2))\ncnn.add(tf.keras.layers.BatchNormalization())\n\n\ncnn.add(tf.keras.layers.Flatten())\ncnn.add(tf.keras.layers.Dense(units=10 ,activation='softmax'))\n\n\ncnn.compile(optimizer= 'adam', loss= 'categorical_crossentropy', metrics = ['accuracy'])\n","metadata":{"execution":{"iopub.status.busy":"2021-07-04T04:41:39.077496Z","iopub.execute_input":"2021-07-04T04:41:39.078086Z","iopub.status.idle":"2021-07-04T04:41:39.148900Z","shell.execute_reply.started":"2021-07-04T04:41:39.078038Z","shell.execute_reply":"2021-07-04T04:41:39.148142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"reduce_lr=tf.keras.callbacks.ReduceLROnPlateau(monitor='accuracy',factor=0.2,patience=5,min_lr=0.001)","metadata":{"execution":{"iopub.status.busy":"2021-07-04T04:41:39.152872Z","iopub.execute_input":"2021-07-04T04:41:39.153286Z","iopub.status.idle":"2021-07-04T04:41:39.158174Z","shell.execute_reply.started":"2021-07-04T04:41:39.153255Z","shell.execute_reply":"2021-07-04T04:41:39.157212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"history=cnn.fit(x_train, y_train, validation_data=(x_test ,y_test), epochs=32, callbacks=[reduce_lr])","metadata":{"execution":{"iopub.status.busy":"2021-07-04T04:41:39.159814Z","iopub.execute_input":"2021-07-04T04:41:39.160375Z","iopub.status.idle":"2021-07-04T04:50:20.355069Z","shell.execute_reply.started":"2021-07-04T04:41:39.160333Z","shell.execute_reply":"2021-07-04T04:50:20.354087Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = cnn.predict(x_test)","metadata":{"execution":{"iopub.status.busy":"2021-07-04T04:50:20.356484Z","iopub.execute_input":"2021-07-04T04:50:20.356770Z","iopub.status.idle":"2021-07-04T04:50:21.562542Z","shell.execute_reply.started":"2021-07-04T04:50:20.356742Z","shell.execute_reply":"2021-07-04T04:50:21.561751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred=np.argmax(y_pred, axis=1)\ny_test=np.argmax(y_test, axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-07-04T04:50:21.563467Z","iopub.execute_input":"2021-07-04T04:50:21.563768Z","iopub.status.idle":"2021-07-04T04:50:21.569586Z","shell.execute_reply.started":"2021-07-04T04:50:21.563737Z","shell.execute_reply":"2021-07-04T04:50:21.568710Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred","metadata":{"execution":{"iopub.status.busy":"2021-07-04T04:50:21.571159Z","iopub.execute_input":"2021-07-04T04:50:21.571482Z","iopub.status.idle":"2021-07-04T04:50:21.584405Z","shell.execute_reply.started":"2021-07-04T04:50:21.571454Z","shell.execute_reply":"2021-07-04T04:50:21.583333Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_test","metadata":{"execution":{"iopub.status.busy":"2021-07-04T04:50:21.585581Z","iopub.execute_input":"2021-07-04T04:50:21.585929Z","iopub.status.idle":"2021-07-04T04:50:21.599117Z","shell.execute_reply.started":"2021-07-04T04:50:21.585899Z","shell.execute_reply":"2021-07-04T04:50:21.598244Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix, accuracy_score\ncn=confusion_matrix(y_test,y_pred)\nprint(cn)\naccuracy_score(y_test,y_pred)","metadata":{"execution":{"iopub.status.busy":"2021-07-04T04:50:21.600276Z","iopub.execute_input":"2021-07-04T04:50:21.600606Z","iopub.status.idle":"2021-07-04T04:50:21.628577Z","shell.execute_reply.started":"2021-07-04T04:50:21.600571Z","shell.execute_reply":"2021-07-04T04:50:21.627632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(history.history.keys())","metadata":{"execution":{"iopub.status.busy":"2021-07-04T04:50:21.629775Z","iopub.execute_input":"2021-07-04T04:50:21.630033Z","iopub.status.idle":"2021-07-04T04:50:21.635315Z","shell.execute_reply.started":"2021-07-04T04:50:21.630007Z","shell.execute_reply":"2021-07-04T04:50:21.634574Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(history.history['loss'], label='loss')\nplt.plot(history.history['accuracy'], label='accuracy')\nplt.legend()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2021-07-04T04:50:21.636284Z","iopub.execute_input":"2021-07-04T04:50:21.636536Z","iopub.status.idle":"2021-07-04T04:50:21.795480Z","shell.execute_reply.started":"2021-07-04T04:50:21.636511Z","shell.execute_reply":"2021-07-04T04:50:21.794794Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_set=test_set.values.reshape(-1,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2021-07-04T04:50:21.796447Z","iopub.execute_input":"2021-07-04T04:50:21.796821Z","iopub.status.idle":"2021-07-04T04:50:21.801002Z","shell.execute_reply.started":"2021-07-04T04:50:21.796792Z","shell.execute_reply":"2021-07-04T04:50:21.799844Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_prediction=cnn.predict(test_set)","metadata":{"execution":{"iopub.status.busy":"2021-07-04T04:50:21.802097Z","iopub.execute_input":"2021-07-04T04:50:21.802446Z","iopub.status.idle":"2021-07-04T04:50:26.120282Z","shell.execute_reply.started":"2021-07-04T04:50:21.802418Z","shell.execute_reply":"2021-07-04T04:50:26.119015Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_prediction=np.argmax(Y_prediction, axis=1)","metadata":{"execution":{"iopub.status.busy":"2021-07-04T04:50:26.122087Z","iopub.execute_input":"2021-07-04T04:50:26.122472Z","iopub.status.idle":"2021-07-04T04:50:26.129867Z","shell.execute_reply.started":"2021-07-04T04:50:26.122430Z","shell.execute_reply":"2021-07-04T04:50:26.128747Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_prediction","metadata":{"execution":{"iopub.status.busy":"2021-07-04T04:50:26.131043Z","iopub.execute_input":"2021-07-04T04:50:26.131324Z","iopub.status.idle":"2021-07-04T04:50:26.146599Z","shell.execute_reply.started":"2021-07-04T04:50:26.131286Z","shell.execute_reply":"2021-07-04T04:50:26.145785Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission['Label'] = Y_prediction\nsubmission.to_csv(\"my_submission.csv\", index=False)\nsubmission.head()","metadata":{"execution":{"iopub.status.busy":"2021-07-04T04:59:57.039094Z","iopub.execute_input":"2021-07-04T04:59:57.039496Z","iopub.status.idle":"2021-07-04T04:59:57.106008Z","shell.execute_reply.started":"2021-07-04T04:59:57.039463Z","shell.execute_reply":"2021-07-04T04:59:57.104997Z"},"trusted":true},"execution_count":null,"outputs":[]}]}