{"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":"2023-08-28T13:58:19.551529Z","iopub.execute_input":"2023-08-28T13:58:19.552193Z","iopub.status.idle":"2023-08-28T13:58:19.560576Z","shell.execute_reply.started":"2023-08-28T13:58:19.552158Z","shell.execute_reply":"2023-08-28T13:58:19.559308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport tensorflow as tf\nfrom tensorflow import keras\nfrom keras import layers, Sequential ","metadata":{"execution":{"iopub.status.busy":"2023-08-28T13:58:20.604482Z","iopub.execute_input":"2023-08-28T13:58:20.605133Z","iopub.status.idle":"2023-08-28T13:58:20.609829Z","shell.execute_reply.started":"2023-08-28T13:58:20.605098Z","shell.execute_reply":"2023-08-28T13:58:20.608897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1= pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ndf2= pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"execution":{"iopub.status.busy":"2023-08-28T13:58:38.754806Z","iopub.execute_input":"2023-08-28T13:58:38.755463Z","iopub.status.idle":"2023-08-28T13:58:44.167634Z","shell.execute_reply.started":"2023-08-28T13:58:38.755428Z","shell.execute_reply":"2023-08-28T13:58:44.166689Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df1.head(5)","metadata":{"execution":{"iopub.status.busy":"2023-08-28T13:58:56.635790Z","iopub.execute_input":"2023-08-28T13:58:56.636545Z","iopub.status.idle":"2023-08-28T13:58:56.656505Z","shell.execute_reply.started":"2023-08-28T13:58:56.636507Z","shell.execute_reply":"2023-08-28T13:58:56.655123Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# since the pixel values of a particular image has been given in multiple columns rather than the original format\n# the idea is to recreate the original format of the image of shape(28,28,1) (the channels for RGB image is 3, but for this case its 1)\n# the CNN model. We will be using the pandas iloc method for extracting all the pixel columns\n\npix_values, labels= df1.iloc[:,1:], df1['label']","metadata":{"execution":{"iopub.status.busy":"2023-08-28T13:58:58.377427Z","iopub.execute_input":"2023-08-28T13:58:58.377826Z","iopub.status.idle":"2023-08-28T13:58:58.384080Z","shell.execute_reply.started":"2023-08-28T13:58:58.377795Z","shell.execute_reply":"2023-08-28T13:58:58.382896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# changing the image format and normalizing the pixel values to a range from 0 to 1`\n\ndef reformat_data(data):\n    data=data.to_numpy()\n    data=data.reshape(data.shape[0],28,28)\n    data=data/255\n    \n    return data","metadata":{"execution":{"iopub.status.busy":"2023-08-28T13:59:00.860720Z","iopub.execute_input":"2023-08-28T13:59:00.861104Z","iopub.status.idle":"2023-08-28T13:59:00.867376Z","shell.execute_reply.started":"2023-08-28T13:59:00.861075Z","shell.execute_reply":"2023-08-28T13:59:00.865936Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"images= reformat_data(pix_values)","metadata":{"execution":{"iopub.status.busy":"2023-08-28T13:59:10.783855Z","iopub.execute_input":"2023-08-28T13:59:10.784300Z","iopub.status.idle":"2023-08-28T13:59:10.921163Z","shell.execute_reply.started":"2023-08-28T13:59:10.784251Z","shell.execute_reply":"2023-08-28T13:59:10.919966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# we will be implementing a CNN (Convolutional Neural Network) architecture which works best for tasks \n# related to computer vision and image classification. The Model will be implemented using the tensorflow Keras APIt\n# with the model consisting of Convolution, Pooling and fully connected layers.\n\ndef Model(learning_rate,activation):\n    CNN_model= tf.keras.Sequential([\n    layers.Conv2D(filters=25, kernel_size=(3, 3), activation=activation, input_shape=(28,28,1)), \n    layers.MaxPooling2D((2, 2)),\n        \n    layers.Conv2D(filters=64, kernel_size=(3, 3), activation=activation),\n    layers.MaxPooling2D((2, 2)),\n        \n    layers.Conv2D(filters=64, kernel_size=(3, 3), activation=activation),\n    layers.MaxPooling2D((2, 2)),\n\n    layers.Flatten(),\n    layers.Dense(64, activation=activation),\n    layers.Dense(10, activation='softmax')                \n    ])\n    \n    opt= keras.optimizers.Adam(learning_rate=learning_rate)\n    CNN_model.compile(optimizer=opt, loss='sparse_categorical_crossentropy', metrics=['accuracy'])\n    \n    return CNN_model","metadata":{"execution":{"iopub.status.busy":"2023-08-28T13:59:13.342741Z","iopub.execute_input":"2023-08-28T13:59:13.343179Z","iopub.status.idle":"2023-08-28T13:59:13.353447Z","shell.execute_reply.started":"2023-08-28T13:59:13.343144Z","shell.execute_reply":"2023-08-28T13:59:13.352147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = Model(0.001,'relu')\nresult = model.fit(images,labels,batch_size=8,epochs=10)","metadata":{"execution":{"iopub.status.busy":"2023-08-28T14:04:30.740393Z","iopub.execute_input":"2023-08-28T14:04:30.740823Z","iopub.status.idle":"2023-08-28T14:12:03.485121Z","shell.execute_reply.started":"2023-08-28T14:04:30.740791Z","shell.execute_reply":"2023-08-28T14:12:03.483242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2.shape","metadata":{"execution":{"iopub.status.busy":"2023-08-28T14:15:14.412186Z","iopub.execute_input":"2023-08-28T14:15:14.412605Z","iopub.status.idle":"2023-08-28T14:15:14.419947Z","shell.execute_reply.started":"2023-08-28T14:15:14.412573Z","shell.execute_reply":"2023-08-28T14:15:14.418614Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df2= pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntest_set= df2.to_numpy().reshape(28000,28,28)","metadata":{"execution":{"iopub.status.busy":"2023-08-28T14:16:47.397591Z","iopub.execute_input":"2023-08-28T14:16:47.398029Z","iopub.status.idle":"2023-08-28T14:16:49.862459Z","shell.execute_reply.started":"2023-08-28T14:16:47.397996Z","shell.execute_reply":"2023-08-28T14:16:49.860960Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred= model.predict(test_set)","metadata":{"execution":{"iopub.status.busy":"2023-08-28T15:08:43.740652Z","iopub.execute_input":"2023-08-28T15:08:43.741081Z","iopub.status.idle":"2023-08-28T15:08:51.177097Z","shell.execute_reply.started":"2023-08-28T15:08:43.741050Z","shell.execute_reply":"2023-08-28T15:08:51.175803Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred=np.argmax(pred,axis=1)","metadata":{"execution":{"iopub.status.busy":"2023-08-28T15:09:45.203140Z","iopub.execute_input":"2023-08-28T15:09:45.203913Z","iopub.status.idle":"2023-08-28T15:09:45.210969Z","shell.execute_reply.started":"2023-08-28T15:09:45.203860Z","shell.execute_reply":"2023-08-28T15:09:45.209384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = pd.DataFrame({'ImageId': range(1, len(pred) + 1), 'Label':pred})\npredictions.to_csv('/kaggle/working/predictions.csv',index=False)","metadata":{"execution":{"iopub.status.busy":"2023-08-28T15:10:46.721770Z","iopub.execute_input":"2023-08-28T15:10:46.722198Z","iopub.status.idle":"2023-08-28T15:10:46.806760Z","shell.execute_reply.started":"2023-08-28T15:10:46.722166Z","shell.execute_reply":"2023-08-28T15:10:46.805187Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}