<s> """<STR_LIT>""" <EOL> import logging <EOL> import math <EOL> import re <EOL> from bigml . api import FINISHED <EOL> from bigml . api import ( BigML , get_logistic_regression_id , get_status ) <EOL> from bigml . util import cast <EOL> from bigml . basemodel import retrieve_resource , extract_objective <EOL> from bigml . basemodel import ONLY_MODEL <EOL> from bigml . model import STORAGE <EOL> from bigml . predicate import TM_TOKENS , TM_FULL_TERM <EOL> from bigml . modelfields import ModelFields <EOL> from bigml . cluster import parse_terms , parse_items <EOL> LOGGER = logging . getLogger ( '<STR_LIT>' ) <EOL> EXPANSION_ATTRIBUTES = { "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:text>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" } <EOL> def get_unique_terms ( terms , term_forms , tag_cloud ) : <EOL> """<STR_LIT>""" <EOL> extend_forms = { } <EOL> for term , forms in term_forms . items ( ) : <EOL> for form in forms : <EOL> extend_forms [ form ] = term <EOL> extend_forms [ term ] = term <EOL> terms_set = { } <EOL> for term in terms : <EOL> if term in tag_cloud : <EOL> if term not in terms_set : <EOL> terms_set [ term ] = <NUM_LIT:0> <EOL> terms_set [ term ] += <NUM_LIT:1> <EOL> elif term in extend_forms : <EOL> term = extend_forms [ term ] <EOL> if term not in terms_set : <EOL> terms_set [ term ] = <NUM_LIT:0> <EOL> terms_set [ term ] += <NUM_LIT:1> <EOL> return terms_set . items ( ) <EOL> class LogisticRegression ( ModelFields ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , logistic_regression , api = None ) : <EOL> self . resource_id = None <EOL> self . term_forms = { } <EOL> self . tag_clouds = { } <EOL> self . term_analysis = { } <EOL> self . items = { } <EOL> self . item_analysis = { } <EOL> self . categories = { } <EOL> self . coefficients = { } <EOL> self . data_field_types = { } <EOL> self . numeric_fields = { } <EOL> self . bias = None <EOL> self . missing_numerics = None <EOL> self . c = None <EOL> self . eps = None <EOL> self . lr_normalize = None <EOL> self . regularization = None <EOL> if not ( isinstance ( logistic_regression , dict ) <EOL> and '<STR_LIT>' in logistic_regression and <EOL> logistic_regression [ '<STR_LIT>' ] is not None ) : <EOL> if api is None : <EOL> api = BigML ( storage = STORAGE ) <EOL> self . resource_id = get_logistic_regression_id ( logistic_regression ) <EOL> if self . resource_id is None : <EOL> raise Exception ( <EOL> api . error_message ( logistic_regression , <EOL> resource_type = '<STR_LIT>' , <EOL> method = '<STR_LIT>' ) ) <EOL> query_string = ONLY_MODEL <EOL> logistic_regression = retrieve_resource ( <EOL> api , self . resource_id , query_string = query_string ) <EOL> else : <EOL> self . resource_id = get_logistic_regression_id ( logistic_regression ) <EOL> if '<STR_LIT:object>' in logistic_regression and isinstance ( logistic_regression [ '<STR_LIT:object>' ] , dict ) : <EOL> logistic_regression = logistic_regression [ '<STR_LIT:object>' ] <EOL> try : <EOL> self . dataset_field_types = logistic_regression . get ( <EOL> "<STR_LIT>" , { } ) <EOL> objective_field = logistic_regression [ '<STR_LIT>' ] if logistic_regression [ '<STR_LIT>' ] else logistic_regression [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> if '<STR_LIT>' in logistic_regression and isinstance ( logistic_regression [ '<STR_LIT>' ] , dict ) : <EOL> status = get_status ( logistic_regression ) <EOL> if '<STR_LIT:code>' in status and status [ '<STR_LIT:code>' ] == FINISHED : <EOL> logistic_regression_info = logistic_regression [ '<STR_LIT>' ] <EOL> fields = logistic_regression_info . get ( '<STR_LIT>' , { } ) <EOL> self . coefficients . update ( logistic_regression_info . get ( '<STR_LIT>' , [ ] ) ) <EOL> self . bias = logistic_regression_info . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . c = logistic_regression_info . get ( '<STR_LIT:c>' ) <EOL> self . eps = logistic_regression_info . get ( '<STR_LIT>' ) <EOL> self . lr_normalize = logistic_regression_info . get ( '<STR_LIT>' ) <EOL> self . regularization = logistic_regression_info . get ( '<STR_LIT>' ) <EOL> self . missing_numerics = logistic_regression_info . get ( '<STR_LIT>' , False ) <EOL> objective_id = extract_objective ( objective_field ) <EOL> for field_id , field in fields . items ( ) : <EOL> if field [ '<STR_LIT>' ] == '<STR_LIT:text>' : <EOL> self . term_forms [ field_id ] = { } <EOL> self . term_forms [ field_id ] . update ( <EOL> field [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> self . tag_clouds [ field_id ] = [ ] <EOL> self . tag_clouds [ field_id ] = [ tag for [ tag , _ ] in field [ <EOL> '<STR_LIT>' ] [ '<STR_LIT>' ] ] <EOL> self . term_analysis [ field_id ] = { } <EOL> self . term_analysis [ field_id ] . update ( <EOL> field [ '<STR_LIT>' ] ) <EOL> if field [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> self . items [ field_id ] = [ ] <EOL> self . items [ field_id ] = [ item for item , _ in field [ '<STR_LIT>' ] [ '<STR_LIT>' ] ] <EOL> self . item_analysis [ field_id ] = { } <EOL> self . item_analysis [ field_id ] . update ( <EOL> field [ '<STR_LIT>' ] ) <EOL> if field [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> self . categories [ field_id ] = [ category for [ category , _ ] in field [ '<STR_LIT>' ] [ '<STR_LIT>' ] ] <EOL> if self . missing_numerics and field [ '<STR_LIT>' ] == '<STR_LIT>' : <EOL> self . numeric_fields [ field_id ] = True <EOL> ModelFields . __init__ ( <EOL> self , fields , <EOL> objective_id = objective_id ) <EOL> self . map_coefficients ( ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> else : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % <EOL> logistic_regression ) <EOL> def predict ( self , input_data , by_name = True ) : <EOL> """<STR_LIT>""" <EOL> input_data = self . filter_input_data ( input_data , by_name = by_name ) <EOL> if not self . missing_numerics : <EOL> for field_id , field in self . fields . items ( ) : <EOL> if ( not field [ '<STR_LIT>' ] in OPTIONAL_FIELDS and <EOL> not field_id in input_data ) : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> cast ( input_data , self . fields ) <EOL> unique_terms = self . get_unique_terms ( input_data ) <EOL> probabilities = { } <EOL> total = <NUM_LIT:0> <EOL> for category in self . coefficients . keys ( ) : <EOL> coefficients = self . coefficients [ category ] <EOL> probabilities [ category ] = self . category_probability ( <EOL> input_data , unique_terms , coefficients ) <EOL> total += probabilities [ category ] <EOL> for category in probabilities . keys ( ) : <EOL> probabilities [ category ] /= total <EOL> predictions = sorted ( probabilities . items ( ) , <EOL> key = lambda x : x [ <NUM_LIT:1> ] , reverse = True ) <EOL> prediction , probability = predictions [ <NUM_LIT:0> ] <EOL> return { <EOL> "<STR_LIT>" : prediction , <EOL> "<STR_LIT>" : probability , <EOL> "<STR_LIT>" : [ { "<STR_LIT>" : category , "<STR_LIT>" : probability } <EOL> for category , probability in predictions ] } <EOL> def category_probability ( self , input_data , unique_terms , coefficients ) : <EOL> """<STR_LIT>""" <EOL> probability = <NUM_LIT:0> <EOL> for field_id in input_data : <EOL> shift = self . fields [ field_id ] [ '<STR_LIT>' ] <EOL> probability += coefficients [ shift ] * input_data [ field_id ] <EOL> for field_id in unique_terms : <EOL> shift = self . fields [ field_id ] [ '<STR_LIT>' ] <EOL> for term , occurrences in unique_terms [ field_id ] : <EOL> try : <EOL> if field_id in self . tag_clouds : <EOL> index = self . tag_clouds [ field_id ] . index ( term ) <EOL> elif field_id in self . items : <EOL> index = self . items [ field_id ] . index ( term ) <EOL> elif field_id in self . categories : <EOL> index = self . categories [ field_id ] . index ( term ) <EOL> probability += coefficients [ shift + index ] * occurrences <EOL> except ValueError : <EOL> pass <EOL> for field_id in self . numeric_fields : <EOL> if field_id not in input_data : <EOL> shift = self . fields [ field_id ] [ '<STR_LIT>' ] + <NUM_LIT:1> <EOL> probability += coefficients [ shift ] <EOL> for field_id in self . tag_clouds : <EOL> shift = self . fields [ field_id ] [ '<STR_LIT>' ] <EOL> if field_id not in unique_terms or not unique_terms [ field_id ] : <EOL> probability += coefficients [ shift + len ( self . tag_clouds [ field_id ] ) ] <EOL> for field_id in self . items : <EOL> shift = self . fields [ field_id ] [ '<STR_LIT>' ] <EOL> if field_id not in unique_terms or not unique_terms [ field_id ] : <EOL> probability += coefficients [ shift + len ( self . items [ field_id ] ) ] <EOL> for field_id in self . categories : <EOL> if field_id != self . objective_id and field_id not in unique_terms : <EOL> shift = self . fields [ field_id ] [ '<STR_LIT>' ] <EOL> probability += coefficients [ shift + len ( self . categories [ field_id ] ) ] <EOL> probability += coefficients [ - <NUM_LIT:1> ] <EOL> probability = <NUM_LIT:1> / ( <NUM_LIT:1> + math . exp ( - probability ) ) <EOL> return probability <EOL> def get_unique_terms ( self , input_data ) : <EOL> """<STR_LIT>""" <EOL> unique_terms = { } <EOL> for field_id in self . term_forms : <EOL> if field_id in input_data : <EOL> input_data_field = input_data . get ( field_id , '<STR_LIT>' ) <EOL> if isinstance ( input_data_field , basestring ) : <EOL> case_sensitive = self . term_analysis [ field_id ] . get ( <EOL> '<STR_LIT>' , True ) <EOL> token_mode = self . term_analysis [ field_id ] . get ( <EOL> '<STR_LIT>' , '<STR_LIT:all>' ) <EOL> if token_mode != TM_FULL_TERM : <EOL> terms = parse_terms ( input_data_field , <EOL> case_sensitive = case_sensitive ) <EOL> else : <EOL> terms = [ ] <EOL> if token_mode != TM_TOKENS : <EOL> terms . append ( <EOL> input_data_field if case_sensitive <EOL> else input_data_field . lower ( ) ) <EOL> unique_terms [ field_id ] = get_unique_terms ( <EOL> terms , self . term_forms [ field_id ] , <EOL> self . tag_clouds . get ( field_id , [ ] ) ) <EOL> else : <EOL> unique_terms [ field_id ] = [ ( input_data_field , <NUM_LIT:1> ) ] <EOL> del input_data [ field_id ] <EOL> for field_id in self . item_analysis : <EOL> if field_id in input_data : <EOL> input_data_field = input_data . get ( field_id , '<STR_LIT>' ) <EOL> if isinstance ( input_data_field , basestring ) : <EOL> separator = self . item_analysis [ field_id ] . get ( <EOL> '<STR_LIT>' , '<STR_LIT:U+0020>' ) <EOL> regexp = self . item_analysis [ field_id ] . get ( <EOL> '<STR_LIT>' ) <EOL> if regexp is None : <EOL> regexp = ur '<STR_LIT:%s>' % re . escape ( separator ) <EOL> terms = parse_items ( input_data_field , regexp ) <EOL> unique_terms [ field_id ] = get_unique_terms ( <EOL> terms , { } , <EOL> self . items . get ( field_id , [ ] ) ) <EOL> else : <EOL> unique_terms [ field_id ] = [ ( input_data_field , <NUM_LIT:1> ) ] <EOL> del input_data [ field_id ] <EOL> for field_id in self . categories : <EOL> if field_id in input_data : <EOL> input_data_field = input_data . get ( field_id , '<STR_LIT>' ) <EOL> unique_terms [ field_id ] = [ ( input_data_field , <NUM_LIT:1> ) ] <EOL> del input_data [ field_id ] <EOL> return unique_terms <EOL> def map_coefficients ( self ) : <EOL> """<STR_LIT>""" <EOL> field_ids = [ field_id for field_id , _ in <EOL> sorted ( self . fields . items ( ) , <EOL> key = lambda x : x [ <NUM_LIT:1> ] . get ( "<STR_LIT>" ) ) <EOL> if field_id != self . objective_id ] <EOL> shift = <NUM_LIT:0> <EOL> for field_id in field_ids : <EOL> optype = self . fields [ field_id ] [ '<STR_LIT>' ] <EOL> if optype in EXPANSION_ATTRIBUTES . keys ( ) : <EOL> length = len ( self . fields [ field_id ] [ '<STR_LIT>' ] [ EXPANSION_ATTRIBUTES [ optype ] ] ) <EOL> length += <NUM_LIT:1> <EOL> else : <EOL> length = <NUM_LIT:2> if self . missing_numerics else <NUM_LIT:1> <EOL> self . fields [ field_id ] [ '<STR_LIT>' ] = shift <EOL> shift += length </s>
