{ "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", " C = 1\n", " # Plot the decision boundary. For that, we will assign a color to each\n", " for other_params in plot_dict:\n", " models = svm.SVC(kernel=kernel,\n", " C=C,\n", " degree=other_params,\n", " gamma=1 if kernel == 'poly' else other_params)\n", " models.fit(X, y)\n", " accuracy = models.score(X, y)\n", " # prediction = models.predict(y)\n", " plt.subplot(plot_dict[other_params])\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", " if kernel == 'poly':\n", " plt.title('Plot for degree=%.2f' % other_params + '\\n' +\n", " 'Accuracy:%.2f' % accuracy)\n", " if kernel == 'rbf':\n", " plt.title('Plot for gamma=%.2f' % other_params + '\\n' +\n", " 'Accuracy:%.2f' % accuracy)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Degree in Polynominal Kernel\n", "\n", "* 在scikit-learn中kernel定義文件: [kernel func](https://scikit-learn.org/dev/modules/svm.html#kernel-functions)\n", "* 補充討論: [poly中使用gamma](https://stackoverflow.com/questions/12227881/scikit-learn-poly-kernel-and-gamma-value)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot_dict = {1: 141, 2: 142, 3: 143, 4: 144}\n", "svm_example(plot_dict, kernel='poly')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "plot_dict = {0.1: 141, 1: 142, 10: 143, 100: 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 }