{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"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":30626,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom sklearn.metrics import accuracy_score, classification_report, confusion_matrix\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.svm import SVC\nfrom sklearn.datasets import load_digits","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:01.757652Z","iopub.execute_input":"2023-12-31T11:46:01.758072Z","iopub.status.idle":"2023-12-31T11:46:02.606220Z","shell.execute_reply.started":"2023-12-31T11:46:01.758042Z","shell.execute_reply":"2023-12-31T11:46:02.605050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/digit-recognizer/train.csv\")\ntest_df = pd.read_csv(\"/kaggle/input/digit-recognizer/test.csv\")\n","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:02.608658Z","iopub.execute_input":"2023-12-31T11:46:02.609284Z","iopub.status.idle":"2023-12-31T11:46:09.201644Z","shell.execute_reply.started":"2023-12-31T11:46:02.609244Z","shell.execute_reply":"2023-12-31T11:46:09.200561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.head() \n","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:09.202939Z","iopub.execute_input":"2023-12-31T11:46:09.203255Z","iopub.status.idle":"2023-12-31T11:46:09.242866Z","shell.execute_reply.started":"2023-12-31T11:46:09.203228Z","shell.execute_reply":"2023-12-31T11:46:09.241769Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.head()  \n","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:09.245639Z","iopub.execute_input":"2023-12-31T11:46:09.246036Z","iopub.status.idle":"2023-12-31T11:46:09.265690Z","shell.execute_reply.started":"2023-12-31T11:46:09.246006Z","shell.execute_reply":"2023-12-31T11:46:09.264449Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.isnull().sum().head(10)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:09.267922Z","iopub.execute_input":"2023-12-31T11:46:09.268405Z","iopub.status.idle":"2023-12-31T11:46:09.314979Z","shell.execute_reply.started":"2023-12-31T11:46:09.268316Z","shell.execute_reply":"2023-12-31T11:46:09.313791Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.isnull().sum().head(10)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:09.316247Z","iopub.execute_input":"2023-12-31T11:46:09.317203Z","iopub.status.idle":"2023-12-31T11:46:09.344789Z","shell.execute_reply.started":"2023-12-31T11:46:09.317159Z","shell.execute_reply":"2023-12-31T11:46:09.343624Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.describe()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:09.346346Z","iopub.execute_input":"2023-12-31T11:46:09.346690Z","iopub.status.idle":"2023-12-31T11:46:11.732297Z","shell.execute_reply.started":"2023-12-31T11:46:09.346661Z","shell.execute_reply":"2023-12-31T11:46:11.731197Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Logistic Regression","metadata":{}},{"cell_type":"code","source":"from sklearn.datasets import load_digits\ndigits = load_digits()\nprint(\"Image Data Shape:\", digits.data.shape)\nprint(\"Label Data Shape:\", digits.target.shape)\n\n","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:11.733803Z","iopub.execute_input":"2023-12-31T11:46:11.734677Z","iopub.status.idle":"2023-12-31T11:46:11.760908Z","shell.execute_reply.started":"2023-12-31T11:46:11.734637Z","shell.execute_reply":"2023-12-31T11:46:11.759621Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n\nplt.figure(figsize=(20, 4))\nfor index, (image, label) in enumerate(zip(digits.data[0:5], digits.target[0:5])):\n    plt.subplot(1, 5, index + 1)\n    plt.imshow(np.reshape(image, (8, 8)), cmap=plt.cm.gray)\n    plt.title('Training: %i\\n' % label, fontsize=20)\n\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:11.762765Z","iopub.execute_input":"2023-12-31T11:46:11.763185Z","iopub.status.idle":"2023-12-31T11:46:12.829645Z","shell.execute_reply.started":"2023-12-31T11:46:11.763148Z","shell.execute_reply":"2023-12-31T11:46:12.828486Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"x_train, x_test, y_train, y_test = train_test_split(digits.data, digits.target, test_size=0.25, random_state=0)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:12.835497Z","iopub.execute_input":"2023-12-31T11:46:12.835852Z","iopub.status.idle":"2023-12-31T11:46:12.843289Z","shell.execute_reply.started":"2023-12-31T11:46:12.835823Z","shell.execute_reply":"2023-12-31T11:46:12.842077Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"logisticRegr = LogisticRegression()\n\nlogisticRegr.fit(x_train, y_train)\n\nprediction = logisticRegr.predict(x_test[0].reshape(1, -1))\n\nprint(\"Prediction for the first test observation:\", prediction)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:12.845369Z","iopub.execute_input":"2023-12-31T11:46:12.845965Z","iopub.status.idle":"2023-12-31T11:46:13.056646Z","shell.execute_reply.started":"2023-12-31T11:46:12.845924Z","shell.execute_reply":"2023-12-31T11:46:13.055578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":" logisticRegr = LogisticRegression(max_iter=1000)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:13.058551Z","iopub.execute_input":"2023-12-31T11:46:13.059339Z","iopub.status.idle":"2023-12-31T11:46:13.064783Z","shell.execute_reply.started":"2023-12-31T11:46:13.059300Z","shell.execute_reply":"2023-12-31T11:46:13.063462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.preprocessing import StandardScaler\n\nscaler = StandardScaler()\nx_train_scaled = scaler.fit_transform(x_train)\nx_test_scaled = scaler.transform(x_test)\n\nlogisticRegr = LogisticRegression()\nlogisticRegr.fit(x_train_scaled, y_train)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:13.066867Z","iopub.execute_input":"2023-12-31T11:46:13.068099Z","iopub.status.idle":"2023-12-31T11:46:13.265290Z","shell.execute_reply.started":"2023-12-31T11:46:13.068061Z","shell.execute_reply":"2023-12-31T11:46:13.264212Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = logisticRegr.predict(x_test[0:10])\n\nprint(\"Predictions for the first 10 test observations:\", predictions)","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:13.267184Z","iopub.execute_input":"2023-12-31T11:46:13.267960Z","iopub.status.idle":"2023-12-31T11:46:13.277359Z","shell.execute_reply.started":"2023-12-31T11:46:13.267922Z","shell.execute_reply":"2023-12-31T11:46:13.275732Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"predictions = logisticRegr.predict(x_test)\n\nprint(\"Predictions for the test dataset:\", predictions)","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:13.280065Z","iopub.execute_input":"2023-12-31T11:46:13.280632Z","iopub.status.idle":"2023-12-31T11:46:13.297975Z","shell.execute_reply.started":"2023-12-31T11:46:13.280593Z","shell.execute_reply":"2023-12-31T11:46:13.296911Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Measuring Model Performance","metadata":{}},{"cell_type":"code","source":"\nscore = logisticRegr.score(x_test, y_test)\n\nprint(\"Accuracy of the Logistic Regression model on the test data:\", score)","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:13.299694Z","iopub.execute_input":"2023-12-31T11:46:13.300363Z","iopub.status.idle":"2023-12-31T11:46:13.314543Z","shell.execute_reply.started":"2023-12-31T11:46:13.300324Z","shell.execute_reply":"2023-12-31T11:46:13.313151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Confusion Matrix**","metadata":{}},{"cell_type":"code","source":"from sklearn import metrics\n\ncm = metrics.confusion_matrix(y_test, predictions)\n\nplt.figure(figsize=(9, 9))\nsns.heatmap(cm, annot=True, fmt=\".3f\", linewidths=.5, square=True, cmap='Blues_r')\nplt.ylabel('Actual label')\nplt.xlabel('Predicted label')\nall_sample_title = 'Accuracy Score: {0}'.format(score)\nplt.title(all_sample_title, size=15)\nplt.savefig('toy_Digits_ConfusionSeabornCodementor.png')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:13.316739Z","iopub.execute_input":"2023-12-31T11:46:13.317605Z","iopub.status.idle":"2023-12-31T11:46:14.292718Z","shell.execute_reply.started":"2023-12-31T11:46:13.317553Z","shell.execute_reply":"2023-12-31T11:46:14.291817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm = metrics.confusion_matrix(y_test, predictions)\nplt.figure(figsize=(9,9))\nplt.imshow(cm, interpolation='nearest', cmap='Pastel1')\nplt.title('Confusion matrix', size = 15)\nplt.colorbar()\ntick_marks = np.arange(10)\nplt.xticks(tick_marks, [\"0\", \"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\"], rotation=45, size = 10)\nplt.yticks(tick_marks, [\"0\", \"1\", \"2\", \"3\", \"4\", \"5\", \"6\", \"7\", \"8\", \"9\"], size = 10)\nplt.tight_layout()\nplt.ylabel('Actual label', size = 15)\nplt.xlabel('Predicted label', size = 15)\nwidth, height = cm.shape\n\nfor x in range(width):\n    for y in range(height):\n        plt.annotate(str(cm[x][y]), xy=(y, x), \n                    horizontalalignment='center',\n                    verticalalignment='center')\nplt.savefig('toy_Digits_ConfusionMatplotlibCodementor.png')","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:14.294045Z","iopub.execute_input":"2023-12-31T11:46:14.295005Z","iopub.status.idle":"2023-12-31T11:46:15.561928Z","shell.execute_reply.started":"2023-12-31T11:46:14.294970Z","shell.execute_reply":"2023-12-31T11:46:15.560782Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**svm**","metadata":{}},{"cell_type":"code","source":"from sklearn.datasets import load_digits\n\ndigits = load_digits()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:15.563940Z","iopub.execute_input":"2023-12-31T11:46:15.564377Z","iopub.status.idle":"2023-12-31T11:46:15.587772Z","shell.execute_reply.started":"2023-12-31T11:46:15.564335Z","shell.execute_reply":"2023-12-31T11:46:15.586544Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(dir(digits))","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:15.589359Z","iopub.execute_input":"2023-12-31T11:46:15.589747Z","iopub.status.idle":"2023-12-31T11:46:15.595510Z","shell.execute_reply.started":"2023-12-31T11:46:15.589684Z","shell.execute_reply":"2023-12-31T11:46:15.594392Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.gray()\nfor i in range(4):\n    plt.matshow(digits.images[i])\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:15.596962Z","iopub.execute_input":"2023-12-31T11:46:15.597318Z","iopub.status.idle":"2023-12-31T11:46:17.252183Z","shell.execute_reply.started":"2023-12-31T11:46:15.597290Z","shell.execute_reply":"2023-12-31T11:46:17.250928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = digits.data\ny = digits.target","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:17.253456Z","iopub.execute_input":"2023-12-31T11:46:17.253789Z","iopub.status.idle":"2023-12-31T11:46:17.258841Z","shell.execute_reply.started":"2023-12-31T11:46:17.253761Z","shell.execute_reply":"2023-12-31T11:46:17.257768Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Shape of X:\", X.shape)\nprint(\"Shape of y:\", y.shape)","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:17.260143Z","iopub.execute_input":"2023-12-31T11:46:17.260471Z","iopub.status.idle":"2023-12-31T11:46:17.268526Z","shell.execute_reply.started":"2023-12-31T11:46:17.260442Z","shell.execute_reply":"2023-12-31T11:46:17.267350Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(digits.data, digits.target, test_size=0.2, random_state=0)","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:17.269997Z","iopub.execute_input":"2023-12-31T11:46:17.270867Z","iopub.status.idle":"2023-12-31T11:46:17.284072Z","shell.execute_reply.started":"2023-12-31T11:46:17.270836Z","shell.execute_reply":"2023-12-31T11:46:17.282926Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sc = SVC(C=100, gamma=0.001)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:17.285468Z","iopub.execute_input":"2023-12-31T11:46:17.285874Z","iopub.status.idle":"2023-12-31T11:46:17.294169Z","shell.execute_reply.started":"2023-12-31T11:46:17.285842Z","shell.execute_reply":"2023-12-31T11:46:17.292914Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Shape of X_train:\", X_train.shape)\nprint(\"Shape of y_train:\", y_train.shape)","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:17.296003Z","iopub.execute_input":"2023-12-31T11:46:17.296416Z","iopub.status.idle":"2023-12-31T11:46:17.304901Z","shell.execute_reply.started":"2023-12-31T11:46:17.296384Z","shell.execute_reply":"2023-12-31T11:46:17.303802Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sc.fit(X_train, y_train)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:17.306563Z","iopub.execute_input":"2023-12-31T11:46:17.306919Z","iopub.status.idle":"2023-12-31T11:46:17.389060Z","shell.execute_reply.started":"2023-12-31T11:46:17.306889Z","shell.execute_reply":"2023-12-31T11:46:17.387888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = sc.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:17.395490Z","iopub.execute_input":"2023-12-31T11:46:17.395856Z","iopub.status.idle":"2023-12-31T11:46:17.425444Z","shell.execute_reply.started":"2023-12-31T11:46:17.395825Z","shell.execute_reply":"2023-12-31T11:46:17.424503Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy = accuracy_score(y_test, y_pred)\nprint(\"Accuracy:\", accuracy)","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:17.426777Z","iopub.execute_input":"2023-12-31T11:46:17.427086Z","iopub.status.idle":"2023-12-31T11:46:17.433511Z","shell.execute_reply.started":"2023-12-31T11:46:17.427059Z","shell.execute_reply":"2023-12-31T11:46:17.432193Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"cm = confusion_matrix(y_test, y_pred)\nplt.figure(figsize=(10, 7))\nsns.heatmap(cm, annot=True, fmt=\"d\", cmap=\"Blues\", linewidths=.5)\nplt.xlabel('Predicted')\nplt.ylabel('Actual')\nplt.title('Confusion Matrix')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:17.435632Z","iopub.execute_input":"2023-12-31T11:46:17.436124Z","iopub.status.idle":"2023-12-31T11:46:18.199844Z","shell.execute_reply.started":"2023-12-31T11:46:17.436082Z","shell.execute_reply":"2023-12-31T11:46:18.198780Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Predictions:\", y_pred)","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:18.201445Z","iopub.execute_input":"2023-12-31T11:46:18.202226Z","iopub.status.idle":"2023-12-31T11:46:18.209472Z","shell.execute_reply.started":"2023-12-31T11:46:18.202190Z","shell.execute_reply":"2023-12-31T11:46:18.208440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(\"Accuracy Score:\", accuracy)","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:18.210766Z","iopub.execute_input":"2023-12-31T11:46:18.211098Z","iopub.status.idle":"2023-12-31T11:46:18.221801Z","shell.execute_reply.started":"2023-12-31T11:46:18.211069Z","shell.execute_reply":"2023-12-31T11:46:18.220848Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**RFORST**","metadata":{}},{"cell_type":"code","source":"X_train = train_df.drop('label', axis=1)\ny_train = train_df['label']\n\nX_test = test_df","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:18.223179Z","iopub.execute_input":"2023-12-31T11:46:18.223478Z","iopub.status.idle":"2023-12-31T11:46:18.319666Z","shell.execute_reply.started":"2023-12-31T11:46:18.223452Z","shell.execute_reply":"2023-12-31T11:46:18.318484Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:18.321167Z","iopub.execute_input":"2023-12-31T11:46:18.321526Z","iopub.status.idle":"2023-12-31T11:46:18.575555Z","shell.execute_reply.started":"2023-12-31T11:46:18.321497Z","shell.execute_reply":"2023-12-31T11:46:18.574463Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.ensemble import RandomForestClassifier\n\n\nmodel = RandomForestClassifier(n_estimators=100, random_state=42)\n","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:18.576906Z","iopub.execute_input":"2023-12-31T11:46:18.577252Z","iopub.status.idle":"2023-12-31T11:46:18.674012Z","shell.execute_reply.started":"2023-12-31T11:46:18.577223Z","shell.execute_reply":"2023-12-31T11:46:18.672789Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:18.675337Z","iopub.execute_input":"2023-12-31T11:46:18.675681Z","iopub.status.idle":"2023-12-31T11:46:44.869090Z","shell.execute_reply.started":"2023-12-31T11:46:18.675650Z","shell.execute_reply":"2023-12-31T11:46:44.867928Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred = model.predict(X_val)","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:44.870318Z","iopub.execute_input":"2023-12-31T11:46:44.870625Z","iopub.status.idle":"2023-12-31T11:46:45.201463Z","shell.execute_reply.started":"2023-12-31T11:46:44.870600Z","shell.execute_reply":"2023-12-31T11:46:45.200635Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy = accuracy_score(y_val, y_pred)\nprint(\"Validation Data Accuracy:\", accuracy)","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:45.202672Z","iopub.execute_input":"2023-12-31T11:46:45.203176Z","iopub.status.idle":"2023-12-31T11:46:45.210082Z","shell.execute_reply.started":"2023-12-31T11:46:45.203147Z","shell.execute_reply":"2023-12-31T11:46:45.208752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"classification_report = classification_report(y_val, y_pred)\nprint(\"Classification Report:\")\nprint(classification_report)\n\nconfusion_matrix = confusion_matrix(y_val, y_pred)\nprint(\"Confusion Matrix:\")\nprint(confusion_matrix)","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:45.211286Z","iopub.execute_input":"2023-12-31T11:46:45.211562Z","iopub.status.idle":"2023-12-31T11:46:45.250206Z","shell.execute_reply.started":"2023-12-31T11:46:45.211537Z","shell.execute_reply":"2023-12-31T11:46:45.249148Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Using K-nearest Neighbour**","metadata":{}},{"cell_type":"code","source":"digits = load_digits()","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:45.251527Z","iopub.execute_input":"2023-12-31T11:46:45.251876Z","iopub.status.idle":"2023-12-31T11:46:45.274089Z","shell.execute_reply.started":"2023-12-31T11:46:45.251847Z","shell.execute_reply":"2023-12-31T11:46:45.273050Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train, X_test, y_train, y_test = train_test_split(digits.data, digits.target, test_size=0.2, random_state=0)","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:45.275437Z","iopub.execute_input":"2023-12-31T11:46:45.275786Z","iopub.status.idle":"2023-12-31T11:46:45.283611Z","shell.execute_reply.started":"2023-12-31T11:46:45.275751Z","shell.execute_reply":"2023-12-31T11:46:45.282554Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn_classifier = KNeighborsClassifier(n_neighbors=3)","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:45.284936Z","iopub.execute_input":"2023-12-31T11:46:45.285932Z","iopub.status.idle":"2023-12-31T11:46:45.292986Z","shell.execute_reply.started":"2023-12-31T11:46:45.285900Z","shell.execute_reply":"2023-12-31T11:46:45.291772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"knn_classifier.fit(X_train, y_train)","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:45.294273Z","iopub.execute_input":"2023-12-31T11:46:45.294559Z","iopub.status.idle":"2023-12-31T11:46:45.306851Z","shell.execute_reply.started":"2023-12-31T11:46:45.294533Z","shell.execute_reply":"2023-12-31T11:46:45.306005Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_pred_knn = knn_classifier.predict(X_test)","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:45.307677Z","iopub.execute_input":"2023-12-31T11:46:45.308021Z","iopub.status.idle":"2023-12-31T11:46:45.395461Z","shell.execute_reply.started":"2023-12-31T11:46:45.307993Z","shell.execute_reply":"2023-12-31T11:46:45.394529Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"accuracy_knn = accuracy_score(y_test, y_pred_knn)\nprint(\"KNN Accuracy:\", accuracy_knn)","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:45.397128Z","iopub.execute_input":"2023-12-31T11:46:45.397740Z","iopub.status.idle":"2023-12-31T11:46:45.404468Z","shell.execute_reply.started":"2023-12-31T11:46:45.397685Z","shell.execute_reply":"2023-12-31T11:46:45.403378Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import confusion_matrix\n\ncm_knn = confusion_matrix(y_test, y_pred_knn)","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:45.405968Z","iopub.execute_input":"2023-12-31T11:46:45.407112Z","iopub.status.idle":"2023-12-31T11:46:45.414275Z","shell.execute_reply.started":"2023-12-31T11:46:45.407074Z","shell.execute_reply":"2023-12-31T11:46:45.413366Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 7))\nsns.heatmap(cm_knn, annot=True, fmt=\"d\", cmap=\"Blues\")\nplt.xlabel('Predicted')\nplt.ylabel('Actual')\nplt.title('KNN Confusion Matrix')\nplt.show()\n","metadata":{"execution":{"iopub.status.busy":"2023-12-31T11:46:45.415262Z","iopub.execute_input":"2023-12-31T11:46:45.416248Z","iopub.status.idle":"2023-12-31T11:46:46.049787Z","shell.execute_reply.started":"2023-12-31T11:46:45.416219Z","shell.execute_reply":"2023-12-31T11:46:46.048619Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"","metadata":{}}]}