{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.10.12","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":30839,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"id":"2b8007e7","cell_type":"code","source":"import pandas as pd\nimport numpy as np","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-25T03:00:28.246172Z","iopub.execute_input":"2025-01-25T03:00:28.246583Z","iopub.status.idle":"2025-01-25T03:00:29.505669Z","shell.execute_reply.started":"2025-01-25T03:00:28.246538Z","shell.execute_reply":"2025-01-25T03:00:29.504467Z"}},"outputs":[],"execution_count":null},{"id":"7d4bb20c","cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-25T03:00:29.506413Z","iopub.execute_input":"2025-01-25T03:00:29.506877Z","iopub.status.idle":"2025-01-25T03:00:33.324333Z","shell.execute_reply.started":"2025-01-25T03:00:29.506850Z","shell.execute_reply":"2025-01-25T03:00:33.323213Z"}},"outputs":[],"execution_count":null},{"id":"ac7cb1c5","cell_type":"code","source":"train_df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-25T03:00:33.325562Z","iopub.execute_input":"2025-01-25T03:00:33.326033Z","iopub.status.idle":"2025-01-25T03:00:33.334085Z","shell.execute_reply.started":"2025-01-25T03:00:33.325992Z","shell.execute_reply":"2025-01-25T03:00:33.333018Z"}},"outputs":[],"execution_count":null},{"id":"6d089c86","cell_type":"code","source":"train_df.head()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-25T03:00:33.337469Z","iopub.execute_input":"2025-01-25T03:00:33.337785Z","iopub.status.idle":"2025-01-25T03:00:33.379492Z","shell.execute_reply.started":"2025-01-25T03:00:33.337739Z","shell.execute_reply":"2025-01-25T03:00:33.378210Z"}},"outputs":[],"execution_count":null},{"id":"60a935a7","cell_type":"code","source":"train_df.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-25T03:00:33.381387Z","iopub.execute_input":"2025-01-25T03:00:33.381807Z","iopub.status.idle":"2025-01-25T03:00:33.432402Z","shell.execute_reply.started":"2025-01-25T03:00:33.381741Z","shell.execute_reply":"2025-01-25T03:00:33.431102Z"}},"outputs":[],"execution_count":null},{"id":"08d456b8","cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-25T03:00:33.433800Z","iopub.execute_input":"2025-01-25T03:00:33.434234Z","iopub.status.idle":"2025-01-25T03:00:34.892661Z","shell.execute_reply.started":"2025-01-25T03:00:33.434192Z","shell.execute_reply":"2025-01-25T03:00:34.891400Z"}},"outputs":[],"execution_count":null},{"id":"01233cc2","cell_type":"code","source":"X = train_df.drop(columns=['label'])\ny = train_df['label']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-25T03:00:34.894131Z","iopub.execute_input":"2025-01-25T03:00:34.894903Z","iopub.status.idle":"2025-01-25T03:00:35.046759Z","shell.execute_reply.started":"2025-01-25T03:00:34.894850Z","shell.execute_reply":"2025-01-25T03:00:35.045283Z"}},"outputs":[],"execution_count":null},{"id":"d4ee65d8","cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-25T03:00:35.048248Z","iopub.execute_input":"2025-01-25T03:00:35.048702Z","iopub.status.idle":"2025-01-25T03:00:35.299264Z","shell.execute_reply.started":"2025-01-25T03:00:35.048659Z","shell.execute_reply":"2025-01-25T03:00:35.297899Z"}},"outputs":[],"execution_count":null},{"id":"93fb120f","cell_type":"code","source":"from sklearn.linear_model import LogisticRegression\nfrom sklearn.tree import DecisionTreeClassifier, ExtraTreeClassifier\nfrom sklearn.svm import LinearSVC, SVC\nfrom sklearn.neighbors import KNeighborsClassifier, RadiusNeighborsClassifier\nfrom sklearn.ensemble import AdaBoostClassifier, BaggingClassifier, GradientBoostingClassifier, RandomForestClassifier, StackingClassifier\nfrom sklearn.neural_network import MLPClassifier\nfrom sklearn.metrics import accuracy_score","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-25T03:00:35.300342Z","iopub.execute_input":"2025-01-25T03:00:35.300605Z","iopub.status.idle":"2025-01-25T03:00:35.709748Z","shell.execute_reply.started":"2025-01-25T03:00:35.300580Z","shell.execute_reply":"2025-01-25T03:00:35.708814Z"}},"outputs":[],"execution_count":null},{"id":"830d7c7b","cell_type":"code","source":"# models = {\n#     'LogisticRegression': LogisticRegression(),\n#     'DecisionTree': DecisionTreeClassifier(),\n#     'ExtraTree': ExtraTreeClassifier(),\n#     'SVC': SVC(),\n#     'KNeighbors' :KNeighborsClassifier(),\n#     'AdaBoost' :AdaBoostClassifier(),\n#     'Bagging': BaggingClassifier(),\n#     'GradientBoosting': GradientBoostingClassifier(),\n#     'RandomForest': RandomForestClassifier(),\n#     'MLPC': MLPClassifier()\n# }","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-25T03:00:35.710831Z","iopub.execute_input":"2025-01-25T03:00:35.711122Z","iopub.status.idle":"2025-01-25T03:00:35.715847Z","shell.execute_reply.started":"2025-01-25T03:00:35.711095Z","shell.execute_reply":"2025-01-25T03:00:35.714522Z"}},"outputs":[],"execution_count":null},{"id":"d48efb36","cell_type":"code","source":"# def model_building(X_train, y_train, X_test, y_test):\n#     for name, model in models.items():\n#         model.fit(X_train, y_train)\n#         y_pred = model.predict(X_test)\n#         print(f'{name} prediction: {accuracy_score(y_test, y_pred)}')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-25T03:00:35.717423Z","iopub.execute_input":"2025-01-25T03:00:35.717828Z","iopub.status.idle":"2025-01-25T03:00:35.735543Z","shell.execute_reply.started":"2025-01-25T03:00:35.717788Z","shell.execute_reply":"2025-01-25T03:00:35.734365Z"}},"outputs":[],"execution_count":null},{"id":"6ed04349","cell_type":"code","source":"# model_building(X_train, y_train, X_test, y_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-25T03:00:35.736987Z","iopub.execute_input":"2025-01-25T03:00:35.737269Z","iopub.status.idle":"2025-01-25T03:00:35.755843Z","shell.execute_reply.started":"2025-01-25T03:00:35.737244Z","shell.execute_reply":"2025-01-25T03:00:35.754737Z"}},"outputs":[],"execution_count":null},{"id":"43dd7ff8","cell_type":"code","source":"rf_model = RandomForestClassifier()\nrf_model.fit(X_train, y_train)\ny_pred_rf = rf_model.predict(X_test)\nprint(accuracy_score(y_test, y_pred_rf))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-25T03:00:35.759093Z","iopub.execute_input":"2025-01-25T03:00:35.759488Z","iopub.status.idle":"2025-01-25T03:01:00.729622Z","shell.execute_reply.started":"2025-01-25T03:00:35.759457Z","shell.execute_reply":"2025-01-25T03:01:00.728457Z"}},"outputs":[],"execution_count":null},{"id":"8be1dd3e","cell_type":"code","source":"svc = SVC()\nsvc.fit(X_train, y_train)\ny_pred_svc = svc.predict(X_test)\nprint(accuracy_score(y_test, y_pred_svc))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-25T03:01:00.731183Z","iopub.execute_input":"2025-01-25T03:01:00.731477Z","iopub.status.idle":"2025-01-25T03:04:06.550125Z","shell.execute_reply.started":"2025-01-25T03:01:00.731448Z","shell.execute_reply":"2025-01-25T03:04:06.548841Z"}},"outputs":[],"execution_count":null},{"id":"c1ea4362-938f-4e75-8161-1f73821bcc9f","cell_type":"code","source":"kneighbors = KNeighborsClassifier()\nkneighbors.fit(X_train, y_train)\ny_pred_kn = kneighbors.predict(X_test)\nprint(accuracy_score(y_test, y_pred_kn))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-25T03:04:06.551328Z","iopub.execute_input":"2025-01-25T03:04:06.551684Z","iopub.status.idle":"2025-01-25T03:04:20.489382Z","shell.execute_reply.started":"2025-01-25T03:04:06.551651Z","shell.execute_reply":"2025-01-25T03:04:20.488127Z"}},"outputs":[],"execution_count":null},{"id":"95149d51-cde7-42b5-ae96-3d5f993eb2bd","cell_type":"markdown","source":"GB will take some time to train","metadata":{}},{"id":"34a38a33-8a79-4724-9eb9-b0016d988689","cell_type":"code","source":"# GB = GradientBoostingClassifier()\n# GB.fit(X_train, y_train)\n# y_pred_GB = GB.predict(X_test)\n# print(accuracy_score(y_test, y_pred_GB))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-25T03:04:20.490586Z","iopub.execute_input":"2025-01-25T03:04:20.490925Z","iopub.status.idle":"2025-01-25T03:04:20.495889Z","shell.execute_reply.started":"2025-01-25T03:04:20.490896Z","shell.execute_reply":"2025-01-25T03:04:20.494447Z"}},"outputs":[],"execution_count":null},{"id":"c79e4fd5-a238-498d-b663-169cbbfac227","cell_type":"code","source":"test = pd.read_csv('/kaggle/input/digit-recognizer/test.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-25T03:04:20.497318Z","iopub.execute_input":"2025-01-25T03:04:20.497729Z","iopub.status.idle":"2025-01-25T03:04:22.643018Z","shell.execute_reply.started":"2025-01-25T03:04:20.497684Z","shell.execute_reply":"2025-01-25T03:04:22.641839Z"}},"outputs":[],"execution_count":null},{"id":"7cad0de6-edd2-4938-a37f-e3c9b4be3c83","cell_type":"code","source":"test_pred_svc = svc.predict(test)\nresult_svc = pd.DataFrame({'ImageId': range(1, len(test)+1), 'Label': test_pred_svc})\nresult_svc","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-25T03:04:22.648034Z","iopub.execute_input":"2025-01-25T03:04:22.648384Z","iopub.status.idle":"2025-01-25T03:08:02.208297Z","shell.execute_reply.started":"2025-01-25T03:04:22.648356Z","shell.execute_reply":"2025-01-25T03:08:02.206951Z"}},"outputs":[],"execution_count":null},{"id":"84be2244-526c-478d-b7f0-486091605c5d","cell_type":"code","source":"test_pred_kn = kneighbors.predict(test)\nresult_kn = pd.DataFrame({'ImageId': range(1, len(test)+1), 'Label': test_pred_kn})\nresult_kn","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-25T03:12:45.407020Z","iopub.execute_input":"2025-01-25T03:12:45.408617Z","iopub.status.idle":"2025-01-25T03:13:32.186193Z","shell.execute_reply.started":"2025-01-25T03:12:45.408420Z","shell.execute_reply":"2025-01-25T03:13:32.184743Z"}},"outputs":[],"execution_count":null},{"id":"62541124-cfe6-4e2e-8930-0b2444ecc4ef","cell_type":"code","source":"result_svc.to_csv('Support_Vector_Classifier.csv', index=False)\nresult_kn.to_csv('K_Neighbors_Classifier.csv', index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-01-25T03:14:38.038167Z","iopub.execute_input":"2025-01-25T03:14:38.038758Z","iopub.status.idle":"2025-01-25T03:14:38.108305Z","shell.execute_reply.started":"2025-01-25T03:14:38.038700Z","shell.execute_reply":"2025-01-25T03:14:38.107154Z"}},"outputs":[],"execution_count":null},{"id":"d1215291-0048-4fbc-8a82-dde2e4c66b71","cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}