{"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":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"# Models\n\nimport numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nfrom sklearn.neighbors import KNeighborsClassifier\nfrom sklearn.ensemble import AdaBoostClassifier\nfrom sklearn.tree import DecisionTreeClassifier\nfrom sklearn.metrics import accuracy_score, precision_recall_fscore_support\nfrom sklearn.metrics import confusion_matrix, ConfusionMatrixDisplay\n\n# Read data\ntrain_data = pd.read_csv(\"../input/digit-recognizer/train.csv\")\ntest_data = pd.read_csv(\"../input/digit-recognizer/test.csv\")\n\n# Prepare data\ntrain_X, train_Y = train_data.iloc[:, 1:].values.astype('float32'), train_data.iloc[:, 0].values.astype('int32')\ntest_X = test_data.values.astype('float32')\n\n# Train AdaBoost model with Decision Tree as base estimator\nadaboost_model = AdaBoostClassifier(base_estimator=DecisionTreeClassifier(max_depth=1), n_estimators=50, random_state=1)\nadaboost_model.fit(train_X, train_Y)\n\n# Predictions for AdaBoost\nadaboost_predictions = adaboost_model.predict(test_X)\nadaboost_predict_probs = adaboost_model.predict_proba(test_X)\n\n# Filter predict_probs for correct element only\npredict_prob_correct = adaboost_predict_probs[np.arange(len(adaboost_predictions)), adaboost_predictions]\n\n# Plotting\nfig, axes = plt.subplots(3, 9, figsize=(15, 7))\n# Plotting function\ndef plot_images(indices, probs, title_suffix, ax_row):\n    for i, (index, prob) in enumerate(zip(indices, probs)):\n        ax = axes[ax_row, i]\n        ax.axis(\"off\")\n        ax.imshow(test_X[index].reshape(28, 28), cmap=plt.get_cmap('gray'))\n        ax.set_title(f\"Label: {int(adaboost_predictions[index])}\\n({title_suffix} {prob:.2f})\", fontsize=8)\n\n# Get indices for 9 greatest predict_probs\nmost_certain_indices = np.argsort(predict_prob_correct)[-9:]\nplot_images(most_certain_indices, predict_prob_correct, \"Confidence:\", 0)\n\n# Get indices for 9 smallest predict_probs\nmost_uncertain_indices = np.argsort(predict_prob_correct)[:9]\nplot_images(most_uncertain_indices, predict_prob_correct, \"Uncertainty:\", 1)\n\n# Get avg of differences between probs\npredict_prob_diffs = np.abs(predict_prob_correct - adaboost_predict_probs.mean(axis=1))\n\n# Get indices for 9 most near the decision boundary\nmost_uncertain_indices = np.argsort(predict_prob_diffs)[:9]\nplot_images(most_uncertain_indices, predict_prob_diffs, \"Difference:\", 2)\n\n# Save model\nsubmissions = pd.DataFrame({\"ImageId\": np.arange(1, len(adaboost_predictions) + 1), \"Label\": adaboost_predictions})\nsubmissions.to_csv(\"DR.csv\", index=False, header=True)\n\n# Save plot\nplt.tight_layout()\nplt.savefig('adaboost_plot.png')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-19T00:25:37.491430Z","iopub.execute_input":"2023-12-19T00:25:37.492552Z","iopub.status.idle":"2023-12-19T00:26:24.020823Z","shell.execute_reply.started":"2023-12-19T00:25:37.492500Z","shell.execute_reply":"2023-12-19T00:26:24.019531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Calculate and print confusion matrix\nconf_matrix = confusion_matrix(train_Y, adaboost_model.predict(train_X))\n\n# Plot the confusion matrix with a better color scheme\nfig, axarr = plt.subplots(1, 1, figsize=(8, 8))\ndisp = ConfusionMatrixDisplay(confusion_matrix=conf_matrix, display_labels=np.unique(train_Y))\ncax = disp.plot(include_values=True, cmap='Blues', ax=axarr, xticks_rotation='horizontal', values_format='d')\ncax.ax_.set_title('Confusion Matrix')\n\n# Predictions for AdaBoost on test data\nadaboost_predictions_test = adaboost_model.predict(test_X)\n\n# Save model\nsubmissions = pd.DataFrame({\"ImageId\": np.arange(1, len(adaboost_predictions_test) + 1), \"Label\": adaboost_predictions_test})\nsubmissions.to_csv(\"DR.csv\", index=False, header=True)\n\n# Save plot\nplt.tight_layout()\nplt.savefig('adaboost_plot.png')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-19T00:26:24.023292Z","iopub.execute_input":"2023-12-19T00:26:24.023648Z","iopub.status.idle":"2023-12-19T00:26:27.337581Z","shell.execute_reply.started":"2023-12-19T00:26:24.023619Z","shell.execute_reply":"2023-12-19T00:26:27.336236Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Calculate accuracy\nacc = accuracy_score(train_Y, adaboost_model.predict(train_X))\n\n# Calculate precision, recall, and F1 score for each class\nprecision, recall, f1_score, _ = precision_recall_fscore_support(train_Y, adaboost_model.predict(train_X), average=None)\n\n# Plot precision, recall, accuracy, and F1 score\nclasses = np.unique(train_Y)\nfig, ax = plt.subplots(figsize=(10, 6))\n\n# Plot Precision\nax.plot(classes, precision, label='Precision', marker='o', linestyle='-')\n\n# Plot Recall\nax.plot(classes, recall, label='Recall', marker='s', linestyle='-')\n\n# Plot Accuracy\nax.axhline(y=acc, color='r', linestyle='--', label='Accuracy')\n\n# Plot F1 Score\nax.plot(classes, f1_score, label='F1 Score', marker='^', linestyle='-')\n\nax.set_xticks(classes)\nax.set_xlabel('Class')\nax.set_ylabel('Score')\nax.set_title('Precision, Recall, Accuracy, and F1 Score')\nax.legend()\nplt.grid(True)\nplt.tight_layout()\n\n# Save plot\nplt.savefig('adaboost_metrics_plot.png')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-19T00:26:27.339039Z","iopub.execute_input":"2023-12-19T00:26:27.339727Z","iopub.status.idle":"2023-12-19T00:26:30.573756Z","shell.execute_reply.started":"2023-12-19T00:26:27.339692Z","shell.execute_reply":"2023-12-19T00:26:30.572662Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Read data\ntrain_data = pd.read_csv(\"../input/digit-recognizer/train.csv\")\ntest_data = pd.read_csv(\"../input/digit-recognizer/test.csv\")\n\n# Prepare data\ntrain_X, train_Y = train_data.iloc[:, 1:].values.astype('float32'), train_data.iloc[:, 0].values.astype('int32')\ntest_X = test_data.values.astype('float32')\n\n\n# Train k-Nearest Neighbors model\nknn_model = KNeighborsClassifier(n_neighbors=5).fit(train_X, train_Y)\n\n\n# Predictions for k-Nearest Neighbors\nknn_predictions = knn_model.predict(test_X)\nknn_predict_probs = knn_model.predict_proba(test_X)\n\n# Filter predict_probs for correct element only\npredict_prob_correct = knn_predict_probs[np.arange(len(knn_predictions)), knn_predictions]\n\n# Plotting\nfig, axes = plt.subplots(3, 9, figsize=(15, 7))\n# Plotting function\ndef plot_images(indices, probs, title_suffix, ax_row):\n    for i, (index, prob) in enumerate(zip(indices, probs)):\n        ax = axes[ax_row, i]\n        ax.axis(\"off\")\n        ax.imshow(test_X[index].reshape(28, 28), cmap=plt.get_cmap('gray'))\n        ax.set_title(f\"Label: {int(knn_predictions[index])}\\n({title_suffix} {prob:.2f})\", fontsize=8)\n\n# Get indices for 9 greatest predict_probs\nmost_certain_indices = np.argsort(predict_prob_correct)[-9:]\nplot_images(most_certain_indices, predict_prob_correct, \"Confidence:\", 0)\n\n# Get indices for 9 smallest predict_probs\nmost_uncertain_indices = np.argsort(predict_prob_correct)[:9]\nplot_images(most_uncertain_indices, predict_prob_correct, \"Uncertainty:\", 1)\n\n# Get avg of differences between probs\npredict_prob_diffs = np.abs(predict_prob_correct - knn_predict_probs.mean(axis=1))\n\n# Get indices for 9 most near the decision boundary\nmost_uncertain_indices = np.argsort(predict_prob_diffs)[:9]\nplot_images(most_uncertain_indices, predict_prob_diffs, \"Difference:\", 2)\n\n# Save model\nsubmissions = pd.DataFrame({\"ImageId\": np.arange(1, len(knn_predictions) + 1), \"Label\": knn_predictions})\nsubmissions.to_csv(\"DR.csv\", index=False, header=True)\n\n# Save plot\nplt.tight_layout()\nplt.savefig('knn_plot.png')\nplt.show()","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-12-19T00:26:30.575579Z","iopub.execute_input":"2023-12-19T00:26:30.575920Z","iopub.status.idle":"2023-12-19T00:27:47.182819Z","shell.execute_reply.started":"2023-12-19T00:26:30.575890Z","shell.execute_reply":"2023-12-19T00:27:47.181740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Calculate and print confusion matrix\nconf_matrix = confusion_matrix(train_Y, knn_model.predict(train_X))\n\n# Plot the confusion matrix with a better color scheme\nfig, axarr = plt.subplots(1, 1, figsize=(8, 8))\ndisp = ConfusionMatrixDisplay(confusion_matrix=conf_matrix, display_labels=np.unique(train_Y))\ncax = disp.plot(include_values=True, cmap='Blues', ax=axarr, xticks_rotation='horizontal', values_format='d')\ncax.ax_.set_title('Confusion Matrix')\n\n# Predictions for k-Nearest Neighbors on test data\nknn_predictions_test = knn_model.predict(test_X)\n\n# Save model\nsubmissions = pd.DataFrame({\"ImageId\": np.arange(1, len(knn_predictions_test) + 1), \"Label\": knn_predictions_test})\nsubmissions.to_csv(\"DR.csv\", index=False, header=True)\n\n# Save plot\nplt.tight_layout()\nplt.savefig('knn_plot.png')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-19T00:27:47.185153Z","iopub.execute_input":"2023-12-19T00:27:47.186174Z","iopub.status.idle":"2023-12-19T00:29:15.474021Z","shell.execute_reply.started":"2023-12-19T00:27:47.186136Z","shell.execute_reply":"2023-12-19T00:29:15.472828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# Calculate and print accuracy\nacc = accuracy_score(train_Y, knn_model.predict(train_X))\n\n# Calculate precision, recall, and F1 score for each class\nprecision, recall, f1_score, _ = precision_recall_fscore_support(train_Y, knn_model.predict(train_X), average=None)\n\n# Plot precision, recall, accuracy, and F1 score\nclasses = np.unique(train_Y)\nfig, ax = plt.subplots(figsize=(10, 6))\n\n# Plot Precision\nax.plot(classes, precision, label='Precision', marker='o', linestyle='-')\n\n# Plot Recall\nax.plot(classes, recall, label='Recall', marker='s', linestyle='-')\n\n# Plot Accuracy\nax.axhline(y=acc, color='r', linestyle='--', label='Accuracy')\n\n# Plot F1 Score\nax.plot(classes, f1_score, label='F1 Score', marker='^', linestyle='-')\n\nax.set_xticks(classes)\nax.set_xlabel('Class')\nax.set_ylabel('Score')\nax.set_title('Precision, Recall, Accuracy, and F1 Score')\nax.legend()\nplt.grid(True)\nplt.tight_layout()\n\n# Save plot\nplt.savefig('metrics_plot.png')\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-12-19T00:29:15.475424Z","iopub.execute_input":"2023-12-19T00:29:15.475740Z","iopub.status.idle":"2023-12-19T00:31:01.030281Z","shell.execute_reply.started":"2023-12-19T00:29:15.475713Z","shell.execute_reply":"2023-12-19T00:31:01.029158Z"},"trusted":true},"execution_count":null,"outputs":[]}]}