{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":3004,"databundleVersionId":861823,"sourceType":"competition"}],"dockerImageVersionId":30646,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport matplotlib.pyplot as plt, matplotlib.image as mpimg\nfrom sklearn.model_selection import train_test_split\nfrom sklearn import svm\n%matplotlib inline","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-02-13T13:07:51.996288Z","iopub.execute_input":"2024-02-13T13:07:51.996739Z","iopub.status.idle":"2024-02-13T13:07:52.004407Z","shell.execute_reply.started":"2024-02-13T13:07:51.996704Z","shell.execute_reply":"2024-02-13T13:07:52.003323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# store csv file in a DATAFRAME\nlabeled_images = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\nlabeled_images.head()\n# NOTE :- Label is the first col (0th index)\n# That 0,1,2,3... at the leftmost col is => default rownames (not actual col)","metadata":{"execution":{"iopub.status.busy":"2024-02-13T13:07:55.502099Z","iopub.execute_input":"2024-02-13T13:07:55.502539Z","iopub.status.idle":"2024-02-13T13:07:58.646132Z","shell.execute_reply.started":"2024-02-13T13:07:55.502505Z","shell.execute_reply":"2024-02-13T13:07:58.644929Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labeled_images.shape #what are the dimensions of dataframe => .shape\n# 42000 images\n# 1 output / label\n# image size = 784","metadata":{"execution":{"iopub.status.busy":"2024-02-13T13:08:02.354459Z","iopub.execute_input":"2024-02-13T13:08:02.354880Z","iopub.status.idle":"2024-02-13T13:08:02.361625Z","shell.execute_reply.started":"2024-02-13T13:08:02.354830Z","shell.execute_reply":"2024-02-13T13:08:02.360343Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# images is now an array (subsetting of an dataframe applied using .iloc)\nimages = labeled_images.iloc[0:5000,1:] # to make it simple, we are taking only 5000 photos\nlabels = labeled_images.iloc[0:5000,0:1] # First col is labels\nlabels.head()","metadata":{"execution":{"iopub.status.busy":"2024-02-13T13:08:04.858500Z","iopub.execute_input":"2024-02-13T13:08:04.858912Z","iopub.status.idle":"2024-02-13T13:08:04.869872Z","shell.execute_reply.started":"2024-02-13T13:08:04.858877Z","shell.execute_reply":"2024-02-13T13:08:04.868596Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Learning to split a single dataset into Train + Test. (even though we have a test csv file already :>)\n# All DATAFRAMES\ntrain_images, test_images, train_labels, test_labels = train_test_split(images, labels, train_size=0.8, random_state=0)\n# train_test_spit(<images to split into train/test>, <labels to...>, <train_size>, <random_state>)\nprint(\"train_images = \", type(train_images), \"\\ntest_images = \", type(test_images), \"\\ntrain_labels = \", type(train_labels), \"\\ntest_labels = \", type(test_labels))","metadata":{"execution":{"iopub.status.busy":"2024-02-13T13:09:43.311093Z","iopub.execute_input":"2024-02-13T13:09:43.311556Z","iopub.status.idle":"2024-02-13T13:09:43.337696Z","shell.execute_reply.started":"2024-02-13T13:09:43.311521Z","shell.execute_reply":"2024-02-13T13:09:43.336195Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels.head() #training dataset b/w 0-5000","metadata":{"execution":{"iopub.status.busy":"2024-02-13T13:10:05.350392Z","iopub.execute_input":"2024-02-13T13:10:05.351193Z","iopub.status.idle":"2024-02-13T13:10:05.361615Z","shell.execute_reply.started":"2024-02-13T13:10:05.351148Z","shell.execute_reply":"2024-02-13T13:10:05.360358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i=1\nimg=np.array(train_images.iloc[i]) #convert normal-array -> numpy-array\nimg=img.reshape((28,28)) # 784 is the size of the image\nplt.imshow(img,cmap='gray') # gray commonly used\nplt.title(train_labels.iloc[i,0]) ","metadata":{"execution":{"iopub.status.busy":"2024-02-13T13:10:08.321248Z","iopub.execute_input":"2024-02-13T13:10:08.321903Z","iopub.status.idle":"2024-02-13T13:10:08.575222Z","shell.execute_reply.started":"2024-02-13T13:10:08.321871Z","shell.execute_reply":"2024-02-13T13:10:08.574026Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Pixel Intensity of the 'ith' image\nplt.hist(train_images.iloc[i])","metadata":{"execution":{"iopub.status.busy":"2024-02-13T13:10:10.923208Z","iopub.execute_input":"2024-02-13T13:10:10.923621Z","iopub.status.idle":"2024-02-13T13:10:11.151168Z","shell.execute_reply.started":"2024-02-13T13:10:10.923588Z","shell.execute_reply":"2024-02-13T13:10:11.149920Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Training Model using SVM\nclf = svm.SVC(kernel='linear', C=1.0) #SVC is a type of svm classifier\n\n# This line trains the SVC classifier on the training data\n# '.values.ravel()' is used to convert the DataFrame 'labels' to a 1D NumPy array, which is the expected format for the fit() method\nclf.fit(train_images, train_labels.values.ravel())\nclf.score(test_images,test_labels)","metadata":{"execution":{"iopub.status.busy":"2024-02-13T13:10:14.697826Z","iopub.execute_input":"2024-02-13T13:10:14.698235Z","iopub.status.idle":"2024-02-13T13:10:16.417755Z","shell.execute_reply.started":"2024-02-13T13:10:14.698205Z","shell.execute_reply":"2024-02-13T13:10:16.416573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Image Preprocessing\ntest_images[test_images>0]=1 \ntrain_images[train_images>0]=1\n# binarize the image data by setting any pixel value greater than 0 to 1\n\nimg=np.array(train_images.iloc[i]).reshape((28,28)) #This step is necessary for visualizing the image correctly\nplt.imshow(img,cmap='binary') # since only 2 pixels\nplt.title(str(train_labels.iloc[i]))","metadata":{"execution":{"iopub.status.busy":"2024-02-13T13:10:45.750564Z","iopub.execute_input":"2024-02-13T13:10:45.751084Z","iopub.status.idle":"2024-02-13T13:10:46.077821Z","shell.execute_reply.started":"2024-02-13T13:10:45.751047Z","shell.execute_reply":"2024-02-13T13:10:46.076685Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# only 0s and 1s\nplt.hist(train_images.iloc[i])","metadata":{"execution":{"iopub.status.busy":"2024-02-13T13:10:49.120033Z","iopub.execute_input":"2024-02-13T13:10:49.120615Z","iopub.status.idle":"2024-02-13T13:10:49.351772Z","shell.execute_reply.started":"2024-02-13T13:10:49.120576Z","shell.execute_reply":"2024-02-13T13:10:49.350778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Retraining our model\nclf = svm.SVC()\nclf.fit(train_images, train_labels.values.ravel())\nclf.score(test_images,test_labels)","metadata":{"execution":{"iopub.status.busy":"2024-02-13T13:10:51.888300Z","iopub.execute_input":"2024-02-13T13:10:51.888688Z","iopub.status.idle":"2024-02-13T13:10:55.360777Z","shell.execute_reply.started":"2024-02-13T13:10:51.888658Z","shell.execute_reply":"2024-02-13T13:10:55.359655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load the test file and make predcition\ntest_data=pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\ntest_data[test_data>0]=1\nresults=clf.predict(test_data)\nresults # an output for each label","metadata":{"execution":{"iopub.status.busy":"2024-02-13T13:10:57.971663Z","iopub.execute_input":"2024-02-13T13:10:57.972069Z","iopub.status.idle":"2024-02-13T13:11:36.309048Z","shell.execute_reply.started":"2024-02-13T13:10:57.972037Z","shell.execute_reply":"2024-02-13T13:11:36.307840Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Now make your output match the format of the \"sample_submission.csv\"\ndf = pd.DataFrame(results)\ndf # check default\n# left column => 'index column' => nameless (NOT A COLUMN)\n# right column => ACTUAL COLUMNS START","metadata":{"execution":{"iopub.status.busy":"2024-02-13T13:11:39.535727Z","iopub.execute_input":"2024-02-13T13:11:39.536116Z","iopub.status.idle":"2024-02-13T13:11:39.548585Z","shell.execute_reply.started":"2024-02-13T13:11:39.536074Z","shell.execute_reply":"2024-02-13T13:11:39.547339Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.index += 1 # start from 1\ndf.index.name = 'ImageId'\ndf.columns = ['Label']\ndf.to_csv('results.csv', header = True)","metadata":{"execution":{"iopub.status.busy":"2024-02-13T13:11:42.307196Z","iopub.execute_input":"2024-02-13T13:11:42.307608Z","iopub.status.idle":"2024-02-13T13:11:42.350544Z","shell.execute_reply.started":"2024-02-13T13:11:42.307574Z","shell.execute_reply":"2024-02-13T13:11:42.349586Z"},"trusted":true},"execution_count":null,"outputs":[]}]}