{"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":"from sklearn import svm\nfrom sklearn.datasets import load_digits\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.neighbors import KNeighborsClassifier as KNN\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.pipeline import make_pipeline, Pipeline\nfrom sklearn.decomposition import TruncatedSVD\nimport matplotlib.pyplot as plt\nimport numpy as np\nimport pandas as pd","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-09-16T20:17:38.825767Z","iopub.execute_input":"2022-09-16T20:17:38.826597Z","iopub.status.idle":"2022-09-16T20:17:38.833279Z","shell.execute_reply.started":"2022-09-16T20:17:38.826561Z","shell.execute_reply":"2022-09-16T20:17:38.832032Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))","metadata":{"execution":{"iopub.status.busy":"2022-09-16T20:17:40.014558Z","iopub.execute_input":"2022-09-16T20:17:40.014940Z","iopub.status.idle":"2022-09-16T20:17:40.022992Z","shell.execute_reply.started":"2022-09-16T20:17:40.014909Z","shell.execute_reply":"2022-09-16T20:17:40.021761Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_submission = pd.read_csv(\"/kaggle/input/digit-recognizer/sample_submission.csv\")\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":"2022-09-16T20:17:43.490567Z","iopub.execute_input":"2022-09-16T20:17:43.490995Z","iopub.status.idle":"2022-09-16T20:17:49.947913Z","shell.execute_reply.started":"2022-09-16T20:17:43.490958Z","shell.execute_reply":"2022-09-16T20:17:49.946759Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head(5)","metadata":{"execution":{"iopub.status.busy":"2022-09-16T20:17:49.951558Z","iopub.execute_input":"2022-09-16T20:17:49.952035Z","iopub.status.idle":"2022-09-16T20:17:49.980429Z","shell.execute_reply.started":"2022-09-16T20:17:49.951989Z","shell.execute_reply":"2022-09-16T20:17:49.979147Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train = train[\"label\"]\nx_train = train.drop(\"label\", axis=1).to_numpy()","metadata":{"execution":{"iopub.status.busy":"2022-09-16T21:06:37.252251Z","iopub.execute_input":"2022-09-16T21:06:37.252751Z","iopub.status.idle":"2022-09-16T21:06:37.339935Z","shell.execute_reply.started":"2022-09-16T21:06:37.252706Z","shell.execute_reply":"2022-09-16T21:06:37.338806Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_train.shape, x_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-16T21:00:44.313141Z","iopub.execute_input":"2022-09-16T21:00:44.313546Z","iopub.status.idle":"2022-09-16T21:00:44.320462Z","shell.execute_reply.started":"2022-09-16T21:00:44.313511Z","shell.execute_reply":"2022-09-16T21:00:44.319440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_test = test.to_numpy()\ny_test = sample_submission.to_numpy()","metadata":{"execution":{"iopub.status.busy":"2022-09-16T21:00:46.399941Z","iopub.execute_input":"2022-09-16T21:00:46.400355Z","iopub.status.idle":"2022-09-16T21:00:46.405698Z","shell.execute_reply.started":"2022-09-16T21:00:46.400319Z","shell.execute_reply":"2022-09-16T21:00:46.404620Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, x_test, y_train, y_test = train_test_split(x_train, y_train, test_size=0.1, random_state=10)","metadata":{"execution":{"iopub.status.busy":"2022-09-16T21:06:39.249277Z","iopub.execute_input":"2022-09-16T21:06:39.250450Z","iopub.status.idle":"2022-09-16T21:06:39.706262Z","shell.execute_reply.started":"2022-09-16T21:06:39.250399Z","shell.execute_reply":"2022-09-16T21:06:39.705100Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train[:10000].shape, y_train.shape","metadata":{"execution":{"iopub.status.busy":"2022-09-16T20:24:50.947960Z","iopub.execute_input":"2022-09-16T20:24:50.948350Z","iopub.status.idle":"2022-09-16T20:24:50.955754Z","shell.execute_reply.started":"2022-09-16T20:24:50.948312Z","shell.execute_reply":"2022-09-16T20:24:50.954655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# fig, axe = plt.subplots(nrows=3, ncols=5)\n# for i in range(15):\n#     axe[i//5, i%5].imshow(X_train[i], cmap='gray')\n#     axe[i//5, i%5].axis('off')\n#     axe[i//5, i%5].set_title(y_train[i])\n\n# plt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2022-09-16T18:22:18.916008Z","iopub.execute_input":"2022-09-16T18:22:18.919091Z","iopub.status.idle":"2022-09-16T18:22:18.925567Z","shell.execute_reply.started":"2022-09-16T18:22:18.919040Z","shell.execute_reply":"2022-09-16T18:22:18.924472Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"svd = TruncatedSVD(n_components=4, random_state = 42)\nsvc = svm.SVC(gamma=\"scale\")\nknn = KNN(n_neighbors=5)\nscaler = StandardScaler()","metadata":{"execution":{"iopub.status.busy":"2022-09-16T21:07:00.974406Z","iopub.execute_input":"2022-09-16T21:07:00.974856Z","iopub.status.idle":"2022-09-16T21:07:00.980307Z","shell.execute_reply.started":"2022-09-16T21:07:00.974817Z","shell.execute_reply":"2022-09-16T21:07:00.979536Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# ssk_model = Pipeline([('scaler', scaler), ('svd',svd), ('knn', knn)])\n# ssk_model.fit(X_train, y_train)\n# ssk_model.score(x_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2022-09-16T20:17:06.515641Z","iopub.execute_input":"2022-09-16T20:17:06.516022Z","iopub.status.idle":"2022-09-16T20:17:09.216543Z","shell.execute_reply.started":"2022-09-16T20:17:06.515989Z","shell.execute_reply":"2022-09-16T20:17:09.213202Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ss_model = Pipeline([('scaler', scaler), ('svc',svc)])\nss_model.fit(X_train, y_train)\nss_model.score(x_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2022-09-16T21:07:02.898899Z","iopub.execute_input":"2022-09-16T21:07:02.899798Z","iopub.status.idle":"2022-09-16T21:11:14.869193Z","shell.execute_reply.started":"2022-09-16T21:07:02.899745Z","shell.execute_reply":"2022-09-16T21:11:14.868033Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# svc.fit(X_train, y_train)\n# svc.score(x_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2022-09-16T20:26:31.424235Z","iopub.execute_input":"2022-09-16T20:26:31.424631Z","iopub.status.idle":"2022-09-16T20:53:36.710468Z","shell.execute_reply.started":"2022-09-16T20:26:31.424600Z","shell.execute_reply":"2022-09-16T20:53:36.709392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# knn_modal = KNN(n_neighbors=7)\n# knn_modal.fit(X_train, y_train)\n# knn_modal.score(x_test, y_test)\n# 0.96314","metadata":{"execution":{"iopub.status.busy":"2022-09-16T19:58:16.656603Z","iopub.execute_input":"2022-09-16T19:58:16.657530Z","iopub.status.idle":"2022-09-16T19:58:29.796192Z","shell.execute_reply.started":"2022-09-16T19:58:16.657490Z","shell.execute_reply":"2022-09-16T19:58:29.795203Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# from sklearn.ensemble import *\n\n# GBC = GradientBoostingClassifier(learning_rate=0.1, criterion=\"mse\")\n# GBC.fit(X_train, y_train)\n# GBC.score(x_test, y_test)","metadata":{"scrolled":true,"execution":{"iopub.status.busy":"2022-09-16T18:41:33.946072Z","iopub.execute_input":"2022-09-16T18:41:33.947179Z","iopub.status.idle":"2022-09-16T19:08:28.226580Z","shell.execute_reply.started":"2022-09-16T18:41:33.947110Z","shell.execute_reply":"2022-09-16T19:08:28.225563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# #23:21\n# RFC = RandomForestClassifier()\n# RFC.fit(X_train, y_train)\n# RFC.score(x_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2022-09-16T19:21:51.713017Z","iopub.execute_input":"2022-09-16T19:21:51.713451Z","iopub.status.idle":"2022-09-16T19:22:12.569973Z","shell.execute_reply.started":"2022-09-16T19:21:51.713416Z","shell.execute_reply":"2022-09-16T19:22:12.569019Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# test_predict = knn_modal.predict(test)\ntest_predict = ss_model.predict(test)","metadata":{"execution":{"iopub.status.busy":"2022-09-16T21:11:24.898030Z","iopub.execute_input":"2022-09-16T21:11:24.898462Z","iopub.status.idle":"2022-09-16T21:16:04.672175Z","shell.execute_reply.started":"2022-09-16T21:11:24.898426Z","shell.execute_reply":"2022-09-16T21:16:04.670904Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# index = 3263\n# plt.imshow(np.resize(test.to_numpy()[index], (28, 28)), cmap='gray')\n# plt.title(test_predict[index])","metadata":{"execution":{"iopub.status.busy":"2022-09-16T19:43:19.504339Z","iopub.execute_input":"2022-09-16T19:43:19.504899Z","iopub.status.idle":"2022-09-16T19:43:19.723606Z","shell.execute_reply.started":"2022-09-16T19:43:19.504857Z","shell.execute_reply":"2022-09-16T19:43:19.722632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pd.DataFrame(test_predict).to_csv(\"/kaggle/working/submission.csv\")\n\nsubmission = pd.DataFrame({\n        \"ImageId\": pd.DataFrame([int(x) for x in range(1, 28000+1)])[0],\n        \"Label\": test_predict\n    })\nsubmission.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-09-16T21:16:04.674075Z","iopub.execute_input":"2022-09-16T21:16:04.674442Z","iopub.status.idle":"2022-09-16T21:16:04.757410Z","shell.execute_reply.started":"2022-09-16T21:16:04.674408Z","shell.execute_reply":"2022-09-16T21:16:04.756294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# c = pd.read_csv(\"submission.csv\")","metadata":{"execution":{"iopub.status.busy":"2022-09-16T21:23:55.528813Z","iopub.execute_input":"2022-09-16T21:23:55.529249Z","iopub.status.idle":"2022-09-16T21:23:55.543627Z","shell.execute_reply.started":"2022-09-16T21:23:55.529210Z","shell.execute_reply":"2022-09-16T21:23:55.542250Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{"execution":{"iopub.status.busy":"2022-09-16T21:24:02.051523Z","iopub.execute_input":"2022-09-16T21:24:02.052635Z","iopub.status.idle":"2022-09-16T21:24:02.066189Z","shell.execute_reply.started":"2022-09-16T21:24:02.052580Z","shell.execute_reply":"2022-09-16T21:24:02.065373Z"},"trusted":true},"execution_count":null,"outputs":[]}]}