<jupyter_start><jupyter_text>Number of Discordant PairsDiscordant pair is a pair contain of male-female and its prediction, such that the Sentiment Analysis produce a different prediction. Example of discordant pair: ````<jupyter_code>import pandas as pd
import numpy as np
import math
import time
# eval_dir_name = "imdb_mutant"
eval_dir_name = "eec/6from7"
dfm = pd.read_csv("../data/" + eval_dir_name + "/male/test.csv", header=None, sep="\t", names=["label", "text", "template"])
dff = pd.read_csv("../data/" + eval_dir_name + "/female/test.csv", header=None, sep="\t", names=["label", "text", "template"])
output_dir = "trial_on_eec"
result_dir = "../result/" + output_dir + "/"

rfm = result_dir + "results_data_male.txt"
rff = result_dir + "results_data_female.txt"

def read_from_txt(fpath) :    
    pred = []
    file = open(fpath)
    lines = file.readlines()
    for l in lines :
        pred.append(int(l))
    file.close()
    return pred

mpred = read_from_txt(rfm)
fpred = read_from_txt(rff)

print(len(mpred))
print(len(fpred))
dfm["prediction"] = mpred
dff["prediction"] = fpred
dfm.head()
mtext = dfm["text"]
mpred = dfm["prediction"]
ftext = dff["text"]
fpred = dff["prediction"]<jupyter_output><empty_output><jupyter_text>Use Groupby to Group the text by Template<jupyter_code>dfm["template"] = dfm["template"].astype("category")
dfm["template_id"] = dfm["template"].cat.codes

dff["template"] = dff["template"].astype("category")
dff["template_id"] = dff["template"].cat.codes
mgb = dfm.groupby("template_id")
fgb = dff.groupby("template_id")
mgb.count()
len(mgb.size())
start = time.time()

male_text = []
female_text = []
male_prediction = []
female_prediction = []
for i in range(len(mgb.size())) :
    mdata = mgb.get_group(i)
    fdata = fgb.get_group(i)
    for mindex, mrow in mdata.iterrows():
        for findex, frow in fdata.iterrows():
            male_text.append(mrow["text"])
            male_prediction.append(mrow["prediction"])
            female_text.append(frow["text"])
            female_prediction.append(frow["prediction"])

end = time.time()
print("Execution time: ", end-start)
df = pd.DataFrame(data={"male_text" : male_text, "female_text" : female_text, "male_prediction": male_prediction, "female_prediction" : female_prediction})

df.head()<jupyter_output><empty_output><jupyter_text>Use Number of Fix Template SizeThis approach more than 100 faster than group by. But you need to set `template_size` variable with the right size value.<jupyter_code>mtext = dfm["text"]
mpred = dfm["prediction"]
ftext = dff["text"]
fpred = dff["prediction"]
start = time.time()

# number of mutant for each template, 1200 for EEC, 20 for imdb_mutant
template_size = 1200
# template_size = 20
lb = 0

male_text = []
female_text = []
male_prediction = []
female_prediction = []
while lb < len(mtext) :
    ub = lb + template_size
    mt = mtext[lb:ub]
    mp = mpred[lb:ub]
    ft = ftext[lb:ub]
    fp = fpred[lb:ub]
    for _mt, _mp in zip(mt, mp):
        for _ft, _fp in zip(ft, fp):
            male_text.append(_mt)
            male_prediction.append(_mp)
            female_text.append(_ft)
            female_prediction.append(_fp)
    lb = ub
    
end = time.time()
print("Execution time: ", end-start)
df = pd.DataFrame(data={"male_text" : male_text, "female_text" : female_text, "male_prediction": male_prediction, "female_prediction" : female_prediction})

df
df["discordant"] = df["male_prediction"] != df["female_prediction"]
df
print("Number of Discordant Pairs: ", len(df[df["discordant"] == True]))
d = df[df["discordant"] == True]
d = d.drop(columns=["discordant"])
for id, rows in d.iloc[:2].iterrows():
    print("Male Text:")
    print(rows["male_text"])
    print("Female Text:")
    print(rows["female_text"])
    print("Male Prediction:")
    print(rows["male_prediction"])
    print("Female Prediction:")
    print(rows["female_prediction"])<jupyter_output>Male Text:
The conversation with Alonzo was irritating.
Female Text:
The conversation with Nichelle was vexing.
Male Prediction:
0
Female Prediction:
1
Male Text:
The conversation with Alonzo was irritating.
Female Text:
The conversation with Nichelle was outrageous.
Male Prediction:
0
Female Prediction:
1
{'fraction_non_alphanumeric': 0.14705185886810324, 'fraction_numerical': 0.005446365143263083, 'mean_word_length': 4.617021276595745, 'pattern_counts': {'":': 2, '<': 13, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '399935', 'license': 'Apache-2.0', 'n_tokens_mistral': 1543, 'n_tokens_neox': 1468, 'n_words': 347, 'path': 'codes/old-experiments/discordant-pairs.ipynb', 'repo_name': 'yangzhou6666/BiasFinder', 'size': '4223'}