{"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":30698,"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\nimport seaborn as sns\n\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.model_selection import train_test_split, cross_val_predict, cross_val_score\n\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.ensemble import GradientBoostingClassifier, AdaBoostClassifier, ExtraTreesClassifier, HistGradientBoostingClassifier, RandomForestClassifier, StackingClassifier\n\nfrom sklearn.metrics import classification_report, confusion_matrix\n\nfrom sklearn.model_selection import GridSearchCV, RandomizedSearchCV","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-05-01T14:44:51.149319Z","iopub.execute_input":"2024-05-01T14:44:51.149771Z","iopub.status.idle":"2024-05-01T14:44:51.160019Z","shell.execute_reply.started":"2024-05-01T14:44:51.149736Z","shell.execute_reply":"2024-05-01T14:44:51.158571Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train = pd.read_csv('/kaggle/input/digit-recognizer/train.csv')\ndf_train.shape","metadata":{"execution":{"iopub.status.busy":"2024-05-01T14:07:11.550128Z","iopub.execute_input":"2024-05-01T14:07:11.551018Z","iopub.status.idle":"2024-05-01T14:07:15.588512Z","shell.execute_reply.started":"2024-05-01T14:07:11.550971Z","shell.execute_reply":"2024-05-01T14:07:15.587127Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train.head()","metadata":{"execution":{"iopub.status.busy":"2024-05-01T13:16:20.943787Z","iopub.execute_input":"2024-05-01T13:16:20.944308Z","iopub.status.idle":"2024-05-01T13:16:20.983359Z","shell.execute_reply.started":"2024-05-01T13:16:20.944246Z","shell.execute_reply":"2024-05-01T13:16:20.982162Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"first_digit = df_train.iloc[0, 1:].values.reshape(28, 28)\nplt.figure(figsize=(2, 2))\nplt.axis('off')\nplt.title('First digit, label = 1')\nplt.imshow(first_digit, cmap='binary');","metadata":{"execution":{"iopub.status.busy":"2024-05-01T13:22:44.024530Z","iopub.execute_input":"2024-05-01T13:22:44.024963Z","iopub.status.idle":"2024-05-01T13:22:44.259575Z","shell.execute_reply.started":"2024-05-01T13:22:44.024929Z","shell.execute_reply":"2024-05-01T13:22:44.257777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_train['label'].value_counts(normalize=True)","metadata":{"execution":{"iopub.status.busy":"2024-05-01T13:27:41.795320Z","iopub.execute_input":"2024-05-01T13:27:41.795717Z","iopub.status.idle":"2024-05-01T13:27:41.806553Z","shell.execute_reply.started":"2024-05-01T13:27:41.795689Z","shell.execute_reply":"2024-05-01T13:27:41.805365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = df_train.drop(columns=['label'])\ny = df_train['label']\n\nX = StandardScaler().fit_transform(X)\nX_train, X_test, y_train, y_test = train_test_split(X, y, stratify=y)","metadata":{"execution":{"iopub.status.busy":"2024-05-01T16:01:41.774913Z","iopub.execute_input":"2024-05-01T16:01:41.776661Z","iopub.status.idle":"2024-05-01T16:01:43.757051Z","shell.execute_reply.started":"2024-05-01T16:01:41.776596Z","shell.execute_reply":"2024-05-01T16:01:43.755640Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# KNeighborsClassifier","metadata":{"execution":{"iopub.status.busy":"2024-05-01T14:39:30.537656Z","iopub.execute_input":"2024-05-01T14:39:30.538092Z","iopub.status.idle":"2024-05-01T14:39:30.546140Z","shell.execute_reply.started":"2024-05-01T14:39:30.538060Z","shell.execute_reply":"2024-05-01T14:39:30.544110Z"}}},{"cell_type":"code","source":"knn = KNeighborsClassifier()\nknn.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2024-05-01T16:01:45.504698Z","iopub.execute_input":"2024-05-01T16:01:45.505107Z","iopub.status.idle":"2024-05-01T16:01:45.540530Z","shell.execute_reply.started":"2024-05-01T16:01:45.505076Z","shell.execute_reply":"2024-05-01T16:01:45.538994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn.score(X_train, y_train), knn.score(X_test, y_test)","metadata":{"execution":{"iopub.status.busy":"2024-05-01T16:01:46.348741Z","iopub.execute_input":"2024-05-01T16:01:46.351307Z","iopub.status.idle":"2024-05-01T16:02:27.503909Z","shell.execute_reply.started":"2024-05-01T16:01:46.351227Z","shell.execute_reply":"2024-05-01T16:02:27.502653Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn_params = {\n    'weights': ['uniform', 'distance'],\n    'n_neighbors': [3, 5, 7, 9, 10]\n}\n\nknn_grid = GridSearchCV(knn, knn_params, cv=10, n_jobs=-1, verbose=1)\nknn_grid.fit(X_train, y_train)\nknn_grid.best_params_","metadata":{"execution":{"iopub.status.busy":"2024-05-01T14:46:05.237630Z","iopub.execute_input":"2024-05-01T14:46:05.238441Z","iopub.status.idle":"2024-05-01T14:50:38.197865Z","shell.execute_reply.started":"2024-05-01T14:46:05.238403Z","shell.execute_reply":"2024-05-01T14:50:38.196167Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def cross_val_plot(model, title):\n    cv_h = cross_val_score(model, X_train, y_train, scoring='accuracy', cv=10)\n    plt.plot([n for n in range(1, 11)], cv_h, c='yellow', label=title)\n    plt.legend();\n    \ncross_val_plot(knn_grid.best_estimator_, 'KNN')","metadata":{"execution":{"iopub.status.busy":"2024-05-01T15:12:39.778653Z","iopub.execute_input":"2024-05-01T15:12:39.779099Z","iopub.status.idle":"2024-05-01T15:13:07.384162Z","shell.execute_reply.started":"2024-05-01T15:12:39.779066Z","shell.execute_reply":"2024-05-01T15:13:07.383104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn_predict = knn_grid.predict(X_test)\nprint(classification_report(y_test, knn_predict))","metadata":{"execution":{"iopub.status.busy":"2024-05-01T14:50:38.201120Z","iopub.execute_input":"2024-05-01T14:50:38.203691Z","iopub.status.idle":"2024-05-01T14:50:47.914174Z","shell.execute_reply.started":"2024-05-01T14:50:38.203629Z","shell.execute_reply":"2024-05-01T14:50:47.912608Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def heat_m(y_true, y_pred, title):\n    conf_m = confusion_matrix(y_true, y_pred)\n    row_s = conf_m.sum(axis=1, keepdims=1)\n    error_conf = conf_m / row_s\n    np.fill_diagonal(error_conf, 0)\n    plt.title(title)\n    sns.heatmap(error_conf, cmap=plt.cm.gray);\n    \nheat_m(y_test, knn_predict, 'KNN error')","metadata":{"execution":{"iopub.status.busy":"2024-05-01T15:59:00.999203Z","iopub.execute_input":"2024-05-01T15:59:01.000764Z","iopub.status.idle":"2024-05-01T15:59:01.538513Z","shell.execute_reply.started":"2024-05-01T15:59:01.000709Z","shell.execute_reply":"2024-05-01T15:59:01.537308Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}