In [1]:
%load_ext autoreload
%autoreload 2
%matplotlib inline
%load_ext autotime
from baselines import get_annotator_ensemble_baselines_parallel,get_annotator_ensemble_baseline, get_model_baselines_parallel, get_model_baseline
from baselines import multi_class_roc_auc, multi_class_spearman
from baselines import empirical_dist
from ngram import load_comments_and_labels,assemble_data
from pprint import pprint
import pandas as pd
import numpy as np
from serialization import load_pipeline
import os
In [2]:
iters = 25
K = 20
F = int(K/2)
In [3]:
annotations = pd.read_csv('../../data/annotations/split/baseline/annotations.tsv', sep='\t')
annotations.index = annotations.rev_id
comments = annotations.drop_duplicates('rev_id')['clean_diff']
In [4]:
metrics = {'ROC': multi_class_roc_auc, 'spearman':multi_class_spearman}
tasks = ['attack']
#tasks = ['attack' 'recipient', 'aggression']
annotations_subsets = {'all': annotations}
#annotations_subsets = {
# 'all': d,
# 'blocked' : annotations.query("sample=='blocked'"),
# 'random' : annotations.query("sample=='random'")
#}
In [33]:
pairs = list(zip(range(1, F+1), range(1, F+1))) + list(zip(range(1, F+1), [F]*F))
In [34]:
dfs = []
for task in tasks:
for annotations_subset_name, annotations_subset in annotations_subsets.items():
task_annotations = annotations_subset[task]
for metric_name, metric in metrics.items():
args = [[task_annotations, K, empirical_dist, metric, n_t, n_p] for n_t, n_p in pairs] * iters
result = get_annotator_ensemble_baselines_parallel(args)
result['metric'] = metric_name
result['task'] = task
dfs.append(result)
ensemble_baseline_results = pd.concat(dfs)
ensemble_baseline_results['score'] = ensemble_baseline_results['score'] * 100
ensemble_baseline_results = ensemble_baseline_results.groupby(['metric', 'n_p', 'n_t', 'task'])['score'].agg({"mean": np.mean, 'std': np.std, 'count': 'count'})
pd.DataFrame(ensemble_baseline_results).to_csv('baselines1.csv')
In [35]:
ensemble_baseline_results
Out[35]:
In [5]:
dfs = []
cv_path = '../../models/attack'
model_type = 'linear_char_ed'
model_name = 'linear_char_ed_train'
for task in tasks:
model = load_pipeline(os.path.join(cv_path,model_type), model_name)
y_baseline_pred = model.predict_proba(comments)
y_baseline_pred = pd.DataFrame(y_baseline_pred, index = comments.index)
for annotations_subset_name, annotations_subset in annotations_subsets.items():
task_annotations = annotations_subset[task]
for metric_name, metric in metrics.items():
args = [[y_baseline_pred, task_annotations, K, empirical_dist, metric, F]] * iters
result = get_model_baselines_parallel(args)
result['metric'] = metric_name
result['task'] = task
dfs.append(result)
model_baseline_results = pd.concat(dfs)
model_baseline_results['score'] = model_baseline_results['score'] * 100
model_baseline_results = model_baseline_results.groupby(['metric', 'n_t','task' ])['score'].agg({"mean": np.mean, 'std': np.std, 'count': 'count'})
pd.DataFrame(model_baseline_results).to_csv('baselines2.csv')
In [6]:
model_baseline_results
Out[6]:
In [ ]: