<s> """<STR_LIT>""" <EOL> import sys <EOL> import math <EOL> import logging <EOL> from bigml . api import FINISHED <EOL> from bigml . api import ( BigML , get_association_id , get_status ) <EOL> from bigml . basemodel import retrieve_resource <EOL> from bigml . basemodel import ONLY_MODEL <EOL> from bigml . model import STORAGE <EOL> from bigml . modelfields import ModelFields <EOL> from bigml . associationrule import AssociationRule <EOL> from bigml . item import Item <EOL> from bigml . io import UnicodeWriter <EOL> LOGGER = logging . getLogger ( '<STR_LIT>' ) <EOL> RULE_HEADERS = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" ] <EOL> ASSOCIATION_METRICS = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> SCORES = ASSOCIATION_METRICS [ : - <NUM_LIT:1> ] <EOL> METRIC_LITERALS = { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" } <EOL> INDENT = "<STR_LIT:U+0020>" * <NUM_LIT:4> <EOL> DEFAULT_K = <NUM_LIT:100> <EOL> DEFAULT_SEARCH_STRATEGY = "<STR_LIT>" <EOL> NO_ITEMS = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def get_metric_string ( rule , reverse = False ) : <EOL> """<STR_LIT>""" <EOL> metric_values = [ ] <EOL> for metric in ASSOCIATION_METRICS : <EOL> if reverse and metric == '<STR_LIT>' : <EOL> metric_key = '<STR_LIT>' <EOL> else : <EOL> metric_key = metric <EOL> metric_value = getattr ( rule , metric_key ) <EOL> if isinstance ( metric_value , list ) : <EOL> metric_values . append ( "<STR_LIT>" % ( <EOL> METRIC_LITERALS [ metric ] , ( ( round ( metric_value [ <NUM_LIT:0> ] , <NUM_LIT:4> ) * <NUM_LIT:100> ) ) , metric_value [ <NUM_LIT:1> ] ) ) <EOL> elif metric == '<STR_LIT>' : <EOL> metric_values . append ( "<STR_LIT>" % ( <EOL> METRIC_LITERALS [ metric ] , ( ( round ( metric_value , <NUM_LIT:4> ) * <NUM_LIT:100> ) ) ) ) <EOL> else : <EOL> metric_values . append ( "<STR_LIT>" % ( <EOL> METRIC_LITERALS [ metric ] , metric_value ) ) <EOL> return "<STR_LIT>" . join ( metric_values ) <EOL> class Association ( ModelFields ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , association , api = None ) : <EOL> self . resource_id = None <EOL> self . complement = None <EOL> self . discretization = { } <EOL> self . field_discretizations = { } <EOL> self . items = [ ] <EOL> self . k = None <EOL> self . max_lhs = None <EOL> self . min_coverage = None <EOL> self . min_leverage = None <EOL> self . min_strength = None <EOL> self . min_support = None <EOL> self . min_lift = None <EOL> self . prune = None <EOL> self . search_strategy = DEFAULT_SEARCH_STRATEGY <EOL> self . rules = [ ] <EOL> self . significance_level = None <EOL> if not ( isinstance ( association , dict ) and '<STR_LIT>' in association and <EOL> association [ '<STR_LIT>' ] is not None ) : <EOL> if api is None : <EOL> api = BigML ( storage = STORAGE ) <EOL> self . resource_id = get_association_id ( association ) <EOL> if self . resource_id is None : <EOL> raise Exception ( api . error_message ( association , <EOL> resource_type = '<STR_LIT>' , <EOL> method = '<STR_LIT>' ) ) <EOL> query_string = ONLY_MODEL <EOL> association = retrieve_resource ( api , self . resource_id , <EOL> query_string = query_string ) <EOL> else : <EOL> self . resource_id = get_association_id ( association ) <EOL> if '<STR_LIT:object>' in association and isinstance ( association [ '<STR_LIT:object>' ] , dict ) : <EOL> association = association [ '<STR_LIT:object>' ] <EOL> if '<STR_LIT>' in association and isinstance ( association [ '<STR_LIT>' ] , dict ) : <EOL> status = get_status ( association ) <EOL> if '<STR_LIT:code>' in status and status [ '<STR_LIT:code>' ] == FINISHED : <EOL> associations = association [ '<STR_LIT>' ] <EOL> fields = associations [ '<STR_LIT>' ] <EOL> ModelFields . __init__ ( self , fields ) <EOL> self . complement = associations . get ( '<STR_LIT>' , False ) <EOL> self . discretization = associations . get ( '<STR_LIT>' , { } ) <EOL> self . field_discretizations = associations . get ( <EOL> '<STR_LIT>' , { } ) <EOL> self . items = [ Item ( index , item , fields ) for index , item in <EOL> enumerate ( associations . get ( '<STR_LIT>' , [ ] ) ) ] <EOL> self . k = associations . get ( '<STR_LIT:k>' , <NUM_LIT:100> ) <EOL> self . max_lhs = associations . get ( '<STR_LIT>' , <NUM_LIT:4> ) <EOL> self . min_coverage = associations . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . min_leverage = associations . get ( '<STR_LIT>' , - <NUM_LIT:1> ) <EOL> self . min_strength = associations . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . min_support = associations . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . min_lift = associations . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . prune = associations . get ( '<STR_LIT>' , True ) <EOL> self . search_strategy = associations . get ( '<STR_LIT>' , <EOL> DEFAULT_SEARCH_STRATEGY ) <EOL> self . rules = [ AssociationRule ( rule ) for rule in <EOL> associations . get ( '<STR_LIT>' , [ ] ) ] <EOL> self . significance_level = associations . get ( <EOL> '<STR_LIT>' , <NUM_LIT> ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> association ) <EOL> def association_set ( self , input_data , <EOL> k = DEFAULT_K , score_by = None , by_name = True ) : <EOL> """<STR_LIT>""" <EOL> predictions = { } <EOL> if score_by and score_by not in SCORES : <EOL> raise ValueError ( "<STR_LIT>" % <EOL> "<STR_LIT:U+002CU+0020>" . join ( SCORES ) ) <EOL> input_data = self . filter_input_data ( input_data , by_name = by_name ) <EOL> items_indexes = [ item . index for item in <EOL> self . get_items ( input_map = input_data ) ] <EOL> if score_by is None : <EOL> score_by = self . search_strategy <EOL> for rule in self . rules : <EOL> field_type = self . fields [ self . items [ rule . rhs [ <NUM_LIT:0> ] ] . field_id ] [ '<STR_LIT>' ] <EOL> if field_type in NO_ITEMS and self . items [ rule . rhs [ <NUM_LIT:0> ] ] . field_id in input_data : <EOL> continue <EOL> if field_type not in NO_ITEMS and rule . rhs [ <NUM_LIT:0> ] in items_indexes : <EOL> continue <EOL> cosine = sum ( [ <NUM_LIT:1> for index in items_indexes if index in rule . lhs ] ) <EOL> if cosine > <NUM_LIT:0> : <EOL> cosine = cosine / float ( math . sqrt ( len ( items_indexes ) ) * math . sqrt ( len ( rule . lhs ) ) ) <EOL> rhs = tuple ( rule . rhs ) <EOL> if rhs not in predictions : <EOL> predictions [ rhs ] = { "<STR_LIT>" : <NUM_LIT:0> } <EOL> predictions [ rhs ] [ "<STR_LIT>" ] += cosine * getattr ( <EOL> rule , score_by ) <EOL> k = len ( predictions . keys ( ) ) if k is None else k <EOL> predictions = sorted ( predictions . items ( ) , <EOL> key = lambda x : x [ <NUM_LIT:1> ] [ "<STR_LIT>" ] , reverse = True ) [ : k ] <EOL> final_predictions = [ ] <EOL> for rhs , prediction in predictions : <EOL> prediction [ "<STR_LIT>" ] = self . items [ rhs [ <NUM_LIT:0> ] ] . to_json ( ) <EOL> final_predictions . append ( prediction ) <EOL> return final_predictions <EOL> def get_items ( self , field = None , <EOL> names = None , input_map = None , filter_function = None ) : <EOL> """<STR_LIT>""" <EOL> items = [ ] <EOL> if field : <EOL> if field in self . fields : <EOL> field_id = field <EOL> elif field in self . inverted_fields : <EOL> field_id = self . inverted_fields [ field ] <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" % field ) <EOL> def filter_function_set ( item ) : <EOL> """<STR_LIT>""" <EOL> if filter_function is None : <EOL> return True <EOL> return filter_function ( item ) <EOL> def field_filter ( item ) : <EOL> """<STR_LIT>""" <EOL> if field is None : <EOL> return True <EOL> return item . field_id == field_id <EOL> def names_filter ( item ) : <EOL> """<STR_LIT>""" <EOL> if names is None : <EOL> return True <EOL> return item . name in names <EOL> def input_map_filter ( item ) : <EOL> """<STR_LIT>""" <EOL> if input_map is None : <EOL> return True <EOL> value = input_map . get ( item . field_id ) <EOL> return item . matches ( value ) <EOL> for item in self . items : <EOL> if all ( [ field_filter ( item ) , names_filter ( item ) , <EOL> input_map_filter ( item ) , <EOL> filter_function_set ( item ) ] ) : <EOL> items . append ( item ) <EOL> return items <EOL> def get_rules ( self , min_leverage = None , min_strength = None , <EOL> min_support = None , min_p_value = None , item_list = None , <EOL> filter_function = None ) : <EOL> """<STR_LIT>""" <EOL> def leverage ( rule ) : <EOL> """<STR_LIT>""" <EOL> if min_leverage is None : <EOL> return True <EOL> return rule . leverage >= min_leverage <EOL> def strength ( rule ) : <EOL> """<STR_LIT>""" <EOL> if min_strength is None : <EOL> return True <EOL> return rule . strength >= min_strength <EOL> def support ( rule ) : <EOL> """<STR_LIT>""" <EOL> if min_support is None : <EOL> return True <EOL> return rule . support >= min_support <EOL> def p_value ( rule ) : <EOL> """<STR_LIT>""" <EOL> if min_p_value is None : <EOL> return True <EOL> return rule . p_value >= min_p_value <EOL> def filter_function_set ( rule ) : <EOL> """<STR_LIT>""" <EOL> if filter_function is None : <EOL> return True <EOL> return filter_function ( rule ) <EOL> def item_list_set ( rule ) : <EOL> """<STR_LIT>""" <EOL> if item_list is None : <EOL> return True <EOL> if isinstance ( item_list [ <NUM_LIT:0> ] , Item ) : <EOL> items = [ item . index for item in item_list ] <EOL> elif isinstance ( item_list [ <NUM_LIT:0> ] , basestring ) : <EOL> items = [ item . index for item <EOL> in self . get_items ( names = item_list ) ] <EOL> for item_index in rule . lhs : <EOL> if item_index in items : <EOL> return True <EOL> for item_index in rule . rhs : <EOL> if item_index in items : <EOL> return True <EOL> return False <EOL> rules = [ ] <EOL> for rule in self . rules : <EOL> if all ( [ leverage ( rule ) , strength ( rule ) , support ( rule ) , <EOL> p_value ( rule ) , item_list_set ( rule ) , <EOL> filter_function_set ( rule ) ] ) : <EOL> rules . append ( rule ) <EOL> return rules <EOL> def rules_csv ( self , file_name , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> rules = self . get_rules ( ** kwargs ) <EOL> rules = [ self . describe ( rule . to_csv ( ) ) for rule in rules ] <EOL> if file_name is None : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> with UnicodeWriter ( file_name ) as writer : <EOL> writer . writerow ( RULE_HEADERS ) <EOL> for rule in rules : <EOL> writer . writerow ( [ item if not isinstance ( item , basestring ) <EOL> else item . encode ( "<STR_LIT:utf-8>" ) <EOL> for item in rule ] ) <EOL> def describe ( self , rule_row ) : <EOL> """<STR_LIT>""" <EOL> for index in range ( <NUM_LIT:1> , <NUM_LIT:3> ) : <EOL> description = [ ] <EOL> for item_index in rule_row [ index ] : <EOL> item = self . items [ item_index ] <EOL> item_description = item . name if len ( self . fields . keys ( ) ) == <NUM_LIT:1> and not item . complement else item . describe ( ) <EOL> description . append ( item_description ) <EOL> description_str = "<STR_LIT>" . join ( description ) <EOL> rule_row [ index ] = description_str <EOL> return rule_row <EOL> def summarize ( self , out = sys . stdout , limit = <NUM_LIT:10> , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> rules = self . get_rules ( ** kwargs ) <EOL> out . write ( "<STR_LIT>" % len ( rules ) ) <EOL> for metric in ASSOCIATION_METRICS : <EOL> out . write ( "<STR_LIT>" % ( <EOL> limit , METRIC_LITERALS [ metric ] ) ) <EOL> top_rules = sorted ( rules , key = lambda x : getattr ( x , metric ) , <EOL> reverse = True ) [ <NUM_LIT:0> : limit * <NUM_LIT:2> ] <EOL> out_rules = [ ] <EOL> ref_rules = [ ] <EOL> counter = <NUM_LIT:0> <EOL> for rule in top_rules : <EOL> rule_row = self . describe ( rule . to_csv ( ) ) <EOL> metric_string = get_metric_string ( rule ) <EOL> operator = "<STR_LIT>" <EOL> rule_id_string = "<STR_LIT>" % rule . rule_id <EOL> for item in top_rules : <EOL> if rule . rhs == item . lhs and rule . lhs == item . rhs and metric_string == get_metric_string ( <EOL> item , reverse = True ) : <EOL> rule_id_string = "<STR_LIT>" % ( rule . rule_id , <EOL> item . rule_id ) <EOL> operator = "<STR_LIT>" <EOL> out_rule = "<STR_LIT>" % ( <EOL> rule_row [ <NUM_LIT:1> ] , operator , rule_row [ <NUM_LIT:2> ] , <EOL> metric_string ) <EOL> reverse_rule = "<STR_LIT>" % ( <EOL> rule_row [ <NUM_LIT:2> ] , operator , rule_row [ <NUM_LIT:1> ] , <EOL> metric_string ) <EOL> if operator == "<STR_LIT>" or reverse_rule not in ref_rules : <EOL> ref_rules . append ( out_rule ) <EOL> out_rule = "<STR_LIT>" % ( INDENT * <NUM_LIT:2> , <EOL> rule_id_string , out_rule ) <EOL> out_rules . append ( out_rule ) <EOL> counter += <NUM_LIT:1> <EOL> if counter > limit : <EOL> break <EOL> out . write ( "<STR_LIT:\n>" . join ( out_rules ) ) <EOL> out . write ( "<STR_LIT:\n>" ) </s>
