Coupure prévue mardi 3 Août au matin pour maintenance du serveur. Nous faisons au mieux pour que celle-ci soit la plus brève possible.

library.py 2.5 KB
Newer Older
1
2
3
4
5
'''
Subgroup discovery library functions.

@author: Anze Vavpetic <anze.vavpetic@ijs.si>
'''
Anze Vavpetic's avatar
Anze Vavpetic committed
6
import json
7
from SubgroupDiscovery.SD_learner_classifier import SD_learner
Anze Vavpetic's avatar
Anze Vavpetic committed
8
from SubgroupDiscovery.SDRule import SDRules
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

class SubgroupDiscovery:
    # Available algorithms and their parameters
    algorithms = {
        'SD' : ['minSupport', 'g', 'beamWidth', 'max_rules'],
        'SD-Preprocess' : ['minSupport', 'g', 'beamWidth', 'max_rules'],
        'Apriori-SD' : ['minSupport', 'minConfidence', 'k', 'max_rules'],
        'CN2-SD' : ['k', 'max_rules']
    }
    # Parameter default values and descriptions
    parameter_info = {
        'minSupport' : (0.1, 'Minimum support'), 
        'minConfidence' : (0.7, 'Minimum confidence'),
        'g' : (1.0, 'Generalization parameter'),
        'beamWidth' : (5, 'Beam width'), 
        'k' : (4, 'Number of times covered before removed'), 
        'max_rules' : (0, 'Maximum number of subgroups (0=no limitation)')
    }
    # Parameter data types
    parameter_types = {
        'minSupport' : float, 
        'minConfidence' : float, 
        'g' : float,
        'beamWidth' : int,
        'k' : int,
        'max_rules' : int,
    }

37
def build_subgroups(input_dict):
38
    return {'rules' : None, 'classifier' : None}
39

40
41
42
43
def build_subgroups_finished(postdata, input_dict, output_dict):
    data = input_dict['data']
    widget_id = postdata['widget_id'][0]
    alg = postdata['algorithm'+widget_id][0]
44
    classValue = str(postdata['class'+widget_id][0])
45
46
47
48
49
    params = {'name' : alg, 'algorithm' : alg}
    for param in SubgroupDiscovery.algorithms[alg]:
        value = postdata[param+widget_id][0]
        if value != '':
            params[param] = SubgroupDiscovery.parameter_types[param](value)
50
51
    learner = SD_learner(**params)
    classifier = learner(data)
Anze Vavpetic's avatar
Anze Vavpetic committed
52
53
54
55
56
    rules = classifier.getRules(classValue)
    # add id's
    for i, rule in enumerate(rules.rules):
        rule.id = i
    return {'rules' : rules, 'classifier' : learner}
57
58
59

def subgroup_bar_visualization(input_dict):
    return {'rules' : None}
60

61
def subgroup_roc_visualization(input_dict):
Anze Vavpetic's avatar
Anze Vavpetic committed
62
63
64
65
66
67
68
69
70
71
    return {'rules' : []}

def select_subgroups(input_dict):
    return {'sel_rules' : []}

def select_subgroups_finished(postdata, input_dict, output_dict):
    sd_rules = input_dict['rules']
    selected_subgroups = [int(i) for i in json.loads(postdata['selected_subgroup_ids'][0])]
    rules = SDRules(filter(lambda r: r.id in selected_subgroups, sd_rules.rules), sd_rules.targetClassRule, sd_rules.algorithmName)
    return {'sel_rules' : rules}