{"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":"2022-06-03T13:54:46.369421Z","iopub.execute_input":"2022-06-03T13:54:46.370615Z","iopub.status.idle":"2022-06-03T13:54:46.403721Z","shell.execute_reply.started":"2022-06-03T13:54:46.370455Z","shell.execute_reply":"2022-06-03T13:54:46.402470Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras import datasets,layers,models\nfrom keras.layers import Dropout\nimport matplotlib.pyplot as plt\nimport numpy as np","metadata":{"execution":{"iopub.status.busy":"2022-06-03T13:54:46.405362Z","iopub.execute_input":"2022-06-03T13:54:46.405818Z","iopub.status.idle":"2022-06-03T13:54:54.455413Z","shell.execute_reply.started":"2022-06-03T13:54:46.405749Z","shell.execute_reply":"2022-06-03T13:54:54.454328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(x_train,y_train),(x_test,y_test)=datasets.mnist.load_data()\nx_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-03T13:54:54.456641Z","iopub.execute_input":"2022-06-03T13:54:54.457335Z","iopub.status.idle":"2022-06-03T13:54:55.047340Z","shell.execute_reply.started":"2022-06-03T13:54:54.457299Z","shell.execute_reply":"2022-06-03T13:54:55.046282Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test.shape","metadata":{"execution":{"iopub.status.busy":"2022-06-03T13:54:55.049777Z","iopub.execute_input":"2022-06-03T13:54:55.050302Z","iopub.status.idle":"2022-06-03T13:54:55.057882Z","shell.execute_reply.started":"2022-06-03T13:54:55.050259Z","shell.execute_reply":"2022-06-03T13:54:55.056652Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(x_train[1])\nplt.show()\nplt.imshow(x_train[1], cmap = plt.cm.binary)","metadata":{"execution":{"iopub.status.busy":"2022-06-03T13:54:55.059720Z","iopub.execute_input":"2022-06-03T13:54:55.060522Z","iopub.status.idle":"2022-06-03T13:54:55.425392Z","shell.execute_reply.started":"2022-06-03T13:54:55.060467Z","shell.execute_reply":"2022-06-03T13:54:55.424238Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x_train[0])","metadata":{"execution":{"iopub.status.busy":"2022-06-03T13:54:55.426841Z","iopub.execute_input":"2022-06-03T13:54:55.427217Z","iopub.status.idle":"2022-06-03T13:54:55.436183Z","shell.execute_reply.started":"2022-06-03T13:54:55.427184Z","shell.execute_reply":"2022-06-03T13:54:55.434894Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train =tf.keras.utils.normalize(x_train,axis=1)\nx_test =tf.keras.utils.normalize(x_test,axis=1)\nplt.imshow(x_train[0], cmap = plt.cm.binary)","metadata":{"execution":{"iopub.status.busy":"2022-06-03T13:54:55.437977Z","iopub.execute_input":"2022-06-03T13:54:55.438525Z","iopub.status.idle":"2022-06-03T13:54:56.555947Z","shell.execute_reply.started":"2022-06-03T13:54:55.438469Z","shell.execute_reply":"2022-06-03T13:54:56.554922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(x_train[0])","metadata":{"execution":{"iopub.status.busy":"2022-06-03T13:54:56.557074Z","iopub.execute_input":"2022-06-03T13:54:56.557412Z","iopub.status.idle":"2022-06-03T13:54:56.826121Z","shell.execute_reply.started":"2022-06-03T13:54:56.557381Z","shell.execute_reply":"2022-06-03T13:54:56.824759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"imag_size =28\nx_trains = np.array(x_train).reshape(-1,imag_size,imag_size,1)\nx_tests = np.array(x_test).reshape(-1,imag_size,imag_size,1)\nprint(\"dimension of  training samples\",x_trains.shape)\nprint(\"dimension of testing samples\",x_tests.shape)","metadata":{"execution":{"iopub.status.busy":"2022-06-03T13:54:56.827613Z","iopub.execute_input":"2022-06-03T13:54:56.827999Z","iopub.status.idle":"2022-06-03T13:54:57.133331Z","shell.execute_reply.started":"2022-06-03T13:54:56.827965Z","shell.execute_reply":"2022-06-03T13:54:57.132179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#building model in cnn\ncnn=tf.keras.models.Sequential()\n# First Layer\ncnn.add(tf.keras.layers.Conv2D(filters=64,kernel_size=3, padding=\"same\", activation=\"relu\", input_shape=[28,28,1]))\n#MaxPoolingLayer\ncnn.add(tf.keras.layers.MaxPool2D(pool_size=2,strides=2, padding='valid'))\n# second Layer\ncnn.add(tf.keras.layers.Conv2D(filters=64,kernel_size=3, padding=\"same\", activation=\"relu\"))\n#MaxPoolingLayer\ncnn.add(tf.keras.layers.MaxPool2D(pool_size=2,strides=2, padding='valid'))\n# Flattening Layer\ncnn.add(tf.keras.layers.Flatten())\n# Droput Layer\n# Adding the first fully connected layer\ncnn.add(tf.keras.layers.Dense(units= 64,activation='relu'))\ncnn.add(Dropout(0.3))\ncnn.add(tf.keras.layers.Dense(10,activation='softmax'))\ncnn.summary()","metadata":{"execution":{"iopub.status.busy":"2022-06-03T13:55:26.306188Z","iopub.execute_input":"2022-06-03T13:55:26.306723Z","iopub.status.idle":"2022-06-03T13:55:26.471674Z","shell.execute_reply.started":"2022-06-03T13:55:26.306682Z","shell.execute_reply":"2022-06-03T13:55:26.470718Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn.compile(optimizer='adam',loss='sparse_categorical_crossentropy',\n             metrics=['accuracy'])","metadata":{"execution":{"iopub.status.busy":"2022-06-03T13:55:30.894051Z","iopub.execute_input":"2022-06-03T13:55:30.894603Z","iopub.status.idle":"2022-06-03T13:55:30.912309Z","shell.execute_reply.started":"2022-06-03T13:55:30.894547Z","shell.execute_reply":"2022-06-03T13:55:30.911052Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pt=cnn.fit(x_trains,y_train, validation_split=0.3,epochs = 5)","metadata":{"execution":{"iopub.status.busy":"2022-06-03T13:55:31.177834Z","iopub.execute_input":"2022-06-03T13:55:31.178286Z","iopub.status.idle":"2022-06-03T13:58:54.175148Z","shell.execute_reply.started":"2022-06-03T13:55:31.178253Z","shell.execute_reply":"2022-06-03T13:58:54.174190Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.plot(pt.history['accuracy'], label='acc', color='red')\nplt.plot(pt.history['val_accuracy'], label='val_acc', color='green')\nplt.legend() \n","metadata":{"execution":{"iopub.status.busy":"2022-06-03T13:58:54.177212Z","iopub.execute_input":"2022-06-03T13:58:54.177694Z","iopub.status.idle":"2022-06-03T13:58:54.413587Z","shell.execute_reply.started":"2022-06-03T13:58:54.177647Z","shell.execute_reply":"2022-06-03T13:58:54.412419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cnn.evaluate(x_tests,y_test)","metadata":{"execution":{"iopub.status.busy":"2022-06-03T13:58:54.415336Z","iopub.execute_input":"2022-06-03T13:58:54.416668Z","iopub.status.idle":"2022-06-03T13:58:57.395003Z","shell.execute_reply.started":"2022-06-03T13:58:54.416616Z","shell.execute_reply":"2022-06-03T13:58:57.393900Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = cnn.predict([x_tests])\nprint(predictions)\ny_pred_label = np.argmax(predictions,axis = 1)\nprint(y_pred_label)","metadata":{"execution":{"iopub.status.busy":"2022-06-03T13:58:57.397964Z","iopub.execute_input":"2022-06-03T13:58:57.398701Z","iopub.status.idle":"2022-06-03T13:59:00.001523Z","shell.execute_reply.started":"2022-06-03T13:58:57.398662Z","shell.execute_reply":"2022-06-03T13:59:00.000404Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"c_matrix=tf.math.confusion_matrix(labels = y_test , predictions= y_pred_label)\nc_matrix\n","metadata":{"execution":{"iopub.status.busy":"2022-06-03T13:59:05.192459Z","iopub.execute_input":"2022-06-03T13:59:05.193304Z","iopub.status.idle":"2022-06-03T13:59:05.207722Z","shell.execute_reply.started":"2022-06-03T13:59:05.193255Z","shell.execute_reply":"2022-06-03T13:59:05.206440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"my_submission = pd.DataFrame({'ImageId': list(range(1, len(y_pred_label)+1)), 'Label': y_pred_label})\nmy_submission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-06-03T13:59:06.867500Z","iopub.execute_input":"2022-06-03T13:59:06.868571Z","iopub.status.idle":"2022-06-03T13:59:06.902704Z","shell.execute_reply.started":"2022-06-03T13:59:06.868507Z","shell.execute_reply":"2022-06-03T13:59:06.901641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}