2016-07-04 79 views
1

我有一個尺寸爲(878049, 6)的數據集。使用Orange提取關聯規則時的問題?

它看起來像這樣:

enter image description here

我想提取與其他列鏈接類別列關聯規則。因此,從文檔我試圖用Orange-Associate如下:

In: 

import Orange 
data = Orange.data.Table("data.csv") 

In: 

data.domain.attributes 

Out: 

    (DiscreteVariable('Category', values=['ARSON', 'ASSAULT', 'BAD CHECKS', 'BRIBERY', 'BURGLARY', ...]), 
DiscreteVariable('Descript', values=['ABANDONMENT OF CHILD', 'ABORTION', 'ACCESS CARD INFORMATION, PUBLICATION OF', 'ACCESS CARD INFORMATION, THEFT OF', 'ACCIDENTAL BURNS', ...]), 
DiscreteVariable('DayOfWeek', values=['Friday', 'Monday', 'Saturday', 'Sunday', 'Thursday', ...]), 
DiscreteVariable('PdDistrict', values=['BAYVIEW', 'CENTRAL', 'INGLESIDE', 'MISSION', 'NORTHERN', ...]), 
DiscreteVariable('Resolution', values=['ARREST, BOOKED', 'ARREST, CITED', 'CLEARED-CONTACT JUVENILE FOR MORE INFO', 'COMPLAINANT REFUSES TO PROSECUTE', 'DISTRICT ATTORNEY REFUSES TO PROSECUTE', ...])) 

In: 

from orangecontrib.associate.fpgrowth import * 

X, mapping = OneHot.encode(data, include_class=True) 

X 

Out: 
array([[False, False, False, ..., False, False, False], 
     [False, False, False, ..., False, False, False], 
     [False, False, False, ..., False, False, False], 
     ..., 
     [False, False, False, ..., False, False, False], 
     [False, False, False, ..., False, False, False], 
     [False, False, False, ..., False, False, False]], dtype=bool) 

In: 

sorted(mapping.items()) 

Out: 

[(0, (0, 0)), 
(1, (0, 1)), 
(2, (0, 2)), 
(3, (0, 3)), 
(4, (0, 4)), 
(5, (0, 5)), 
(6, (0, 6)), 
(7, (0, 7)), 
.... 
(950, (4, 15)), 
(951, (4, 16))] 

然後:

In: 

itemsets = dict(frequent_itemsets(X, .4)) 

len(itemsets) 

Out: 

1 

In: 

class_items = {item 

       for item, var, _ in OneHot.decode(mapping, data, mapping) 

       if var is data.domain.class_var} 
In: 
sorted(class_items) 

Out: 

[] 

我認爲,問題是我沒有正確的Orange table屈服。因此,我應該如何使用橙色加載數據集才能生成關聯規則?

更新

通過@ K3 --- RNC答案,我想這:

itemsets = dict(frequent_itemsets(X, .1)) 

print (len(itemsets)) 

print(itemsets) 

for itemset, _support in itemsets: 

    print(' '.join('{}={}'.format(var.name, val) 

        for _, var, val in OneHot.decode(itemset, data, mapping))) 

18 
{frozenset({935}): 206403, frozenset({20}): 92304, frozenset({928}): 119908, frozenset({924}): 129211, frozenset({946}): 526790, frozenset({921}): 116707, frozenset({946, 932}): 93924, frozenset({919}): 121584, frozenset({932}): 157182, frozenset({21}): 126182, frozenset({922}): 125038, frozenset({16}): 174900, frozenset({929}): 105296, frozenset({918}): 133734, frozenset({16, 946}): 156586, frozenset({925}): 89431, frozenset({923}): 124965, frozenset({920}): 126810} 

--------------------------------------------------------------------------- 
ValueError        Traceback (most recent call last) 
<ipython-input-83-83a24c082126> in <module>() 
     2 print (len(itemsets)) 
     3 print(itemsets) 
----> 4 for itemset, _support in itemsets: 
     5  print(' '.join('{}={}'.format(var.name, val) 
     6     for _, var, val in OneHot.decode(itemset, data, mapping))) 

ValueError: not enough values to unpack (expected 2, got 1) 

不過,我還是有同樣的問題...我不能提取關聯規則。

回答

0

您試圖在您的數據域中沒有任何類變量的情況下引發分類規則。如果您打印data.domain,您會看到您只有常規屬性和元數據。

[Category, DayOfWeek, PdDistrict, Resolution] {Descript, Address} 

要解決此問題,您需要將其中一個屬性設置爲類變量。

new_domain = Orange.data.Domain(list(data.domain.attributes[1:]), 
      data.domain.attributes[0], 
      metas=data.domain.metas) 

這將設置'Category'屬性作爲類變量。當然,你可以通過上面的例子設置自己的類變量。如果現在打印NEW_DOMAIN,你會看到這樣的事情:

[DayOfWeek, PdDistrict, Resolution | Category] {Descript, Address} 
+1

您不會將任何數據傳遞到新表,只有域。相反,你應該這樣做: '表= Orange.data.Table(new_domain,data)' – vijolica

+0

再次感謝您的幫助。這實際上是有用的。不過,目前我只提取了一個關聯規則:'Resolution = NONE - > Category = LARCENY/THEFT(supp:156586,conf:0.2972455817308605)''。任何想法爲什麼發生這種情況? –

1

你可以看到你的發現項集包含有:

# Minimum 20% support. Decrease for more results 
itemsets = dict(frequent_itemsets(X, .2)) 

for itemset, _support in itemsets.items(): 
    print(' '.join('{}={}'.format(var.name, val) 
        for _, var, val in OneHot.decode(itemset, data, mapping))) 

會打印:

Category=ASSAULT DayOfWeek=Friday ... 

或任何具有40%支持的項目組是。

+0

感謝您的支持,能否提供更詳細的示例?...迄今爲止,我嘗試了您的方法,但它不打印任何關聯規則! –

+1

奇怪。如果你打印(itemsets),你會看到什麼?那會很奇怪。請參閱上面的修改示例。 –

+0

當我印刷(商品)'我得到:'{frozenset({946}):526790}'。任何想法我做錯了什麼?謝謝您的幫助! –