{"metadata":{"colab":{"provenance":[]},"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"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"### **Imports**","metadata":{"id":"jxHj_l7JTt-D"}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\n\ntrain_df = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest_df = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\n\ntrain_df.head()","metadata":{"id":"Ck-SLQW8KWXu","execution":{"iopub.status.busy":"2023-09-06T11:53:20.064873Z","iopub.execute_input":"2023-09-06T11:53:20.065278Z","iopub.status.idle":"2023-09-06T11:53:25.335862Z","shell.execute_reply.started":"2023-09-06T11:53:20.065248Z","shell.execute_reply":"2023-09-06T11:53:25.334714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Prepare the data**\n\nFor this challenge, it will not be necessary to make a previous analysis of the data, nor any special transformation.","metadata":{"id":"DknqjcpoTy7p"}},{"cell_type":"code","source":"print(f'Train: {train_df.shape} / Test: {test_df.shape}')","metadata":{"id":"tPb_eGWSOj_m","outputId":"bfc0810a-c92a-49be-940c-6c19e165a6d9","execution":{"iopub.status.busy":"2023-09-06T11:53:25.337723Z","iopub.execute_input":"2023-09-06T11:53:25.338038Z","iopub.status.idle":"2023-09-06T11:53:25.344472Z","shell.execute_reply.started":"2023-09-06T11:53:25.338011Z","shell.execute_reply":"2023-09-06T11:53:25.343296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train = train_df.drop('label', axis=1)\ny_train = train_df['label']\n\nX_test = test_df","metadata":{"id":"xLcp9e11KiMf","execution":{"iopub.status.busy":"2023-09-06T11:53:25.346171Z","iopub.execute_input":"2023-09-06T11:53:25.346642Z","iopub.status.idle":"2023-09-06T11:53:25.456520Z","shell.execute_reply.started":"2023-09-06T11:53:25.346605Z","shell.execute_reply":"2023-09-06T11:53:25.455569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Show some image**","metadata":{"id":"dtV81x2iS-If"}},{"cell_type":"code","source":"import matplotlib as mpl\nimport matplotlib.pyplot as plt\n\nX_train_copy = X_train.values.reshape(-1, 28, 28, 1)\n\nplt.figure(figsize=(2,2))\nplt.imshow(X_train_copy[9], cmap=mpl.cm.binary)\nplt.axis(\"off\")\n\nplt.show()","metadata":{"id":"59-wDliiKffL","outputId":"9c862621-4e2e-4df9-da38-a419488cba66","execution":{"iopub.status.busy":"2023-09-06T11:53:25.458142Z","iopub.execute_input":"2023-09-06T11:53:25.458828Z","iopub.status.idle":"2023-09-06T11:53:25.545725Z","shell.execute_reply.started":"2023-09-06T11:53:25.458796Z","shell.execute_reply":"2023-09-06T11:53:25.543671Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Model selection and training**","metadata":{"id":"q4D2oSbIP-rW"}},{"cell_type":"code","source":"from sklearn.linear_model import SGDClassifier\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.ensemble import RandomForestClassifier\n\nsgd_clf = SGDClassifier(random_state=42)\nknn_clf = KNeighborsClassifier()\ntree_clf = DecisionTreeClassifier(random_state=42)\nforest_clf = RandomForestClassifier(random_state=42)","metadata":{"id":"ctPa7ggeQC0W","execution":{"iopub.status.busy":"2023-09-06T11:53:25.549256Z","iopub.execute_input":"2023-09-06T11:53:25.549971Z","iopub.status.idle":"2023-09-06T11:53:26.657956Z","shell.execute_reply.started":"2023-09-06T11:53:25.549924Z","shell.execute_reply":"2023-09-06T11:53:26.656712Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import cross_validate\n\nmodel_list = [sgd_clf, knn_clf, tree_clf, forest_clf]\nscoring_metrics = ['accuracy']\nmodel_names = [\"SGD\", \"KNN\", \"Decision Tree\", \"Random Forest\"]\n\nfor model, name in zip(model_list, model_names):\n    scores = cross_validate(model, X_train, y_train, cv=5, scoring=scoring_metrics)\n    print(f\"{name}: Accuracy: {scores['test_accuracy'].mean():.2f}\")","metadata":{"id":"Je2Qq0fLQNKp","outputId":"65b9da26-f860-448a-882d-0eabd14a9d5c","execution":{"iopub.status.busy":"2023-09-06T11:53:26.659548Z","iopub.execute_input":"2023-09-06T11:53:26.660094Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"The best model is: **KNeighborsClassifier**","metadata":{"id":"1QmBhMGZTBiF"}},{"cell_type":"markdown","source":"### **Cross-validation for hyperparameters**","metadata":{"id":"5WBf03z-TVXn"}},{"cell_type":"code","source":"from sklearn.model_selection import GridSearchCV\n\nparam_grid = {\n    'n_neighbors': [3, 4, 5, 10],\n    'weights': [\"uniform\", \"distance\"]\n}\n\ngrid_search = GridSearchCV(knn_clf, param_grid, cv=5, scoring='accuracy', return_train_score=True)\ngrid_search.fit(X_train, y_train)\n\nprint(\"Best parameters:\", grid_search.best_params_)","metadata":{"id":"dIldwNBZTJg3","outputId":"1f140e59-1dc2-4e6f-9f6f-89c28974b427","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"best_estimator = grid_search.best_estimator_\nprint(best_estimator)","metadata":{"id":"NZZz6c0pJgtS","outputId":"2fc365a5-589c-4b26-faea-cdabefde0287","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Prediction**","metadata":{"id":"iPndtKQuTY3D"}},{"cell_type":"code","source":"y_pred = best_estimator.predict(X_test)","metadata":{"id":"dU6O_cKuJsOY","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### **Submission**","metadata":{"id":"ojCeBH_VTn_i"}},{"cell_type":"code","source":"submission_df = pd.DataFrame({'ImageId': range(1, len(y_pred) + 1), 'Label': y_pred})\nsubmission_df.head()","metadata":{"id":"L7FPmuKbKAum","outputId":"2f0ccc1d-755b-4485-da2f-9d4ba01692d0","trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission_df.to_csv('/kaggle/working/submission', index=False)","metadata":{"id":"Is4-l0P9KI5C","trusted":true},"execution_count":null,"outputs":[]}],"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"}}