{ "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/part3/Chapter4/myfun.py\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from sklearn.datasets import make_blobs\n", "from sklearn.linear_model import LogisticRegression\n", "from myfun import plot_decision_regions\n", "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# make 3-class dataset for classification\n", "centers = [[-5, 0], [0, 1.5], [5, -1]]\n", "X, y = make_blobs(n_samples=1000, centers=centers, random_state=40)\n", "transformation = [[0.4, 0.2], [-0.4, 1.2]]\n", "X = np.dot(X, transformation)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "color = \"rbg\"\n", "color = [color[y[i]] for i in range(len(y))]\n", "plt.scatter(X[:, 0], X[:, 1], c=color)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* LogisticRegression 的參數設定可以參考官方文件\n", " * https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html#sklearn.linear_model.LogisticRegression" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "for multi_class in ('multinomial', 'ovr'):\n", " clf = LogisticRegression(solver='sag',\n", " max_iter=100,\n", " random_state=42,\n", " multi_class=multi_class).fit(X, y)\n", "\n", " # print the training scores\n", " print(\"training score : %.3f (%s)\" % (clf.score(X, y), multi_class))\n", "\n", " # plot the decision region\n", " plt.figure()\n", " plot_decision_regions(X, y, clf)\n", " plt.title(multi_class)\n", " # Plot also the training points\n", " colors = \"rbg\"\n", " for i, color in zip(clf.classes_, colors):\n", " idx = np.where(y == i)\n", " plt.scatter(X[idx, 0], X[idx, 1], c=color, cmap=plt.cm.Paired)\n", "\n", " # Plot the three one-against-all classifiers\n", " xmin, xmax = plt.xlim()\n", " ymin, ymax = plt.ylim()\n", " coef = clf.coef_\n", " intercept = clf.intercept_\n", "\n", " def plot_hyperplane(c, color):\n", " def line(x0):\n", " return (-(x0 * coef[c, 0]) - intercept[c]) / coef[c, 1]\n", "\n", " plt.plot([xmin, xmax], [line(xmin), line(xmax)], ls=\"--\", color=color)\n", "\n", " for i, color in zip(clf.classes_, colors):\n", " plot_hyperplane(i, color)\n", "\n", " plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "clf.coef_" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "clf.intercept_" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "clf.classes_" ] }, { "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 }