def reduce(self, result): if self.select_regexp: inputs = [key3 for key3 in result if re.search(self.select_regexp, str(key3))] else: inputs = result.keys() if len(inputs) != 2: raise KeyError("Need to find exactly two results to compute a score." " Found %i: %s" % (len(inputs), inputs)) key_true = [k for k in inputs if k.find(conf.TRUE) != -1][0] key_pred = [k for k in inputs if k.find(conf.PREDICTION) != -1][0] y_true = result[key_true] y_pred = result[key_pred] try: # If list of arrays (CV, LOO, etc.) concatenate them y_true = np.concatenate(y_true) y_pred = np.concatenate(y_pred) except ValueError: pass out = Result(key=result["key"]) p, r, f1, s = precision_recall_fscore_support(y_true, y_pred, average=None) key, _ = key_pop(key_pred, -1) out[key_push(key, conf.SCORE_PRECISION)] = p out[key_push(key, conf.SCORE_RECALL)] = r out[key_push(key, conf.SCORE_RECALL_MEAN)] = r.mean() out[key_push(key, conf.SCORE_F1)] = f1 out[key_push(key, conf.SCORE_ACCURACY)] = accuracy_score(y_true, y_pred) if self.keep: out.update(result) return out
def reduce(self, result): if self.select_regexp: inputs = [key3 for key3 in result if re.search(self.select_regexp, str(key3))] else: inputs = result.keys() if len(inputs) != 2: raise KeyError("Need to find exactly two results to compute a " "score. Found %i: %s" % (len(inputs), inputs)) key_true = [k for k in inputs if k.find(conf.TRUE) != -1][0] key_pred = [k for k in inputs if k.find(conf.PREDICTION) != -1][0] y_true = result[key_true] y_pred = result[key_pred] try: # If list of arrays (CV, LOO, etc.) concatenate them y_true = np.concatenate(y_true) y_pred = np.concatenate(y_pred) except ValueError: pass out = Result(key=result["key"]) p, r, f1, s = precision_recall_fscore_support(y_true, y_pred, average=None) # Compute p-value of recall for each class def recall_test(recall, n_trials, apriori_p): n_success = recall * n_trials pval = binom_test(n_success, n=n_trials, p=apriori_p) if recall > apriori_p: return (pval / 2) else: return 1 - (pval / 2) n_classes = len(s) # Number of classes n_obs = len(y_true) prior_p = s.astype(np.float)/s.sum() # A priori probability of each class r_pvalues = np.zeros_like(r) for class_index in range(n_classes): n_trials = s[class_index] #print "Class {class_index}: {n_success} success on {n_trials} trials".format(n_success=n_success, n_trials=n_trials, class_index=class_index) r_pvalues[class_index] = recall_test(r[class_index], n_trials, prior_p[class_index]) # Compute p-value of mean recall mean_r = r.mean() mean_r_pvalue = binom_test(int(mean_r * n_obs), n=n_obs, p=.5) key, _ = key_pop(key_pred, -1) out[key_push(key, conf.SCORE_PRECISION)] = p out[key_push(key, conf.SCORE_RECALL)] = r out[key_push(key, conf.SCORE_RECALL_PVALUES)] = r_pvalues out[key_push(key, conf.SCORE_RECALL_MEAN)] = mean_r out[key_push(key, conf.SCORE_RECALL_MEAN_PVALUE)] = mean_r_pvalue out[key_push(key, conf.SCORE_F1)] = f1 out[key_push(key, conf.SCORE_ACCURACY)] = accuracy_score(y_true, y_pred) if self.keep: out.update(result) return out
def reduce(self, result): if self.select_regexp: select_keys = [key for key in result if re.search(self.select_regexp, str(key))] #if re.search(self.select_regexp) != -1] else: select_keys = result.keys() out = Result(key=result.key()) for key in select_keys: out[key] = result[key][0] randm_res = np.vstack(result[key][1:]) count = np.sum(randm_res > result[key][0], axis=0).astype("float") pval = count / (randm_res.shape[0]) out[key_push(key, "pval")] = pval if self.keep: out.update(result) return out