{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from sklearn import linear_model, datasets\n", "%matplotlib inline" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# 調整C值觀察結果" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from myfun import plot_decision_regions\n", "\n", "\n", "def regression_example(plot_dict, c):\n", " # import some data to play with\n", " iris = datasets.load_iris()\n", " X = iris.data[:, :2] # we only take the first two features.\n", " y = iris.target\n", "\n", " # create model\n", " logreg = linear_model.LogisticRegression(C=c)\n", "\n", " # training\n", " logreg.fit(X, y)\n", "\n", " # Plot the decision boundary. For that, we will assign a color to each\n", " if c in plot_dict:\n", " plt.subplot(plot_dict[c])\n", " plt.tight_layout()\n", " # plt.figure()\n", " plot_decision_regions(X, y, logreg)\n", "\n", " # Plot also the training points\n", " plt.scatter(X[:, 0], X[:, 1], c=y, edgecolors='k', cmap=plt.cm.Paired)\n", " plt.xlabel('Sepal length')\n", " plt.ylabel('Sepal width')\n", " plt.title('Plot for C: %d' % c)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plt.figure(figsize=(8.5, 6))\n", "\n", "# 調整 C 值\n", "plot_dict = {1: 231, 10: 232, 1e2: 233, 1e3: 234, 1e4: 235, 1e5: 236}\n", "for i in plot_dict:\n", " regression_example(plot_dict, i)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.2" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": true, "sideBar": true, "skip_h1_title": false, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }