{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "associationrules.ipynb",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "oo1FeOZvQ2d2"
      },
      "source": [
        "# アソシエーション分析の例\n",
        "- 全体の流れ\n",
        "  - 少し偏りのあるデータセットを作成。\n",
        "  - orange3-associate パッケージによりアソシエーション分析。\n",
        "- 参考\n",
        "  - [Python でアソシエーション分析 - Orange3-Associate](https://fits.hatenablog.com/entry/2018/01/09/211900)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "A71_siYIRTAM"
      },
      "source": [
        "## 環境構築"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "ecy6rFx4KIaC",
        "outputId": "35a97f10-f93c-4640-ff74-c19126637280"
      },
      "source": [
        "!pip install orange3\n",
        "!pip install orange3-associate"
      ],
      "execution_count": 1,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "Requirement already satisfied: orange3 in /usr/local/lib/python3.7/dist-packages (3.29.3)\n",
            "Requirement already satisfied: keyrings.alt in /usr/local/lib/python3.7/dist-packages (from orange3) (4.0.2)\n",
            "Requirement already satisfied: setuptools>=36.3 in /usr/local/lib/python3.7/dist-packages (from orange3) (57.0.0)\n",
            "Requirement already satisfied: baycomp>=1.0.2 in /usr/local/lib/python3.7/dist-packages (from orange3) (1.0.2)\n",
            "Requirement already satisfied: httpx<0.17,>=0.14.0 in /usr/local/lib/python3.7/dist-packages (from orange3) (0.16.1)\n",
            "Requirement already satisfied: serverfiles in /usr/local/lib/python3.7/dist-packages (from orange3) (0.3.1)\n",
            "Requirement already satisfied: pyyaml in /usr/local/lib/python3.7/dist-packages (from orange3) (3.13)\n",
            "Requirement already satisfied: pandas>=1.0.0 in /usr/local/lib/python3.7/dist-packages (from orange3) (1.1.5)\n",
            "Requirement already satisfied: pyqtgraph>=0.11.1 in /usr/local/lib/python3.7/dist-packages (from orange3) (0.12.1)\n",
            "Requirement already satisfied: openTSNE>=0.6.0 in /usr/local/lib/python3.7/dist-packages (from orange3) (0.6.0)\n",
            "Requirement already satisfied: orange-canvas-core<0.2a,>=0.1.21 in /usr/local/lib/python3.7/dist-packages (from orange3) (0.1.21)\n",
            "Requirement already satisfied: bottleneck>=1.0.0 in /usr/local/lib/python3.7/dist-packages (from orange3) (1.3.2)\n",
            "Requirement already satisfied: chardet>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from orange3) (3.0.4)\n",
            "Requirement already satisfied: joblib>=0.9.4 in /usr/local/lib/python3.7/dist-packages (from orange3) (1.0.1)\n",
            "Requirement already satisfied: openpyxl in /usr/local/lib/python3.7/dist-packages (from orange3) (2.5.9)\n",
            "Requirement already satisfied: xlsxwriter in /usr/local/lib/python3.7/dist-packages (from orange3) (1.4.3)\n",
            "Requirement already satisfied: matplotlib>=2.0.0 in /usr/local/lib/python3.7/dist-packages (from orange3) (3.2.2)\n",
            "Requirement already satisfied: requests in /usr/local/lib/python3.7/dist-packages (from orange3) (2.23.0)\n",
            "Requirement already satisfied: scipy>=0.16.1 in /usr/local/lib/python3.7/dist-packages (from orange3) (1.4.1)\n",
            "Requirement already satisfied: AnyQt>=0.0.11 in /usr/local/lib/python3.7/dist-packages (from orange3) (0.0.13)\n",
            "Requirement already satisfied: xlrd>=0.9.2 in /usr/local/lib/python3.7/dist-packages (from orange3) (1.1.0)\n",
            "Requirement already satisfied: keyring in /usr/local/lib/python3.7/dist-packages (from orange3) (23.0.1)\n",
            "Requirement already satisfied: pip>=9.0 in /usr/local/lib/python3.7/dist-packages (from orange3) (19.3.1)\n",
            "Requirement already satisfied: scikit-learn!=0.23.0,>=0.22.0 in /usr/local/lib/python3.7/dist-packages (from orange3) (0.22.2.post1)\n",
            "Requirement already satisfied: numpy>=1.16.0 in /usr/local/lib/python3.7/dist-packages (from orange3) (1.19.5)\n",
            "Requirement already satisfied: PyQtWebEngine>=5.12 in /usr/local/lib/python3.7/dist-packages (from orange3) (5.15.4)\n",
            "Requirement already satisfied: networkx in /usr/local/lib/python3.7/dist-packages (from orange3) (2.5.1)\n",
            "Requirement already satisfied: orange-widget-base>=4.13.0 in /usr/local/lib/python3.7/dist-packages (from orange3) (4.13.1)\n",
            "Requirement already satisfied: python-louvain>=0.13 in /usr/local/lib/python3.7/dist-packages (from orange3) (0.15)\n",
            "Requirement already satisfied: PyQt5!=5.15.1,>=5.12 in /usr/local/lib/python3.7/dist-packages (from orange3) (5.15.4)\n",
            "Requirement already satisfied: sniffio in /usr/local/lib/python3.7/dist-packages (from httpx<0.17,>=0.14.0->orange3) (1.2.0)\n",
            "Requirement already satisfied: rfc3986[idna2008]<2,>=1.3 in /usr/local/lib/python3.7/dist-packages (from httpx<0.17,>=0.14.0->orange3) (1.5.0)\n",
            "Requirement already satisfied: certifi in /usr/local/lib/python3.7/dist-packages (from httpx<0.17,>=0.14.0->orange3) (2021.5.30)\n",
            "Requirement already satisfied: httpcore==0.12.* in /usr/local/lib/python3.7/dist-packages (from httpx<0.17,>=0.14.0->orange3) (0.12.3)\n",
            "Requirement already satisfied: python-dateutil>=2.7.3 in /usr/local/lib/python3.7/dist-packages (from pandas>=1.0.0->orange3) (2.8.1)\n",
            "Requirement already satisfied: pytz>=2017.2 in /usr/local/lib/python3.7/dist-packages (from pandas>=1.0.0->orange3) (2018.9)\n",
            "Requirement already satisfied: cachecontrol[filecache] in /usr/local/lib/python3.7/dist-packages (from orange-canvas-core<0.2a,>=0.1.21->orange3) (0.12.6)\n",
            "Requirement already satisfied: qasync in /usr/local/lib/python3.7/dist-packages (from orange-canvas-core<0.2a,>=0.1.21->orange3) (0.16.0)\n",
            "Requirement already satisfied: commonmark>=0.8.1 in /usr/local/lib/python3.7/dist-packages (from orange-canvas-core<0.2a,>=0.1.21->orange3) (0.9.1)\n",
            "Requirement already satisfied: docutils in /usr/local/lib/python3.7/dist-packages (from orange-canvas-core<0.2a,>=0.1.21->orange3) (0.17.1)\n",
            "Requirement already satisfied: dictdiffer in /usr/local/lib/python3.7/dist-packages (from orange-canvas-core<0.2a,>=0.1.21->orange3) (0.8.1)\n",
            "Requirement already satisfied: importlib-metadata; python_version < \"3.8\" in /usr/local/lib/python3.7/dist-packages (from orange-canvas-core<0.2a,>=0.1.21->orange3) (4.5.0)\n",
            "Requirement already satisfied: et-xmlfile in /usr/local/lib/python3.7/dist-packages (from openpyxl->orange3) (1.1.0)\n",
            "Requirement already satisfied: jdcal in /usr/local/lib/python3.7/dist-packages (from openpyxl->orange3) (1.4.1)\n",
            "Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib>=2.0.0->orange3) (1.3.1)\n",
            "Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib>=2.0.0->orange3) (2.4.7)\n",
            "Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.7/dist-packages (from matplotlib>=2.0.0->orange3) (0.10.0)\n",
            "Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests->orange3) (2.10)\n",
            "Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests->orange3) (1.24.3)\n",
            "Requirement already satisfied: SecretStorage>=3.2; sys_platform == \"linux\" in /usr/local/lib/python3.7/dist-packages (from keyring->orange3) (3.3.1)\n",
            "Requirement already satisfied: jeepney>=0.4.2; sys_platform == \"linux\" in /usr/local/lib/python3.7/dist-packages (from keyring->orange3) (0.6.0)\n",
            "Requirement already satisfied: PyQt5-sip<13,>=12.8 in /usr/local/lib/python3.7/dist-packages (from PyQtWebEngine>=5.12->orange3) (12.9.0)\n",
            "Requirement already satisfied: PyQtWebEngine-Qt5>=5.15 in /usr/local/lib/python3.7/dist-packages (from PyQtWebEngine>=5.12->orange3) (5.15.2)\n",
            "Requirement already satisfied: decorator<5,>=4.3 in /usr/local/lib/python3.7/dist-packages (from networkx->orange3) (4.4.2)\n",
            "Requirement already satisfied: typing-extensions>=3.7.4.3 in /usr/local/lib/python3.7/dist-packages (from orange-widget-base>=4.13.0->orange3) (3.7.4.3)\n",
            "Requirement already satisfied: PyQt5-Qt5>=5.15 in /usr/local/lib/python3.7/dist-packages (from PyQt5!=5.15.1,>=5.12->orange3) (5.15.2)\n",
            "Requirement already satisfied: h11==0.* in /usr/local/lib/python3.7/dist-packages (from httpcore==0.12.*->httpx<0.17,>=0.14.0->orange3) (0.12.0)\n",
            "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.7/dist-packages (from python-dateutil>=2.7.3->pandas>=1.0.0->orange3) (1.15.0)\n",
            "Requirement already satisfied: msgpack>=0.5.2 in /usr/local/lib/python3.7/dist-packages (from cachecontrol[filecache]->orange-canvas-core<0.2a,>=0.1.21->orange3) (1.0.2)\n",
            "Requirement already satisfied: lockfile>=0.9; extra == \"filecache\" in /usr/local/lib/python3.7/dist-packages (from cachecontrol[filecache]->orange-canvas-core<0.2a,>=0.1.21->orange3) (0.12.2)\n",
            "Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.7/dist-packages (from importlib-metadata; python_version < \"3.8\"->orange-canvas-core<0.2a,>=0.1.21->orange3) (3.4.1)\n",
            "Requirement already satisfied: cryptography>=2.0 in /usr/local/lib/python3.7/dist-packages (from SecretStorage>=3.2; sys_platform == \"linux\"->keyring->orange3) (3.4.7)\n",
            "Requirement already satisfied: cffi>=1.12 in /usr/local/lib/python3.7/dist-packages (from cryptography>=2.0->SecretStorage>=3.2; sys_platform == \"linux\"->keyring->orange3) (1.14.5)\n",
            "Requirement already satisfied: pycparser in /usr/local/lib/python3.7/dist-packages (from cffi>=1.12->cryptography>=2.0->SecretStorage>=3.2; sys_platform == \"linux\"->keyring->orange3) (2.20)\n",
            "Requirement already satisfied: orange3-associate in /usr/local/lib/python3.7/dist-packages (1.1.8)\n",
            "Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from orange3-associate) (1.19.5)\n",
            "Requirement already satisfied: scipy in /usr/local/lib/python3.7/dist-packages (from orange3-associate) (1.4.1)\n",
            "Requirement already satisfied: Orange3>=3.25.0 in /usr/local/lib/python3.7/dist-packages (from orange3-associate) (3.29.3)\n",
            "Requirement already satisfied: pip>=9.0 in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (19.3.1)\n",
            "Requirement already satisfied: baycomp>=1.0.2 in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (1.0.2)\n",
            "Requirement already satisfied: xlsxwriter in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (1.4.3)\n",
            "Requirement already satisfied: openpyxl in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (2.5.9)\n",
            "Requirement already satisfied: PyQtWebEngine>=5.12 in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (5.15.4)\n",
            "Requirement already satisfied: python-louvain>=0.13 in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (0.15)\n",
            "Requirement already satisfied: joblib>=0.9.4 in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (1.0.1)\n",
            "Requirement already satisfied: scikit-learn!=0.23.0,>=0.22.0 in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (0.22.2.post1)\n",
            "Requirement already satisfied: requests in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (2.23.0)\n",
            "Requirement already satisfied: serverfiles in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (0.3.1)\n",
            "Requirement already satisfied: networkx in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (2.5.1)\n",
            "Requirement already satisfied: keyrings.alt in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (4.0.2)\n",
            "Requirement already satisfied: xlrd>=0.9.2 in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (1.1.0)\n",
            "Requirement already satisfied: bottleneck>=1.0.0 in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (1.3.2)\n",
            "Requirement already satisfied: AnyQt>=0.0.11 in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (0.0.13)\n",
            "Requirement already satisfied: httpx<0.17,>=0.14.0 in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (0.16.1)\n",
            "Requirement already satisfied: pandas>=1.0.0 in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (1.1.5)\n",
            "Requirement already satisfied: pyqtgraph>=0.11.1 in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (0.12.1)\n",
            "Requirement already satisfied: setuptools>=36.3 in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (57.0.0)\n",
            "Requirement already satisfied: pyyaml in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (3.13)\n",
            "Requirement already satisfied: chardet>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (3.0.4)\n",
            "Requirement already satisfied: openTSNE>=0.6.0 in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (0.6.0)\n",
            "Requirement already satisfied: keyring in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (23.0.1)\n",
            "Requirement already satisfied: orange-widget-base>=4.13.0 in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (4.13.1)\n",
            "Requirement already satisfied: matplotlib>=2.0.0 in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (3.2.2)\n",
            "Requirement already satisfied: orange-canvas-core<0.2a,>=0.1.21 in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (0.1.21)\n",
            "Requirement already satisfied: PyQt5!=5.15.1,>=5.12 in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (5.15.4)\n",
            "Requirement already satisfied: et-xmlfile in /usr/local/lib/python3.7/dist-packages (from openpyxl->Orange3>=3.25.0->orange3-associate) (1.1.0)\n",
            "Requirement already satisfied: jdcal in /usr/local/lib/python3.7/dist-packages (from openpyxl->Orange3>=3.25.0->orange3-associate) (1.4.1)\n",
            "Requirement already satisfied: PyQt5-sip<13,>=12.8 in /usr/local/lib/python3.7/dist-packages (from PyQtWebEngine>=5.12->Orange3>=3.25.0->orange3-associate) (12.9.0)\n",
            "Requirement already satisfied: PyQtWebEngine-Qt5>=5.15 in /usr/local/lib/python3.7/dist-packages (from PyQtWebEngine>=5.12->Orange3>=3.25.0->orange3-associate) (5.15.2)\n",
            "Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests->Orange3>=3.25.0->orange3-associate) (2.10)\n",
            "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests->Orange3>=3.25.0->orange3-associate) (2021.5.30)\n",
            "Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests->Orange3>=3.25.0->orange3-associate) (1.24.3)\n",
            "Requirement already satisfied: decorator<5,>=4.3 in /usr/local/lib/python3.7/dist-packages (from networkx->Orange3>=3.25.0->orange3-associate) (4.4.2)\n",
            "Requirement already satisfied: httpcore==0.12.* in /usr/local/lib/python3.7/dist-packages (from httpx<0.17,>=0.14.0->Orange3>=3.25.0->orange3-associate) (0.12.3)\n",
            "Requirement already satisfied: rfc3986[idna2008]<2,>=1.3 in /usr/local/lib/python3.7/dist-packages (from httpx<0.17,>=0.14.0->Orange3>=3.25.0->orange3-associate) (1.5.0)\n",
            "Requirement already satisfied: sniffio in /usr/local/lib/python3.7/dist-packages (from httpx<0.17,>=0.14.0->Orange3>=3.25.0->orange3-associate) (1.2.0)\n",
            "Requirement already satisfied: pytz>=2017.2 in /usr/local/lib/python3.7/dist-packages (from pandas>=1.0.0->Orange3>=3.25.0->orange3-associate) (2018.9)\n",
            "Requirement already satisfied: python-dateutil>=2.7.3 in /usr/local/lib/python3.7/dist-packages (from pandas>=1.0.0->Orange3>=3.25.0->orange3-associate) (2.8.1)\n",
            "Requirement already satisfied: jeepney>=0.4.2; sys_platform == \"linux\" in /usr/local/lib/python3.7/dist-packages (from keyring->Orange3>=3.25.0->orange3-associate) (0.6.0)\n",
            "Requirement already satisfied: SecretStorage>=3.2; sys_platform == \"linux\" in /usr/local/lib/python3.7/dist-packages (from keyring->Orange3>=3.25.0->orange3-associate) (3.3.1)\n",
            "Requirement already satisfied: importlib-metadata>=3.6 in /usr/local/lib/python3.7/dist-packages (from keyring->Orange3>=3.25.0->orange3-associate) (4.5.0)\n",
            "Requirement already satisfied: typing-extensions>=3.7.4.3 in /usr/local/lib/python3.7/dist-packages (from orange-widget-base>=4.13.0->Orange3>=3.25.0->orange3-associate) (3.7.4.3)\n",
            "Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.7/dist-packages (from matplotlib>=2.0.0->Orange3>=3.25.0->orange3-associate) (0.10.0)\n",
            "Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib>=2.0.0->Orange3>=3.25.0->orange3-associate) (2.4.7)\n",
            "Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib>=2.0.0->Orange3>=3.25.0->orange3-associate) (1.3.1)\n",
            "Requirement already satisfied: docutils in /usr/local/lib/python3.7/dist-packages (from orange-canvas-core<0.2a,>=0.1.21->Orange3>=3.25.0->orange3-associate) (0.17.1)\n",
            "Requirement already satisfied: dictdiffer in /usr/local/lib/python3.7/dist-packages (from orange-canvas-core<0.2a,>=0.1.21->Orange3>=3.25.0->orange3-associate) (0.8.1)\n",
            "Requirement already satisfied: cachecontrol[filecache] in /usr/local/lib/python3.7/dist-packages (from orange-canvas-core<0.2a,>=0.1.21->Orange3>=3.25.0->orange3-associate) (0.12.6)\n",
            "Requirement already satisfied: qasync in /usr/local/lib/python3.7/dist-packages (from orange-canvas-core<0.2a,>=0.1.21->Orange3>=3.25.0->orange3-associate) (0.16.0)\n",
            "Requirement already satisfied: commonmark>=0.8.1 in /usr/local/lib/python3.7/dist-packages (from orange-canvas-core<0.2a,>=0.1.21->Orange3>=3.25.0->orange3-associate) (0.9.1)\n",
            "Requirement already satisfied: PyQt5-Qt5>=5.15 in /usr/local/lib/python3.7/dist-packages (from PyQt5!=5.15.1,>=5.12->Orange3>=3.25.0->orange3-associate) (5.15.2)\n",
            "Requirement already satisfied: h11==0.* in /usr/local/lib/python3.7/dist-packages (from httpcore==0.12.*->httpx<0.17,>=0.14.0->Orange3>=3.25.0->orange3-associate) (0.12.0)\n",
            "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.7/dist-packages (from python-dateutil>=2.7.3->pandas>=1.0.0->Orange3>=3.25.0->orange3-associate) (1.15.0)\n",
            "Requirement already satisfied: cryptography>=2.0 in /usr/local/lib/python3.7/dist-packages (from SecretStorage>=3.2; sys_platform == \"linux\"->keyring->Orange3>=3.25.0->orange3-associate) (3.4.7)\n",
            "Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.7/dist-packages (from importlib-metadata>=3.6->keyring->Orange3>=3.25.0->orange3-associate) (3.4.1)\n",
            "Requirement already satisfied: msgpack>=0.5.2 in /usr/local/lib/python3.7/dist-packages (from cachecontrol[filecache]->orange-canvas-core<0.2a,>=0.1.21->Orange3>=3.25.0->orange3-associate) (1.0.2)\n",
            "Requirement already satisfied: lockfile>=0.9; extra == \"filecache\" in /usr/local/lib/python3.7/dist-packages (from cachecontrol[filecache]->orange-canvas-core<0.2a,>=0.1.21->Orange3>=3.25.0->orange3-associate) (0.12.2)\n",
            "Requirement already satisfied: cffi>=1.12 in /usr/local/lib/python3.7/dist-packages (from cryptography>=2.0->SecretStorage>=3.2; sys_platform == \"linux\"->keyring->Orange3>=3.25.0->orange3-associate) (1.14.5)\n",
            "Requirement already satisfied: pycparser in /usr/local/lib/python3.7/dist-packages (from cffi>=1.12->cryptography>=2.0->SecretStorage>=3.2; sys_platform == \"linux\"->keyring->Orange3>=3.25.0->orange3-associate) (2.20)\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "WHxgYiFTRVkM"
      },
      "source": [
        "## データセット作成"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "9FtaywVGIWvl",
        "outputId": "1ef75f51-e41c-4b2f-ca10-cd1d8ba8edc0"
      },
      "source": [
        "import random\n",
        "\n",
        "def make_dataset(num_sample=100, num_item=15):\n",
        "  \"\"\"データセット構築。\n",
        "  一度に購入した商品軍をリストとして作成。\n",
        "  偏り（傾向）を持たせるため、特定条件に合致する場合は別商品も購入するように設定。\n",
        "\n",
        "  num_sample (int): サンプル数。\n",
        "  num_item (int)  : 商品種類数。\n",
        "  \"\"\"\n",
        "  dataset = []\n",
        "  for i in range(num_sample):\n",
        "    temp = []\n",
        "    size = random.randint(1, 3)\n",
        "    for j in range(size):\n",
        "      item = random.randint(1, num_item)\n",
        "      temp.append(item)\n",
        "      if item == 1:      # 特別条件1\n",
        "        temp.append(10)  # 　商品1を購入した場合は、商品10も購入する。\n",
        "      elif item == 2:    # 特別条件2(商品2を購入した場合は、70%の確率で商品3,4も購入する。)\n",
        "        if random.random() <= 0.7:\n",
        "          temp.append(3)\n",
        "          temp.append(4)\n",
        "    dataset.append(temp[:])\n",
        "  return dataset\n",
        "\n",
        "dataset = make_dataset(num_sample=100)\n",
        "print(dataset[0:5])"
      ],
      "execution_count": 2,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[[5, 9, 2, 3, 4], [8], [8, 4], [3], [9, 14]]\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Gcs0rvjKR1xy"
      },
      "source": [
        "## データセットをCSVファイルとして保存"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "H0Bu4TpUOGmK",
        "outputId": "4240ed50-aef8-4c8c-cd38-6bd101767d7e"
      },
      "source": [
        "def save_dataset(dataset, filename=\"data.basket\"):\n",
        "  with open(filename, \"w\") as f:\n",
        "    for items in dataset:\n",
        "      line = \"\"\n",
        "      for index, value in enumerate(items):\n",
        "        if index == 0:\n",
        "          line += str(value)\n",
        "        else:\n",
        "          line += \",\" + str(value)\n",
        "      line += \"\\n\"\n",
        "      f.write(line)\n",
        "\n",
        "filename = \"data.basket\"\n",
        "save_dataset(dataset, filename)\n",
        "!head data.basket"
      ],
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "5,9,2,3,4\n",
            "8\n",
            "8,4\n",
            "3\n",
            "9,14\n",
            "15\n",
            "15,6\n",
            "14\n",
            "11\n",
            "11,2,3,4\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "YHRuyIkDR6Sv"
      },
      "source": [
        "## アソシエーション分析\n",
        "- モジュール読み込み後、Orange.data.Table()によりCSVファイルを読み込む。各商品を OneHoe.encode によりone-hotエンコードする。\n",
        "- frequent_itemsets() によりカウント。第2引数 min_support は、抽出件数最小値を設定。（比率でも設定できる模様）\n",
        "- association_rules() によりアソシエーションルールの抽出。第2引数 min_conficende は、抽出する confidence の最小値を指定。"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "8SrcLryvNutY"
      },
      "source": [
        "import Orange\n",
        "from orangecontrib.associate.fpgrowth import *"
      ],
      "execution_count": 4,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "3BkxZ5KAPaff",
        "outputId": "b4381076-040e-46c3-b0d2-b967a938eaa0"
      },
      "source": [
        "# データファイル読み込み\n",
        "tbl = Orange.data.Table(filename)\n",
        "\n",
        "X, mapping = OneHot.encode(tbl)\n",
        "\n",
        "itemsets = dict(frequent_itemsets(X, 5))\n",
        "print(X[:5])\n",
        "print(itemsets)"
      ],
      "execution_count": 5,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "[[0, 1, 2, 3, 4], [5], [4, 5], [3], [1, 6]]\n",
            "{frozenset({0}): 13, frozenset({1}): 14, frozenset({2}): 11, frozenset({3}): 21, frozenset({2, 3}): 8, frozenset({4}): 22, frozenset({1, 4}): 5, frozenset({2, 4}): 9, frozenset({3, 4}): 8, frozenset({2, 3, 4}): 8, frozenset({5}): 10, frozenset({6}): 19, frozenset({7}): 16, frozenset({8}): 16, frozenset({9}): 13, frozenset({9, 6}): 5, frozenset({10}): 10, frozenset({11}): 14, frozenset({12}): 11, frozenset({13}): 12, frozenset({14}): 24, frozenset({3, 14}): 5, frozenset({4, 14}): 6, frozenset({13, 14}): 12}\n"
          ],
          "name": "stdout"
        },
        {
          "output_type": "stream",
          "text": [
            "/usr/local/lib/python3.7/dist-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use \"pip install psycopg2-binary\" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.\n",
            "  \"\"\")\n"
          ],
          "name": "stderr"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "PAs5da3KPlJW"
      },
      "source": [
        "# アソシエーションルールの抽出\n",
        "# 第2引数: min_confidence は確信度の最小値\n",
        "rules = association_rules(itemsets, 0.2)"
      ],
      "execution_count": 6,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "jaP8SOILPrmR",
        "outputId": "e647fdab-262e-42d9-854b-61df18742991"
      },
      "source": [
        "def decode_onehot(d):\n",
        "    items = OneHot.decode(d, tbl, mapping)\n",
        "    # ContinuousVariable の name 値を取得\n",
        "    return list(map(lambda v: v[1].name, items))\n",
        "\n",
        "for P, Q, support, confidence in rules:\n",
        "  lhs = decode_onehot(P)\n",
        "  rhs = decode_onehot(Q)\n",
        "  print(f\"lhs = {lhs}, rhs = {rhs}, support = {support}, confidence = {confidence}\")\n"
      ],
      "execution_count": 7,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "lhs = ['3', '4'], rhs = ['2'], support = 8, confidence = 1.0\n",
            "lhs = ['2', '4'], rhs = ['3'], support = 8, confidence = 0.8888888888888888\n",
            "lhs = ['4'], rhs = ['2', '3'], support = 8, confidence = 0.36363636363636365\n",
            "lhs = ['2', '3'], rhs = ['4'], support = 8, confidence = 1.0\n",
            "lhs = ['3'], rhs = ['2', '4'], support = 8, confidence = 0.38095238095238093\n",
            "lhs = ['2'], rhs = ['3', '4'], support = 8, confidence = 0.7272727272727273\n",
            "lhs = ['3'], rhs = ['2'], support = 8, confidence = 0.38095238095238093\n",
            "lhs = ['2'], rhs = ['3'], support = 8, confidence = 0.7272727272727273\n",
            "lhs = ['4'], rhs = ['9'], support = 5, confidence = 0.22727272727272727\n",
            "lhs = ['9'], rhs = ['4'], support = 5, confidence = 0.35714285714285715\n",
            "lhs = ['4'], rhs = ['2'], support = 9, confidence = 0.4090909090909091\n",
            "lhs = ['2'], rhs = ['4'], support = 9, confidence = 0.8181818181818182\n",
            "lhs = ['4'], rhs = ['3'], support = 8, confidence = 0.36363636363636365\n",
            "lhs = ['3'], rhs = ['4'], support = 8, confidence = 0.38095238095238093\n",
            "lhs = ['14'], rhs = ['11'], support = 5, confidence = 0.2631578947368421\n",
            "lhs = ['11'], rhs = ['14'], support = 5, confidence = 0.38461538461538464\n",
            "lhs = ['10'], rhs = ['3'], support = 5, confidence = 0.20833333333333334\n",
            "lhs = ['3'], rhs = ['10'], support = 5, confidence = 0.23809523809523808\n",
            "lhs = ['10'], rhs = ['4'], support = 6, confidence = 0.25\n",
            "lhs = ['4'], rhs = ['10'], support = 6, confidence = 0.2727272727272727\n",
            "lhs = ['10'], rhs = ['1'], support = 12, confidence = 0.5\n",
            "lhs = ['1'], rhs = ['10'], support = 12, confidence = 1.0\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "JJAKtrhSPyIL"
      },
      "source": [
        ""
      ],
      "execution_count": 7,
      "outputs": []
    }
  ]
}