{"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":"markdown","source":"# Imports","metadata":{}},{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom sklearn.svm import SVC\nfrom sklearn.model_selection import cross_val_score\nfrom sklearn.preprocessing import StandardScaler\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-05-09T11:23:24.104761Z","iopub.execute_input":"2022-05-09T11:23:24.104998Z","iopub.status.idle":"2022-05-09T11:23:24.115831Z","shell.execute_reply.started":"2022-05-09T11:23:24.104975Z","shell.execute_reply":"2022-05-09T11:23:24.115189Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Explore Data","metadata":{}},{"cell_type":"code","source":"# Load training set and view\nmnist_train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\nmnist_train.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-09T10:52:00.769133Z","iopub.execute_input":"2022-05-09T10:52:00.769466Z","iopub.status.idle":"2022-05-09T10:52:02.460565Z","shell.execute_reply.started":"2022-05-09T10:52:00.769405Z","shell.execute_reply":"2022-05-09T10:52:02.459695Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Load test data and view\nmnist_test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\nmnist_test.head()","metadata":{"execution":{"iopub.status.busy":"2022-05-09T10:52:02.461945Z","iopub.execute_input":"2022-05-09T10:52:02.462109Z","iopub.status.idle":"2022-05-09T10:52:03.518426Z","shell.execute_reply.started":"2022-05-09T10:52:02.462088Z","shell.execute_reply":"2022-05-09T10:52:03.517726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Separate target and data of training set\nX_train = mnist_train.loc[:, mnist_train.columns != 'label']\nprint(X_train.shape)\n\ny_train = mnist_train['label']\nprint(y_train.shape)","metadata":{"execution":{"iopub.status.busy":"2022-05-09T10:57:05.449927Z","iopub.execute_input":"2022-05-09T10:57:05.450282Z","iopub.status.idle":"2022-05-09T10:57:05.503371Z","shell.execute_reply.started":"2022-05-09T10:57:05.450258Z","shell.execute_reply":"2022-05-09T10:57:05.502769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# View a digit from training set\ndigit = X_train.iloc[0].values\ndigit_img = digit.reshape((28, 28))\n\n# Display this array using matplotlib\nplt.imshow(digit_img, cmap=\"binary\")\nplt.axis(\"off\")\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2022-05-09T10:57:05.505281Z","iopub.execute_input":"2022-05-09T10:57:05.505530Z","iopub.status.idle":"2022-05-09T10:57:05.571977Z","shell.execute_reply.started":"2022-05-09T10:57:05.505499Z","shell.execute_reply":"2022-05-09T10:57:05.571317Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(y_train.iloc[0])","metadata":{"execution":{"iopub.status.busy":"2022-05-09T10:57:05.575955Z","iopub.execute_input":"2022-05-09T10:57:05.576624Z","iopub.status.idle":"2022-05-09T10:57:05.581524Z","shell.execute_reply.started":"2022-05-09T10:57:05.576568Z","shell.execute_reply":"2022-05-09T10:57:05.580897Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"*We see that the image displayed seems like '1' and the label for the image is also '1'* (type: numpy.int64).","metadata":{}},{"cell_type":"markdown","source":"## Select and Train a Model","metadata":{}},{"cell_type":"markdown","source":"### Train a Support Vector Classifier","metadata":{}},{"cell_type":"code","source":"svm_clf = SVC()\nsvm_clf.fit(X_train.values, y_train.values)","metadata":{"execution":{"iopub.status.busy":"2022-05-09T11:18:00.747714Z","iopub.execute_input":"2022-05-09T11:18:00.748430Z","iopub.status.idle":"2022-05-09T11:20:17.835124Z","shell.execute_reply.started":"2022-05-09T11:18:00.748384Z","shell.execute_reply":"2022-05-09T11:20:17.834039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"svm_clf.predict([digit])","metadata":{"execution":{"iopub.status.busy":"2022-05-09T11:21:11.725073Z","iopub.execute_input":"2022-05-09T11:21:11.725500Z","iopub.status.idle":"2022-05-09T11:21:11.738332Z","shell.execute_reply.started":"2022-05-09T11:21:11.725462Z","shell.execute_reply":"2022-05-09T11:21:11.737935Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Find out the score of decision function for our input among all classes\ndigit_score = svm_clf.decision_function([digit])\nprint(digit_score)","metadata":{"execution":{"iopub.status.busy":"2022-05-09T11:21:24.504121Z","iopub.execute_input":"2022-05-09T11:21:24.504567Z","iopub.status.idle":"2022-05-09T11:21:24.520059Z","shell.execute_reply.started":"2022-05-09T11:21:24.504533Z","shell.execute_reply":"2022-05-09T11:21:24.519251Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"value = np.max(digit_score)\nindex = np.argmax(digit_score)\n\nprint(f\"The SVC's decision function scores input image {value} at index {index}.\")\nprint(f\"At index {index} the value is {svm_clf.classes_[index]}\")","metadata":{"execution":{"iopub.status.busy":"2022-05-09T11:28:55.921824Z","iopub.execute_input":"2022-05-09T11:28:55.922586Z","iopub.status.idle":"2022-05-09T11:28:55.926774Z","shell.execute_reply.started":"2022-05-09T11:28:55.922558Z","shell.execute_reply":"2022-05-09T11:28:55.926338Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"The cross-validation score of model trained using SVC is: \")\ncross_val_score(svm_clf, X_train.values, y_train.values, cv=3, scoring=\"accuracy\")","metadata":{"execution":{"iopub.status.busy":"2022-05-09T11:32:45.880786Z","iopub.execute_input":"2022-05-09T11:32:45.881237Z","iopub.status.idle":"2022-05-09T11:39:57.582589Z","shell.execute_reply.started":"2022-05-09T11:32:45.881207Z","shell.execute_reply":"2022-05-09T11:39:57.581278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Submit Model","metadata":{}},{"cell_type":"code","source":"# Make predictions on test set\npredictions = svm_clf.predict(mnist_test.values)","metadata":{"execution":{"iopub.status.busy":"2022-05-09T12:00:47.711588Z","iopub.execute_input":"2022-05-09T12:00:47.711813Z","iopub.status.idle":"2022-05-09T12:04:11.702214Z","shell.execute_reply.started":"2022-05-09T12:00:47.711790Z","shell.execute_reply":"2022-05-09T12:04:11.701328Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Convert predictions to DataFrame\noutput = pd.DataFrame({'ImageId': list(range(1, len(predictions)+1)),\n                      'Label': predictions})\n# Convert dataframe to csv file\noutput.to_csv('submission.csv', index=False)\nprint(\"Submitted successfully!\")","metadata":{"execution":{"iopub.status.busy":"2022-05-09T12:53:32.437631Z","iopub.execute_input":"2022-05-09T12:53:32.438091Z","iopub.status.idle":"2022-05-09T12:53:32.479334Z","shell.execute_reply.started":"2022-05-09T12:53:32.438055Z","shell.execute_reply":"2022-05-09T12:53:32.478848Z"},"trusted":true},"execution_count":null,"outputs":[]}]}