{"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-10-25T05:32:30.116858Z","iopub.execute_input":"2023-10-25T05:32:30.117381Z","iopub.status.idle":"2023-10-25T05:32:30.573667Z","shell.execute_reply.started":"2023-10-25T05:32:30.117336Z","shell.execute_reply":"2023-10-25T05:32:30.572391Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Loading Data\ntrain = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-10-25T05:32:31.667363Z","iopub.execute_input":"2023-10-25T05:32:31.667905Z","iopub.status.idle":"2023-10-25T05:32:38.647608Z","shell.execute_reply.started":"2023-10-25T05:32:31.667870Z","shell.execute_reply":"2023-10-25T05:32:38.646278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train.shape)\nprint(test.shape)","metadata":{"execution":{"iopub.status.busy":"2023-10-25T05:32:38.649431Z","iopub.execute_input":"2023-10-25T05:32:38.650184Z","iopub.status.idle":"2023-10-25T05:32:38.656070Z","shell.execute_reply.started":"2023-10-25T05:32:38.650130Z","shell.execute_reply":"2023-10-25T05:32:38.654714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"''''\nVisualization of the train data with the help of matplotlib \nto understand the data more clearly\nAs  we can that we have total 784 column of pixel which will eventually be 28X28 sqaure\nso in dataset when the value of pixel is 1 it will generate number in 28X28 square\nAnd we can visualize the number as below\n'''\nimport matplotlib.pyplot as plt\nnum = np.array(train.iloc[7,1:]).reshape(28,28)\nplt.imshow(num)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-25T05:32:48.411962Z","iopub.execute_input":"2023-10-25T05:32:48.412358Z","iopub.status.idle":"2023-10-25T05:32:48.664394Z","shell.execute_reply.started":"2023-10-25T05:32:48.412325Z","shell.execute_reply":"2023-10-25T05:32:48.662672Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\"\"\"\"\nThe range lies from 0 - 255 which 256 \nso we can say that it as a RGB layer it 3 channels of Red Green Blue and gives color \nThese 3 channels superimpose together to gives us the color where the number lies \nAnd we the help of these it becomes easy to visualize the data on 28X28 matrix\n\"\"\"\nprint(\"Maximun number of array :\",np.max(num))\nplt.hist(num)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-25T05:32:49.747961Z","iopub.execute_input":"2023-10-25T05:32:49.748856Z","iopub.status.idle":"2023-10-25T05:32:50.461364Z","shell.execute_reply.started":"2023-10-25T05:32:49.748810Z","shell.execute_reply":"2023-10-25T05:32:50.460050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x = np.array(train)\ny = np.array(test)\n\nx_train = x[:,1:]\ny_train = x[:,0]\nx_test = y","metadata":{"execution":{"iopub.status.busy":"2023-10-25T05:32:55.517670Z","iopub.execute_input":"2023-10-25T05:32:55.518069Z","iopub.status.idle":"2023-10-25T05:32:55.760457Z","shell.execute_reply.started":"2023-10-25T05:32:55.518038Z","shell.execute_reply":"2023-10-25T05:32:55.759246Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#importing Keras lib for ImageDataGenerator for different position,size and zooming of number on the plane\nfrom keras.preprocessing.image import ImageDataGenerator\nx_train = x_train.reshape(-1,28,28,1)","metadata":{"execution":{"iopub.status.busy":"2023-10-25T05:32:56.621507Z","iopub.execute_input":"2023-10-25T05:32:56.621893Z","iopub.status.idle":"2023-10-25T05:33:06.414918Z","shell.execute_reply.started":"2023-10-25T05:32:56.621864Z","shell.execute_reply":"2023-10-25T05:33:06.413630Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"datagen = ImageDataGenerator(\n    height_shift_range=0.1,\n    shear_range=0.1,\n    zoom_range=0.1,\n    width_shift_range=0.1,\n    rotation_range=15,\n)\ndatagen.fit(x_train)","metadata":{"execution":{"iopub.status.busy":"2023-10-25T05:33:06.417385Z","iopub.execute_input":"2023-10-25T05:33:06.418252Z","iopub.status.idle":"2023-10-25T05:33:06.581954Z","shell.execute_reply.started":"2023-10-25T05:33:06.418209Z","shell.execute_reply":"2023-10-25T05:33:06.580463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train = x_train.reshape(-1,784)","metadata":{"execution":{"iopub.status.busy":"2023-10-25T05:33:06.584027Z","iopub.execute_input":"2023-10-25T05:33:06.584871Z","iopub.status.idle":"2023-10-25T05:33:06.591046Z","shell.execute_reply.started":"2023-10-25T05:33:06.584826Z","shell.execute_reply":"2023-10-25T05:33:06.589856Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Generating our model with the KNN - Classifier\nfrom sklearn.neighbors import KNeighborsClassifier\nknn = KNeighborsClassifier(n_neighbors=5)\nknn.fit(x_train , y_train)","metadata":{"execution":{"iopub.status.busy":"2023-10-25T05:33:06.593872Z","iopub.execute_input":"2023-10-25T05:33:06.594917Z","iopub.status.idle":"2023-10-25T05:33:07.658578Z","shell.execute_reply.started":"2023-10-25T05:33:06.594873Z","shell.execute_reply":"2023-10-25T05:33:07.657000Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Checking Score of our training data\nprint(\"The score of training Data :\",round(knn.score(x_train , y_train)*100,2))","metadata":{"execution":{"iopub.status.busy":"2023-10-25T05:33:07.659917Z","iopub.execute_input":"2023-10-25T05:33:07.660343Z","iopub.status.idle":"2023-10-25T05:34:31.205679Z","shell.execute_reply.started":"2023-10-25T05:33:07.660311Z","shell.execute_reply":"2023-10-25T05:34:31.204465Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = knn.predict(x_test)\n#Predicting our model with different testCases and comparing it with graph below to check its accuarcy\nprediction = int(knn.predict(x_test[55].reshape(1,-1)))\nprint(\"Predicted Digit :\",prediction)","metadata":{"execution":{"iopub.status.busy":"2023-10-25T05:34:34.217054Z","iopub.execute_input":"2023-10-25T05:34:34.217469Z","iopub.status.idle":"2023-10-25T05:35:26.701743Z","shell.execute_reply.started":"2023-10-25T05:34:34.217437Z","shell.execute_reply":"2023-10-25T05:35:26.697272Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import matplotlib.pyplot as plt\nnum = np.array(test.iloc[55]).reshape(28,28)\nplt.imshow(num)\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-10-25T05:35:52.552551Z","iopub.execute_input":"2023-10-25T05:35:52.552973Z","iopub.status.idle":"2023-10-25T05:35:52.742291Z","shell.execute_reply.started":"2023-10-25T05:35:52.552940Z","shell.execute_reply":"2023-10-25T05:35:52.741142Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"output = pd.read_csv('/kaggle/input/digit-recognizer/sample_submission.csv')\n\noutput.Label = y_pred\noutput.to_csv('submission.csv',index = False)","metadata":{"execution":{"iopub.status.busy":"2023-10-25T05:35:49.656082Z","iopub.execute_input":"2023-10-25T05:35:49.656521Z","iopub.status.idle":"2023-10-25T05:35:49.768739Z","shell.execute_reply.started":"2023-10-25T05:35:49.656488Z","shell.execute_reply":"2023-10-25T05:35:49.767616Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2023-10-25T05:36:28.096626Z","iopub.execute_input":"2023-10-25T05:36:28.097391Z","iopub.status.idle":"2023-10-25T05:36:28.111469Z","shell.execute_reply.started":"2023-10-25T05:36:28.097351Z","shell.execute_reply":"2023-10-25T05:36:28.110564Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}