<s> import sys <EOL> import copy <EOL> sys . path . append ( '<STR_LIT:..>' ) <EOL> import argparse <EOL> import explainers <EOL> import parzen_windows <EOL> import numpy as np <EOL> import pickle <EOL> import sklearn <EOL> from load_datasets import * <EOL> from sklearn . metrics import accuracy_score <EOL> def get_tree_explanation ( tree , v ) : <EOL> t = tree . tree_ <EOL> nonzero = v . nonzero ( ) [ <NUM_LIT:1> ] <EOL> current = <NUM_LIT:0> <EOL> left_child = t . children_left [ current ] <EOL> exp = set ( ) <EOL> while left_child != sklearn . tree . _tree . TREE_LEAF : <EOL> left_child = t . children_left [ current ] <EOL> right_child = t . children_right [ current ] <EOL> f = t . feature [ current ] <EOL> if f in nonzero : <EOL> exp . add ( f ) <EOL> if v [ <NUM_LIT:0> , f ] < t . threshold [ current ] : <EOL> current = left_child <EOL> else : <EOL> current = right_child <EOL> return exp <EOL> class ExplanationEvaluator : <EOL> def __init__ ( self , classifier_names = None ) : <EOL> self . classifier_names = classifier_names <EOL> if not self . classifier_names : <EOL> self . classifier_names = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> self . classifiers = { } <EOL> def init_classifiers ( self , dataset ) : <EOL> self . classifiers [ dataset ] = { } <EOL> for classifier in self . classifier_names : <EOL> if classifier == '<STR_LIT>' : <EOL> try_cs = np . arange ( <NUM_LIT> , <NUM_LIT:0> , - <NUM_LIT> ) <EOL> for c in try_cs : <EOL> self . classifiers [ dataset ] [ '<STR_LIT>' ] = linear_model . LogisticRegression ( penalty = '<STR_LIT>' , fit_intercept = True , C = c ) <EOL> self . classifiers [ dataset ] [ '<STR_LIT>' ] . fit ( self . train_vectors [ dataset ] , self . train_labels [ dataset ] ) <EOL> lengths = [ len ( x . nonzero ( ) [ <NUM_LIT:0> ] ) for x in self . classifiers [ dataset ] [ '<STR_LIT>' ] . transform ( self . train_vectors [ dataset ] ) ] <EOL> if np . max ( lengths ) <= <NUM_LIT:10> : <EOL> break <EOL> if classifier == '<STR_LIT>' : <EOL> self . classifiers [ dataset ] [ '<STR_LIT>' ] = tree . DecisionTreeClassifier ( random_state = <NUM_LIT:1> ) <EOL> self . classifiers [ dataset ] [ '<STR_LIT>' ] . fit ( self . train_vectors [ dataset ] , self . train_labels [ dataset ] ) <EOL> lengths = [ len ( get_tree_explanation ( self . classifiers [ dataset ] [ '<STR_LIT>' ] , self . train_vectors [ dataset ] [ i ] ) ) for i in range ( self . train_vectors [ dataset ] . shape [ <NUM_LIT:0> ] ) ] <EOL> def load_datasets ( self , dataset_names ) : <EOL> self . train_data = { } <EOL> self . train_labels = { } <EOL> self . test_data = { } <EOL> self . test_labels = { } <EOL> for dataset in dataset_names : <EOL> self . train_data [ dataset ] , self . train_labels [ dataset ] , self . test_data [ dataset ] , self . test_labels [ dataset ] , _ = LoadDataset ( dataset ) <EOL> def vectorize_and_train ( self ) : <EOL> self . vectorizer = { } <EOL> self . train_vectors = { } <EOL> self . test_vectors = { } <EOL> self . inverse_vocabulary = { } <EOL> print '<STR_LIT>' , <EOL> for d in self . train_data : <EOL> self . vectorizer [ d ] = CountVectorizer ( lowercase = False , binary = True ) <EOL> self . train_vectors [ d ] = self . vectorizer [ d ] . fit_transform ( self . train_data [ d ] ) <EOL> self . test_vectors [ d ] = self . vectorizer [ d ] . transform ( self . test_data [ d ] ) <EOL> terms = np . array ( list ( self . vectorizer [ d ] . vocabulary_ . keys ( ) ) ) <EOL> indices = np . array ( list ( self . vectorizer [ d ] . vocabulary_ . values ( ) ) ) <EOL> self . inverse_vocabulary [ d ] = terms [ np . argsort ( indices ) ] <EOL> print '<STR_LIT>' <EOL> print '<STR_LIT>' <EOL> for d in self . train_data : <EOL> print d <EOL> self . init_classifiers ( d ) <EOL> print '<STR_LIT>' <EOL> print <EOL> def measure_explanation_hability ( self , explain_fn , max_examples = None ) : <EOL> """<STR_LIT>""" <EOL> budget = <NUM_LIT:10> <EOL> train_results = { } <EOL> test_results = { } <EOL> for d in self . train_data : <EOL> train_results [ d ] = { } <EOL> test_results [ d ] = { } <EOL> print '<STR_LIT>' , d <EOL> for c in self . classifiers [ d ] : <EOL> train_results [ d ] [ c ] = [ ] <EOL> test_results [ d ] [ c ] = [ ] <EOL> if c == '<STR_LIT>' : <EOL> c_features = self . classifiers [ d ] [ c ] . coef_ . nonzero ( ) [ <NUM_LIT:1> ] <EOL> print '<STR_LIT>' , c <EOL> for i in range ( len ( self . test_data [ d ] ) ) : <EOL> if c == '<STR_LIT>' : <EOL> true_features = set ( [ x for x in self . test_vectors [ d ] [ i ] . nonzero ( ) [ <NUM_LIT:1> ] if x in c_features ] ) <EOL> elif c == '<STR_LIT>' : <EOL> true_features = get_tree_explanation ( self . classifiers [ d ] [ c ] , self . test_vectors [ d ] [ i ] ) <EOL> if len ( true_features ) == <NUM_LIT:0> : <EOL> continue <EOL> to_get = budget <EOL> exp_features = set ( map ( lambda x : x [ <NUM_LIT:0> ] , <EOL> explain_fn ( self . test_vectors [ d ] [ i ] , self . test_labels [ d ] [ i ] , self . classifiers [ d ] [ c ] , to_get , d ) ) ) <EOL> test_results [ d ] [ c ] . append ( float ( len ( true_features . intersection ( exp_features ) ) ) / len ( true_features ) ) <EOL> if max_examples and i >= max_examples : <EOL> break <EOL> return train_results , test_results <EOL> def main ( ) : <EOL> parser = argparse . ArgumentParser ( description = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , type = str , required = True , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , type = str , required = True , help = '<STR_LIT>' ) <EOL> parser . add_argument ( '<STR_LIT>' , '<STR_LIT>' , type = str , required = True , help = '<STR_LIT>' ) <EOL> args = parser . parse_args ( ) <EOL> dataset = args . dataset <EOL> algorithm = args . algorithm <EOL> evaluator = ExplanationEvaluator ( classifier_names = [ algorithm ] ) <EOL> evaluator . load_datasets ( [ dataset ] ) <EOL> evaluator . vectorize_and_train ( ) <EOL> explain_fn = None <EOL> if args . explainer == '<STR_LIT>' : <EOL> rho = <NUM_LIT> <EOL> kernel = lambda d : np . sqrt ( np . exp ( - ( d ** <NUM_LIT:2> ) / rho ** <NUM_LIT:2> ) ) <EOL> explainer = explainers . GeneralizedLocalExplainer ( kernel , explainers . data_labels_distances_mapping_text , num_samples = <NUM_LIT> , return_mean = False , verbose = False , return_mapped = True ) <EOL> explain_fn = explainer . explain_instance <EOL> elif args . explainer == '<STR_LIT>' : <EOL> sigmas = { '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT:0.5> , <EOL> '<STR_LIT>' : <NUM_LIT:1> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT:1> } , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT> , '<STR_LIT>' : <NUM_LIT> } } <EOL> explainer = parzen_windows . ParzenWindowClassifier ( ) <EOL> cv_preds = sklearn . cross_validation . cross_val_predict ( evaluator . classifiers [ dataset ] [ algorithm ] , evaluator . train_vectors [ dataset ] , evaluator . train_labels [ dataset ] ) <EOL> explainer . fit ( evaluator . train_vectors [ dataset ] , cv_preds ) <EOL> explainer . sigma = sigmas [ dataset ] [ algorithm ] <EOL> explain_fn = explainer . explain_instance <EOL> elif args . explainer == '<STR_LIT>' : <EOL> explain_fn = explainers . explain_greedy <EOL> elif args . explainer == '<STR_LIT>' : <EOL> explainer = explainers . RandomExplainer ( ) <EOL> explain_fn = explainer . explain_instance <EOL> train_results , test_results = evaluator . measure_explanation_hability ( explain_fn ) <EOL> print '<STR_LIT>' , np . mean ( test_results [ dataset ] [ algorithm ] ) <EOL> out = { '<STR_LIT:train>' : train_results [ dataset ] [ algorithm ] , '<STR_LIT:test>' : test_results [ dataset ] [ algorithm ] } <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> main ( ) </s>
