7
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

OrangeでPythonからAprioriを動かす

Posted at

はじめに

  • AprioriのPython実装を探してるとどうやらOrangeで実装されているので試してみたときのメモ
  • Orange is a component-based data mining software.

注意

  • 新しいバージョンのOrange 3 と 古いバージョンのOrange 2(2016/9/11 時点では、Orange 2.7.8) がある

  • Orange 3 にAprioriモジュールが見つからない。。can't find associate module

  • ということで Orange 2 をインストールをする

インストール

Ubuntu にインストールした例を示す

公式サイトよりソースファイルをダウンロードして、展開

Python Software Foundationにあるとおりビルドしてインストール

python setup.py build
python setup.py install

scipyが必要なのでなければインストールしておく

Apriori

アソシエーション分析を参考にした

データは以下のように用意。拡張子がbasketである必要がある

$ more hayes-roth-train1-1.basket
a2,b2,c3,d4,D3
a3,b2,c1,d3,D1
<snip>

実行

>>> import Orange
>>> data = Orange.data.Table('hayes-roth-train1-1.basket')
>>> rules = Orange.associate.AssociationRulesSparseInducer(data, support=0.03, confidence=0.2, classification_rules=1, store_examples=True)
>>> print "%4s %4s %4s  %4s" % ("Supp", "Conf", "Lift", "Rule")
Supp Conf Lift  Rule
>>> for r in rules[:5]:
...     print "%4.1f %4.1f %4.1f   %s" % (r.support, r.confidence, r.lift, r)
...
 0.0  0.2  3.6   b4 -> c4
 0.0  0.3  3.6   c4 -> b4
 0.0  0.2  7.2   c4 -> b4 a1
 0.0  0.5  6.0   c4 a1 -> b4
 0.0  0.2  7.2   c4 -> b4 a1 D3

Ruleを取り出す

以下のドキュメントを見て動かしてみる

Association rules and frequent itemsets
Orange.data.Instance
Orange.data.Value

>>> len(rules)
400
>>> rules[383]
b2 a2 -> c1
>>> rules[383].left
[], {"b2":1.000, "a2":1.000}
>>> rules[383].left.get_metas(str).keys()
['a2', 'b2']
>>> rules[383].right.get_metas(str).keys()
['c1']
>>> rules[383].confidence
0.7333333492279053
>>> rules[383].support
0.07692307978868484
>>> rules[383].n_applies_both
11.0

対象がルールにマッチするか

>>> rule = rules[383]
>>> for d in data:
...     if rule.appliesBoth(d):
...         print d
...
[], {"a2":1.000, "b2":1.000, "c1":1.000, "d3":1.000, "D1":1.000}
[], {"a2":1.000, "b2":1.000, "c1":1.000, "d2":1.000, "D2":1.000}
[], {"a2":1.000, "b2":1.000, "c1":1.000, "d2":1.000, "D2":1.000}
[], {"a2":1.000, "b2":1.000, "c1":1.000, "d2":1.000, "D2":1.000}
[], {"a2":1.000, "b2":1.000, "c1":1.000, "d3":1.000, "D2":1.000}
[], {"a2":1.000, "b2":1.000, "c1":1.000, "d4":1.000, "D3":1.000}
[], {"a2":1.000, "b2":1.000, "c1":1.000, "d1":1.000, "D1":1.000}
[], {"a2":1.000, "b2":1.000, "c1":1.000, "d3":1.000, "D1":1.000}
[], {"a2":1.000, "b2":1.000, "c1":1.000, "d1":1.000, "D1":1.000}
[], {"a2":1.000, "b2":1.000, "c1":1.000, "d2":1.000, "D2":1.000}
[], {"a2":1.000, "b2":1.000, "c1":1.000, "d1":1.000, "D1":1.000}

>>> rule.examples
Orange.data.Table 'table'
>>> rule.match_both
<2, 3, 5, 40, 87, 105, 111, 116, 118, 135, 137>

おわりに

  • 速度が出るかはこれからチェック
  • scikit-learnとの違いはどこかに載っているのかな
7
11
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
11

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?