{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 下載 myfun.py\n", "!wget \"https://raw.githubusercontent.com/TA-aiacademy/course_3.0/ML/02_ML/part4/Chapter5/myfun.py\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from sklearn import svm, datasets\n", "from matplotlib.pylab import rcParams\n", "\n", "%matplotlib inline\n", "rcParams['figure.figsize'] = 10, 5" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from myfun import plot_decision_regions\n", "\n", "\n", "def svm_example(plot_dict, kernel):\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", " # data since we want to plot the support vectors\n", " gamma = 0.7\n", " degree = 3\n", " # Plot the decision boundary. For that, we will assign a color to each\n", " for C in plot_dict:\n", " models = svm.SVC(kernel=kernel, C=C, degree=degree, gamma=gamma)\n", " models.fit(X, y)\n", " accuracy = models.score(X, y)\n", " # prediction = models.predict(y)\n", " plt.subplot(plot_dict[C])\n", " plt.tight_layout()\n", " # plt.figure()\n", " plot_decision_regions(X, y, models)\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=%.2f' % C + '\\n' + 'Accuracy:%.2f' % accuracy)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# C in Linear SVM" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot_dict = {0.01: 141, 0.1: 142, 1: 143, 10: 144}\n", "svm_example(plot_dict, kernel='linear')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# C in polynominal SVM" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 試著繼續調大C看看邊界的變化\n", "plot_dict = {0.01: 141, 0.1: 142, 1: 143, 10: 144}\n", "svm_example(plot_dict, kernel='poly')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# C in RBF SVM" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# 試著繼續調大C看看邊界的變化\n", "plot_dict = {0.01: 141, 0.1: 142, 1: 143, 10: 144}\n", "svm_example(plot_dict, kernel='rbf')" ] }, { "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 }