{"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":30732,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\n%matplotlib inline\n\nfrom sklearn import svm\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.multiclass import OneVsRestClassifier","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-06-17T08:16:46.803046Z","iopub.execute_input":"2024-06-17T08:16:46.803481Z","iopub.status.idle":"2024-06-17T08:16:46.811818Z","shell.execute_reply.started":"2024-06-17T08:16:46.803448Z","shell.execute_reply":"2024-06-17T08:16:46.810532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"../input/digit-recognizer/train.csv\")\ntest_df = pd.read_csv(\"../input/digit-recognizer/test.csv\")\nsample_df = pd.read_csv(\"../input/digit-recognizer/sample_submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2024-06-17T08:34:21.570182Z","iopub.execute_input":"2024-06-17T08:34:21.571497Z","iopub.status.idle":"2024-06-17T08:34:26.951604Z","shell.execute_reply.started":"2024-06-17T08:34:21.571455Z","shell.execute_reply":"2024-06-17T08:34:26.950418Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-06-17T08:03:10.437428Z","iopub.execute_input":"2024-06-17T08:03:10.438783Z","iopub.status.idle":"2024-06-17T08:03:10.472629Z","shell.execute_reply.started":"2024-06-17T08:03:10.438729Z","shell.execute_reply":"2024-06-17T08:03:10.471346Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-06-17T08:03:27.853025Z","iopub.execute_input":"2024-06-17T08:03:27.853456Z","iopub.status.idle":"2024-06-17T08:03:27.874726Z","shell.execute_reply.started":"2024-06-17T08:03:27.853425Z","shell.execute_reply":"2024-06-17T08:03:27.873586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_df.head()","metadata":{"execution":{"iopub.status.busy":"2024-06-17T08:34:29.629206Z","iopub.execute_input":"2024-06-17T08:34:29.629759Z","iopub.status.idle":"2024-06-17T08:34:29.642856Z","shell.execute_reply.started":"2024-06-17T08:34:29.629716Z","shell.execute_reply":"2024-06-17T08:34:29.641411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x = train_df.iloc[:, 1:].values\ntrain_y = train_df[\"label\"].values\ntest_x = test_df.values","metadata":{"execution":{"iopub.status.busy":"2024-06-17T08:04:36.333738Z","iopub.execute_input":"2024-06-17T08:04:36.334236Z","iopub.status.idle":"2024-06-17T08:04:36.341210Z","shell.execute_reply.started":"2024-06-17T08:04:36.334192Z","shell.execute_reply":"2024-06-17T08:04:36.339659Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.imshow(train_x[0, :].reshape((28, 28)))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-06-17T08:17:37.788754Z","iopub.execute_input":"2024-06-17T08:17:37.789643Z","iopub.status.idle":"2024-06-17T08:17:38.048318Z","shell.execute_reply.started":"2024-06-17T08:17:37.789605Z","shell.execute_reply":"2024-06-17T08:17:38.046641Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_y[0]","metadata":{"execution":{"iopub.status.busy":"2024-06-17T08:17:51.015416Z","iopub.execute_input":"2024-06-17T08:17:51.015868Z","iopub.status.idle":"2024-06-17T08:17:51.024015Z","shell.execute_reply.started":"2024-06-17T08:17:51.015835Z","shell.execute_reply":"2024-06-17T08:17:51.022896Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_x.shape, train_y.shape, test_x.shape","metadata":{"execution":{"iopub.status.busy":"2024-06-17T08:04:51.464525Z","iopub.execute_input":"2024-06-17T08:04:51.464933Z","iopub.status.idle":"2024-06-17T08:04:51.473472Z","shell.execute_reply.started":"2024-06-17T08:04:51.464903Z","shell.execute_reply":"2024-06-17T08:04:51.471898Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"SVC model with default setting(one-vs-one)  \nデフォルト設定（one-vs-one）でSVC","metadata":{}},{"cell_type":"code","source":"# train SVC using 1000 records\nmodel = svm.SVC(kernel=\"rbf\")\nmodel.fit(train_x[:1000], train_y[:1000])","metadata":{"execution":{"iopub.status.busy":"2024-06-17T08:10:45.808882Z","iopub.execute_input":"2024-06-17T08:10:45.809420Z","iopub.status.idle":"2024-06-17T08:10:46.079149Z","shell.execute_reply.started":"2024-06-17T08:10:45.809380Z","shell.execute_reply":"2024-06-17T08:10:46.077863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predict train data (first 1000) and valid data (next 1000) 学習と検証（次の1000件）に適用\npred_train = model.predict(train_x[:1000])\npred_valid = model.predict(train_x[1000:2000])","metadata":{"execution":{"iopub.status.busy":"2024-06-17T08:10:46.929419Z","iopub.execute_input":"2024-06-17T08:10:46.929900Z","iopub.status.idle":"2024-06-17T08:10:47.857970Z","shell.execute_reply.started":"2024-06-17T08:10:46.929865Z","shell.execute_reply":"2024-06-17T08:10:47.856534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# confusion matrix(混同行列) / train\npd.crosstab(pred_train, train_y[:1000])","metadata":{"execution":{"iopub.status.busy":"2024-06-17T08:19:46.626644Z","iopub.execute_input":"2024-06-17T08:19:46.627084Z","iopub.status.idle":"2024-06-17T08:19:46.655792Z","shell.execute_reply.started":"2024-06-17T08:19:46.627051Z","shell.execute_reply":"2024-06-17T08:19:46.654204Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# confusion matrix(混同行列) / validation\npd.crosstab(pred_valid, train_y[1000:2000])","metadata":{"execution":{"iopub.status.busy":"2024-06-17T08:19:50.499475Z","iopub.execute_input":"2024-06-17T08:19:50.499933Z","iopub.status.idle":"2024-06-17T08:19:50.536307Z","shell.execute_reply.started":"2024-06-17T08:19:50.499902Z","shell.execute_reply":"2024-06-17T08:19:50.534684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(pred_train == train_y[:1000]).mean(), (pred_valid == train_y[1000:2000]).mean()","metadata":{"execution":{"iopub.status.busy":"2024-06-17T08:21:07.941490Z","iopub.execute_input":"2024-06-17T08:21:07.942492Z","iopub.status.idle":"2024-06-17T08:21:07.951060Z","shell.execute_reply.started":"2024-06-17T08:21:07.942456Z","shell.execute_reply":"2024-06-17T08:21:07.949673Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"one-vs-rest","metadata":{}},{"cell_type":"code","source":"model_ovr = OneVsRestClassifier(svm.SVC(kernel=\"rbf\"))\nmodel_ovr.fit(train_x[:1000], train_y[:1000])","metadata":{"execution":{"iopub.status.busy":"2024-06-17T08:29:51.577553Z","iopub.execute_input":"2024-06-17T08:29:51.578441Z","iopub.status.idle":"2024-06-17T08:29:52.324771Z","shell.execute_reply.started":"2024-06-17T08:29:51.578404Z","shell.execute_reply":"2024-06-17T08:29:52.323691Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# predict train data (first 1000) and valid data (next 1000) 学習と検証（次の1000件）に適用\npred_train = model_ovr.predict(train_x[:1000])\npred_valid = model_ovr.predict(train_x[1000:2000])","metadata":{"execution":{"iopub.status.busy":"2024-06-17T08:29:52.326444Z","iopub.execute_input":"2024-06-17T08:29:52.326776Z","iopub.status.idle":"2024-06-17T08:29:55.027862Z","shell.execute_reply.started":"2024-06-17T08:29:52.326749Z","shell.execute_reply":"2024-06-17T08:29:55.026554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# confusion matrix(混同行列) / train\npd.crosstab(pred_train, train_y[:1000])","metadata":{"execution":{"iopub.status.busy":"2024-06-17T08:29:55.030745Z","iopub.execute_input":"2024-06-17T08:29:55.031173Z","iopub.status.idle":"2024-06-17T08:29:55.060576Z","shell.execute_reply.started":"2024-06-17T08:29:55.031124Z","shell.execute_reply":"2024-06-17T08:29:55.059183Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# confusion matrix(混同行列) / validation\npd.crosstab(pred_valid, train_y[1000:2000])","metadata":{"execution":{"iopub.status.busy":"2024-06-17T08:29:55.062066Z","iopub.execute_input":"2024-06-17T08:29:55.062421Z","iopub.status.idle":"2024-06-17T08:29:55.089913Z","shell.execute_reply.started":"2024-06-17T08:29:55.062392Z","shell.execute_reply":"2024-06-17T08:29:55.088396Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"(pred_train == train_y[:1000]).mean(), (pred_valid == train_y[1000:2000]).mean()","metadata":{"execution":{"iopub.status.busy":"2024-06-17T08:30:01.694800Z","iopub.execute_input":"2024-06-17T08:30:01.695286Z","iopub.status.idle":"2024-06-17T08:30:01.704835Z","shell.execute_reply.started":"2024-06-17T08:30:01.695251Z","shell.execute_reply":"2024-06-17T08:30:01.703432Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# train and submit\n# use all train data\nmodel_best = svm.SVC(kernel=\"rbf\")\nmodel_best.fit(train_x, train_y)","metadata":{"execution":{"iopub.status.busy":"2024-06-17T08:13:15.839837Z","iopub.execute_input":"2024-06-17T08:13:15.840258Z","iopub.status.idle":"2024-06-17T08:15:53.896981Z","shell.execute_reply.started":"2024-06-17T08:13:15.840228Z","shell.execute_reply":"2024-06-17T08:15:53.895858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_test = model_best.predict(test_x)\nsub_df = pd.DataFrame({\n    \"ImageId\": sample_df[\"ImageId\"], \n    \"Label\": pred_test\n})\nsub_df.to_csv(\"submission_svc.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-06-17T08:35:47.264485Z","iopub.execute_input":"2024-06-17T08:35:47.264983Z","iopub.status.idle":"2024-06-17T08:35:47.306054Z","shell.execute_reply.started":"2024-06-17T08:35:47.264945Z","shell.execute_reply":"2024-06-17T08:35:47.303893Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}