{"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":"## Import libraries\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport numpy as np\n%matplotlib inline\n\nfrom sklearn.model_selection import train_test_split","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-07T14:09:11.248077Z","iopub.execute_input":"2023-11-07T14:09:11.248521Z","iopub.status.idle":"2023-11-07T14:09:12.385999Z","shell.execute_reply.started":"2023-11-07T14:09:11.248486Z","shell.execute_reply":"2023-11-07T14:09:12.384743Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## read the data\ndata = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ntest_data = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')\nX = data.drop(['label'], axis=1)\ny = data['label']\n\n## split the data\nx_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-11-07T14:09:12.388217Z","iopub.execute_input":"2023-11-07T14:09:12.388754Z","iopub.status.idle":"2023-11-07T14:09:20.475575Z","shell.execute_reply.started":"2023-11-07T14:09:12.388723Z","shell.execute_reply":"2023-11-07T14:09:20.474259Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## show an image\ni=1\nimg = x_train.iloc[i]\nimg = np.array(img).reshape((28, 28))\nplt.imshow(img, cmap='gray')\nplt.title(y_train.iloc[i])","metadata":{"execution":{"iopub.status.busy":"2023-11-07T14:09:20.478550Z","iopub.execute_input":"2023-11-07T14:09:20.481902Z","iopub.status.idle":"2023-11-07T14:09:20.916906Z","shell.execute_reply.started":"2023-11-07T14:09:20.481840Z","shell.execute_reply":"2023-11-07T14:09:20.915647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Show the amount of color in the image\nplt.hist(x_train.iloc[i])","metadata":{"execution":{"iopub.status.busy":"2023-11-07T14:09:20.920035Z","iopub.execute_input":"2023-11-07T14:09:20.920584Z","iopub.status.idle":"2023-11-07T14:09:21.166499Z","shell.execute_reply.started":"2023-11-07T14:09:20.920542Z","shell.execute_reply":"2023-11-07T14:09:21.165327Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## SVC\nfrom sklearn.svm import SVC\nsvc = SVC().fit(x_train, y_train)\ny_pred1 = svc.predict(test_data)\nacc_svc = svc.score(x_test, y_test)\nacc_svc","metadata":{"execution":{"iopub.status.busy":"2023-11-07T14:09:21.168013Z","iopub.execute_input":"2023-11-07T14:09:21.168382Z","iopub.status.idle":"2023-11-07T14:17:27.956104Z","shell.execute_reply.started":"2023-11-07T14:09:21.168333Z","shell.execute_reply":"2023-11-07T14:17:27.954786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## Change every values > 0 to 1\nx_train[x_train > 0] = 1\nx_test[x_test > 0] = 1","metadata":{"execution":{"iopub.status.busy":"2023-11-07T14:17:27.958424Z","iopub.execute_input":"2023-11-07T14:17:27.958900Z","iopub.status.idle":"2023-11-07T14:17:28.604201Z","shell.execute_reply.started":"2023-11-07T14:17:27.958856Z","shell.execute_reply":"2023-11-07T14:17:28.602594Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test.describe()","metadata":{"execution":{"iopub.status.busy":"2023-11-07T14:17:28.606470Z","iopub.execute_input":"2023-11-07T14:17:28.607171Z","iopub.status.idle":"2023-11-07T14:17:30.425111Z","shell.execute_reply.started":"2023-11-07T14:17:28.607135Z","shell.execute_reply":"2023-11-07T14:17:30.423892Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## show img\ni = 10\nimg = np.array(x_train.iloc[i]).reshape((28, 28))\nplt.imshow(img, cmap='gray')\nplt.title(y_train.iloc[i])\nplt.show()\n\nimg = np.array(x_test.iloc[i]).reshape((28, 28))\nplt.imshow(img, cmap='gray')\nplt.title(y_test.iloc[i])","metadata":{"execution":{"iopub.status.busy":"2023-11-07T14:17:30.426909Z","iopub.execute_input":"2023-11-07T14:17:30.430711Z","iopub.status.idle":"2023-11-07T14:17:30.935235Z","shell.execute_reply.started":"2023-11-07T14:17:30.430667Z","shell.execute_reply":"2023-11-07T14:17:30.933913Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## show the hist\nplt.hist(x_train.iloc[i])","metadata":{"execution":{"iopub.status.busy":"2023-11-07T14:17:30.939846Z","iopub.execute_input":"2023-11-07T14:17:30.941108Z","iopub.status.idle":"2023-11-07T14:17:31.244991Z","shell.execute_reply.started":"2023-11-07T14:17:30.941053Z","shell.execute_reply":"2023-11-07T14:17:31.243479Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## use SVC\nsvc2 = SVC().fit(x_train, y_train)\ny_pred2 = svc.predict(test_data)\nacc_svc2 = svc2.score(x_test, y_test)\nacc_svc2","metadata":{"execution":{"iopub.status.busy":"2023-11-07T14:17:31.246564Z","iopub.execute_input":"2023-11-07T14:17:31.246950Z","iopub.status.idle":"2023-11-07T14:25:50.742803Z","shell.execute_reply.started":"2023-11-07T14:17:31.246920Z","shell.execute_reply":"2023-11-07T14:25:50.741462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"i = 15\nimg = np.array(test_data.iloc[i]).reshape((28, 28))\nplt.imshow(img, cmap='gray')\nplt.title(y_pred1[i])","metadata":{"execution":{"iopub.status.busy":"2023-11-07T14:25:50.744572Z","iopub.execute_input":"2023-11-07T14:25:50.745513Z","iopub.status.idle":"2023-11-07T14:25:51.094660Z","shell.execute_reply.started":"2023-11-07T14:25:50.745467Z","shell.execute_reply":"2023-11-07T14:25:51.093400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame(columns=['ImageId', 'Label'])\nsubmission['ImageId'] = range(1, len(test_data) + 1)\nsubmission['Label'] = y_pred1\nsubmission.to_csv('/kaggle/working/submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2023-11-07T14:25:51.096743Z","iopub.execute_input":"2023-11-07T14:25:51.097640Z","iopub.status.idle":"2023-11-07T14:25:51.175927Z","shell.execute_reply.started":"2023-11-07T14:25:51.097580Z","shell.execute_reply":"2023-11-07T14:25:51.174713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from joblib import dump, load\n\ndump(svc, 'svc.model')\ndump(svc2, 'svc2.model')","metadata":{"execution":{"iopub.status.busy":"2023-11-07T14:27:19.779343Z","iopub.execute_input":"2023-11-07T14:27:19.779843Z","iopub.status.idle":"2023-11-07T14:27:19.905829Z","shell.execute_reply.started":"2023-11-07T14:27:19.779807Z","shell.execute_reply":"2023-11-07T14:27:19.904670Z"},"trusted":true},"execution_count":null,"outputs":[]}]}