4. アソシエーション分析の例¶
全体の流れ
少し偏りのあるデータセットを作成。
orange3-associate パッケージによりアソシエーション分析。
参考
4.1. 環境構築¶
!pip install orange3
!pip install orange3-associate
Requirement already satisfied: orange3 in /usr/local/lib/python3.7/dist-packages (3.29.3)
Requirement already satisfied: keyrings.alt in /usr/local/lib/python3.7/dist-packages (from orange3) (4.0.2)
Requirement already satisfied: setuptools>=36.3 in /usr/local/lib/python3.7/dist-packages (from orange3) (57.0.0)
Requirement already satisfied: baycomp>=1.0.2 in /usr/local/lib/python3.7/dist-packages (from orange3) (1.0.2)
Requirement already satisfied: httpx<0.17,>=0.14.0 in /usr/local/lib/python3.7/dist-packages (from orange3) (0.16.1)
Requirement already satisfied: serverfiles in /usr/local/lib/python3.7/dist-packages (from orange3) (0.3.1)
Requirement already satisfied: pyyaml in /usr/local/lib/python3.7/dist-packages (from orange3) (3.13)
Requirement already satisfied: pandas>=1.0.0 in /usr/local/lib/python3.7/dist-packages (from orange3) (1.1.5)
Requirement already satisfied: pyqtgraph>=0.11.1 in /usr/local/lib/python3.7/dist-packages (from orange3) (0.12.1)
Requirement already satisfied: openTSNE>=0.6.0 in /usr/local/lib/python3.7/dist-packages (from orange3) (0.6.0)
Requirement already satisfied: orange-canvas-core<0.2a,>=0.1.21 in /usr/local/lib/python3.7/dist-packages (from orange3) (0.1.21)
Requirement already satisfied: bottleneck>=1.0.0 in /usr/local/lib/python3.7/dist-packages (from orange3) (1.3.2)
Requirement already satisfied: chardet>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from orange3) (3.0.4)
Requirement already satisfied: joblib>=0.9.4 in /usr/local/lib/python3.7/dist-packages (from orange3) (1.0.1)
Requirement already satisfied: openpyxl in /usr/local/lib/python3.7/dist-packages (from orange3) (2.5.9)
Requirement already satisfied: xlsxwriter in /usr/local/lib/python3.7/dist-packages (from orange3) (1.4.3)
Requirement already satisfied: matplotlib>=2.0.0 in /usr/local/lib/python3.7/dist-packages (from orange3) (3.2.2)
Requirement already satisfied: requests in /usr/local/lib/python3.7/dist-packages (from orange3) (2.23.0)
Requirement already satisfied: scipy>=0.16.1 in /usr/local/lib/python3.7/dist-packages (from orange3) (1.4.1)
Requirement already satisfied: AnyQt>=0.0.11 in /usr/local/lib/python3.7/dist-packages (from orange3) (0.0.13)
Requirement already satisfied: xlrd>=0.9.2 in /usr/local/lib/python3.7/dist-packages (from orange3) (1.1.0)
Requirement already satisfied: keyring in /usr/local/lib/python3.7/dist-packages (from orange3) (23.0.1)
Requirement already satisfied: pip>=9.0 in /usr/local/lib/python3.7/dist-packages (from orange3) (19.3.1)
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)
Requirement already satisfied: numpy>=1.16.0 in /usr/local/lib/python3.7/dist-packages (from orange3) (1.19.5)
Requirement already satisfied: PyQtWebEngine>=5.12 in /usr/local/lib/python3.7/dist-packages (from orange3) (5.15.4)
Requirement already satisfied: networkx in /usr/local/lib/python3.7/dist-packages (from orange3) (2.5.1)
Requirement already satisfied: orange-widget-base>=4.13.0 in /usr/local/lib/python3.7/dist-packages (from orange3) (4.13.1)
Requirement already satisfied: python-louvain>=0.13 in /usr/local/lib/python3.7/dist-packages (from orange3) (0.15)
Requirement already satisfied: PyQt5!=5.15.1,>=5.12 in /usr/local/lib/python3.7/dist-packages (from orange3) (5.15.4)
Requirement already satisfied: sniffio in /usr/local/lib/python3.7/dist-packages (from httpx<0.17,>=0.14.0->orange3) (1.2.0)
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)
Requirement already satisfied: certifi in /usr/local/lib/python3.7/dist-packages (from httpx<0.17,>=0.14.0->orange3) (2021.5.30)
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)
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)
Requirement already satisfied: pytz>=2017.2 in /usr/local/lib/python3.7/dist-packages (from pandas>=1.0.0->orange3) (2018.9)
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)
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)
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)
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)
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)
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)
Requirement already satisfied: et-xmlfile in /usr/local/lib/python3.7/dist-packages (from openpyxl->orange3) (1.1.0)
Requirement already satisfied: jdcal in /usr/local/lib/python3.7/dist-packages (from openpyxl->orange3) (1.4.1)
Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib>=2.0.0->orange3) (1.3.1)
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)
Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.7/dist-packages (from matplotlib>=2.0.0->orange3) (0.10.0)
Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests->orange3) (2.10)
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)
Requirement already satisfied: SecretStorage>=3.2; sys_platform == "linux" in /usr/local/lib/python3.7/dist-packages (from keyring->orange3) (3.3.1)
Requirement already satisfied: jeepney>=0.4.2; sys_platform == "linux" in /usr/local/lib/python3.7/dist-packages (from keyring->orange3) (0.6.0)
Requirement already satisfied: PyQt5-sip<13,>=12.8 in /usr/local/lib/python3.7/dist-packages (from PyQtWebEngine>=5.12->orange3) (12.9.0)
Requirement already satisfied: PyQtWebEngine-Qt5>=5.15 in /usr/local/lib/python3.7/dist-packages (from PyQtWebEngine>=5.12->orange3) (5.15.2)
Requirement already satisfied: decorator<5,>=4.3 in /usr/local/lib/python3.7/dist-packages (from networkx->orange3) (4.4.2)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
Requirement already satisfied: orange3-associate in /usr/local/lib/python3.7/dist-packages (1.1.8)
Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from orange3-associate) (1.19.5)
Requirement already satisfied: scipy in /usr/local/lib/python3.7/dist-packages (from orange3-associate) (1.4.1)
Requirement already satisfied: Orange3>=3.25.0 in /usr/local/lib/python3.7/dist-packages (from orange3-associate) (3.29.3)
Requirement already satisfied: pip>=9.0 in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (19.3.1)
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)
Requirement already satisfied: xlsxwriter in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (1.4.3)
Requirement already satisfied: openpyxl in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (2.5.9)
Requirement already satisfied: PyQtWebEngine>=5.12 in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (5.15.4)
Requirement already satisfied: python-louvain>=0.13 in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (0.15)
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)
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)
Requirement already satisfied: requests in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (2.23.0)
Requirement already satisfied: serverfiles in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (0.3.1)
Requirement already satisfied: networkx in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (2.5.1)
Requirement already satisfied: keyrings.alt in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (4.0.2)
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)
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)
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)
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)
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)
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)
Requirement already satisfied: setuptools>=36.3 in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (57.0.0)
Requirement already satisfied: pyyaml in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (3.13)
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)
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)
Requirement already satisfied: keyring in /usr/local/lib/python3.7/dist-packages (from Orange3>=3.25.0->orange3-associate) (23.0.1)
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)
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)
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)
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)
Requirement already satisfied: et-xmlfile in /usr/local/lib/python3.7/dist-packages (from openpyxl->Orange3>=3.25.0->orange3-associate) (1.1.0)
Requirement already satisfied: jdcal in /usr/local/lib/python3.7/dist-packages (from openpyxl->Orange3>=3.25.0->orange3-associate) (1.4.1)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)
4.2. データセット作成¶
import random
def make_dataset(num_sample=100, num_item=15):
"""データセット構築。
一度に購入した商品軍をリストとして作成。
偏り(傾向)を持たせるため、特定条件に合致する場合は別商品も購入するように設定。
num_sample (int): サンプル数。
num_item (int) : 商品種類数。
"""
dataset = []
for i in range(num_sample):
temp = []
size = random.randint(1, 3)
for j in range(size):
item = random.randint(1, num_item)
temp.append(item)
if item == 1: # 特別条件1
temp.append(10) # 商品1を購入した場合は、商品10も購入する。
elif item == 2: # 特別条件2(商品2を購入した場合は、70%の確率で商品3,4も購入する。)
if random.random() <= 0.7:
temp.append(3)
temp.append(4)
dataset.append(temp[:])
return dataset
dataset = make_dataset(num_sample=100)
print(dataset[0:5])
[[5, 9, 2, 3, 4], [8], [8, 4], [3], [9, 14]]
4.3. データセットをCSVファイルとして保存¶
def save_dataset(dataset, filename="data.basket"):
with open(filename, "w") as f:
for items in dataset:
line = ""
for index, value in enumerate(items):
if index == 0:
line += str(value)
else:
line += "," + str(value)
line += "\n"
f.write(line)
filename = "data.basket"
save_dataset(dataset, filename)
!head data.basket
5,9,2,3,4
8
8,4
3
9,14
15
15,6
14
11
11,2,3,4
4.4. アソシエーション分析¶
モジュール読み込み後、Orange.data.Table()によりCSVファイルを読み込む。各商品を OneHoe.encode によりone-hotエンコードする。
frequent_itemsets() によりカウント。第2引数 min_support は、抽出件数最小値を設定。(比率でも設定できる模様)
association_rules() によりアソシエーションルールの抽出。第2引数 min_conficende は、抽出する confidence の最小値を指定。
import Orange
from orangecontrib.associate.fpgrowth import *
# データファイル読み込み
tbl = Orange.data.Table(filename)
X, mapping = OneHot.encode(tbl)
itemsets = dict(frequent_itemsets(X, 5))
print(X[:5])
print(itemsets)
[[0, 1, 2, 3, 4], [5], [4, 5], [3], [1, 6]]
{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}
/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>.
""")
# アソシエーションルールの抽出
# 第2引数: min_confidence は確信度の最小値
rules = association_rules(itemsets, 0.2)
def decode_onehot(d):
items = OneHot.decode(d, tbl, mapping)
# ContinuousVariable の name 値を取得
return list(map(lambda v: v[1].name, items))
for P, Q, support, confidence in rules:
lhs = decode_onehot(P)
rhs = decode_onehot(Q)
print(f"lhs = {lhs}, rhs = {rhs}, support = {support}, confidence = {confidence}")
lhs = ['3', '4'], rhs = ['2'], support = 8, confidence = 1.0
lhs = ['2', '4'], rhs = ['3'], support = 8, confidence = 0.8888888888888888
lhs = ['4'], rhs = ['2', '3'], support = 8, confidence = 0.36363636363636365
lhs = ['2', '3'], rhs = ['4'], support = 8, confidence = 1.0
lhs = ['3'], rhs = ['2', '4'], support = 8, confidence = 0.38095238095238093
lhs = ['2'], rhs = ['3', '4'], support = 8, confidence = 0.7272727272727273
lhs = ['3'], rhs = ['2'], support = 8, confidence = 0.38095238095238093
lhs = ['2'], rhs = ['3'], support = 8, confidence = 0.7272727272727273
lhs = ['4'], rhs = ['9'], support = 5, confidence = 0.22727272727272727
lhs = ['9'], rhs = ['4'], support = 5, confidence = 0.35714285714285715
lhs = ['4'], rhs = ['2'], support = 9, confidence = 0.4090909090909091
lhs = ['2'], rhs = ['4'], support = 9, confidence = 0.8181818181818182
lhs = ['4'], rhs = ['3'], support = 8, confidence = 0.36363636363636365
lhs = ['3'], rhs = ['4'], support = 8, confidence = 0.38095238095238093
lhs = ['14'], rhs = ['11'], support = 5, confidence = 0.2631578947368421
lhs = ['11'], rhs = ['14'], support = 5, confidence = 0.38461538461538464
lhs = ['10'], rhs = ['3'], support = 5, confidence = 0.20833333333333334
lhs = ['3'], rhs = ['10'], support = 5, confidence = 0.23809523809523808
lhs = ['10'], rhs = ['4'], support = 6, confidence = 0.25
lhs = ['4'], rhs = ['10'], support = 6, confidence = 0.2727272727272727
lhs = ['10'], rhs = ['1'], support = 12, confidence = 0.5
lhs = ['1'], rhs = ['10'], support = 12, confidence = 1.0