Files
tomosco/util_bert.py
2022-05-15 13:29:55 +02:00

1024 lines
53 KiB
Python

import pandas as pd
import numpy as np
from bertopic import BERTopic
from collections import Counter
from tqdm.notebook import tqdm as progress
from sklearn.metrics import classification_report
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score
class BertUtilClass:
TOPIC_EN_NR = 0
TOPIC_DE_NR = 0
TOPIC_MIX_NR = 0
ASSIGN_PARAM = 0
THRESHOLD_PARAM = 0
MIN_TOPIC_SIZE = 0
topics_vector = []
probabilites_vector = []
KEEP_DUPLICATES = False
MODELS = {"en":{"model":None, "topic_df": None, "df_test": None},
"de":{"model":None, "topic_df": None, "df_test": None},
"mix":{"model":None, "topic_df": None, "df_test": None}}
LABELS = {"y_test_en": None, "y_pred_en": None,
"y_test_de": None, "y_pred_de": None,
"y_test_mix": None, "y_pred_mix": None}
COLLABS = {"en_self": None, "en_test": None,
"de_self": None, "de_test": None,
"mix_self": None, "mix_test": None}
MODEL_PROPs = {"en_model":None, "de_model":None,"mix_model":None,}
CM = {"en_test": None, "en_actual": None,
"de_test": None, "de_actual": None,
"mix_test": None, "mix_actual": None}
df = None
projects_df = None
original_df = None
def __init__(self, topics, df, min_topic_size, keep_flag, samples, threshold, original_df, projects_df):
self.TOPIC_EN_NR = topics[0]
self.TOPIC_DE_NR = topics[1]
self.TOPIC_MIX_NR = topics[2]
self.MIN_TOPIC_SIZE = min_topic_size
self.KEEP_DUPLICATES = keep_flag
self.df = df
self.original_df = original_df
self.projects_df = projects_df
self.ASSIGN_PARAM = samples
self.THRESHOLD_PARAM = threshold
@staticmethod
def compute_BERT_model(min_topic_size, topic_nr, df):
corpus = df["title_piped"].values.tolist()
topic_model = BERTopic(verbose=False, n_gram_range=(1, 1) ,embedding_model="all-MiniLM-L6-v2", min_topic_size=min_topic_size , nr_topics=topic_nr)
topics_vector, probabilites_vector = topic_model.fit_transform(corpus)
return topics_vector, probabilites_vector, topic_model
@staticmethod
def compute_BERT_model_multilin(min_topic_size, topic_nr, df):
corpus = df["title_piped"].values.tolist()
topic_model = BERTopic(verbose=False, n_gram_range=(1, 1) ,language="multilingual" , embedding_model="paraphrase-multilingual-MiniLM-L12-v2", min_topic_size=min_topic_size , nr_topics=topic_nr)
topics_vector, probabilites_vector = topic_model.fit_transform(corpus)
return topics_vector, probabilites_vector, topic_model
@staticmethod
def assign_highest_n_contribution(origin_df, model, highest_n):
topics_df = pd.DataFrame()
for index, row in progress(origin_df.iterrows(), total = origin_df.shape[0]):
title = row["title_piped"]
topics, similarities = model.find_topics(title, top_n=highest_n)
for idx,tupel in enumerate(zip(topics, similarities)):
topic_num, similarity = tupel
topic_keywords = ", ".join([word[0] for word in model.get_topic(topic_num)])
topics_df = topics_df.append({"Dominant_topic": int(topic_num),
"Topic_contribution": round(similarity,4),
"Topic_keywords": topic_keywords,
"title_piped": row["title_piped"],
"_id": row["_id"],
"_source.year": row["_source.year"],
"_source.author": row["_source.author"],
"_source.title": row["_source.title"],
"_source.research_area": row["_source.research_area"],
"_source.institute": row["_source.institute"],
"_source.group": row["_source.group"],
"name": row["lsf_name"],
"group": row["group"],
"department_code": row["department_code"],
"institute":row["institute"],
"institute_code":row["institute_code"],
"lang": row["lang"]}, ignore_index=True)
topics_df.reset_index(drop=True, inplace=True)
return topics_df
@staticmethod
def predict_highest_n_classes_projects(projects, merged_df, model, highest_n):
y_pred = []
for index, row in progress(projects.iterrows(), total = projects.shape[0]):
if 'description' in projects.columns:
project = row["description"]
else:
#project = row["en_description_piped"]
project = row["en_description"]
topics, similarities = model.find_topics(project, top_n=highest_n)
if len(topics) > 0 :
institute_temp = None
topic_found = False
for idx, tupel in enumerate(zip(topics, similarities)):
topic_num, similarity = tupel
institute_series = merged_df[merged_df["Dominant_topic"] == topic_num].groupby("institute_code").size().sort_values(ascending=False).head(highest_n)
institutes_list = institute_series.reset_index().values.tolist()
institutes_predicted = [institute [0] for institute in institutes_list]
institute_temp = institutes_predicted[0]
institute_origin = row["institute_code"]
if not topic_found:
if institute_origin in institutes_predicted:
y_pred.append(institute_origin)
topic_found = True
if not topic_found:
y_pred.append(institute_temp)
y_test = projects["institute_code"].tolist()
return y_test, y_pred
@staticmethod
def predict_highest_n_classes(origin_df, merged_df, model, highest_n):
y_pred = []
for index, row in progress(origin_df.iterrows(), total = origin_df.shape[0]):
title = row["title_piped"]
topics, similarities = model.find_topics(title, top_n=highest_n)
if len(topics) > 0 :
institute_temp = None
topic_found = False
for idx,tupel in enumerate(zip(topics, similarities)):
topic_num, similarity = tupel
institute_series = merged_df[merged_df["Dominant_topic"] == topic_num].groupby("institute_code").size().sort_values(ascending=False).head(highest_n)
institutes_list = institute_series.reset_index().values.tolist()
institutes_predicted = [institute [0] for institute in institutes_list]
institute_temp = institutes_predicted[0]
institute_origin = row["institute_code"]
if not topic_found:
if institute_origin in institutes_predicted:
y_pred.append(institute_origin)
topic_found = True
if not topic_found:
y_pred.append(institute_temp)
y_test = origin_df["institute_code"].tolist()
return y_test, y_pred
def get_collabs_dataframe(self):
print("-----------initializing testing (test/test set) sequence--------")
df_mix = self.df
keep_duplicates = self.KEEP_DUPLICATES
df_mix = df_mix[df_mix["title_piped"].notna()]
df_en = df_mix[(df_mix["lang"]=="en")]
df_de = df_mix[(df_mix["lang"]=="de")]
if not keep_duplicates:
df_mix = df_mix.drop_duplicates(subset=["_id"])
df_en = df_en.drop_duplicates(subset=["_id"])
df_de = df_de.drop_duplicates(subset=["_id"])
df_en = df_en.sample(frac=1, random_state=42).reset_index(drop=True)
df_de = df_de.sample(frac=1, random_state=42).reset_index(drop=True)
df_mix = df_mix.sample(frac=1, random_state=42).reset_index(drop=True)
print("-----------splitting datasets in test/trainsets--------")
# df_mix_train, df_mix_test = self.generate_test_trainset(df_mix)
# df_en_train, df_en_test = self.generate_test_trainset(df_en)
# df_de_train, df_de_test = self.generate_test_trainset(df_de)
topic_en_nr = self.TOPIC_EN_NR
topic_de_nr = self.TOPIC_DE_NR
topic_mix_nr = self.TOPIC_MIX_NR
threshold = self.THRESHOLD_PARAM
institutes = self.df["institute_code"].unique().tolist()
institutes.sort()
print("-----------building BERT models from training corpus(s)--------")
topics_en, probs_en, model_en = self.compute_BERT_model(self.MIN_TOPIC_SIZE, topic_en_nr, df_en)
topics_de, probs_de, model_de = self.compute_BERT_model_multilin(self.MIN_TOPIC_SIZE, topic_de_nr, df_de)
topics_mix, probs_mix, model_mix = self.compute_BERT_model_multilin(self.MIN_TOPIC_SIZE, topic_mix_nr, df_mix)
print("-----------assigning top n topics to every record and normalizing--------")
sample_df = self.assign_highest_n_contribution(df_en, model_en, 10)
sample_de_df = self.assign_highest_n_contribution(df_de, model_de, 10)
sample_mix_df = self.assign_highest_n_contribution(df_mix, model_mix, 10)
print("-----------generating collabs--------")
self.COLLABS["en_test"] = self.generate_collabs(df_en, sample_df, model_en, threshold)
self.COLLABS["de_test"] = self.generate_collabs(df_de, sample_de_df, model_de, threshold)
self.COLLABS["mix_test"] = self.generate_collabs(df_mix, sample_mix_df, model_mix, threshold)
print("-----------generating CM dataframes--------")
self.CM["en_test"],self.CM["en_actual"] = self.generate_confusion_matrix(self.COLLABS["en_test"],self.original_df, institutes)
self.CM["de_test"],self.CM["de_actual"] = self.generate_confusion_matrix(self.COLLABS["de_test"],self.original_df, institutes)
self.CM["mix_test"],self.CM["mix_actual"] = self.generate_confusion_matrix(self.COLLABS["mix_test"],self.original_df, institutes)
@staticmethod
def generate_collabs_projects(bert_instance, n, project_corpus):
df = bert_instance["df_test"]
topics_df = bert_instance["topic_df"]
model = bert_instance["model"]
project_topics = model.find_topics(project_corpus[0], top_n=10)
project_topics = [tp[0] for tp in project_topics[:10]]
collabs = []
collab_df = pd.DataFrame()
df_pg = topics_df[topics_df["Dominant_topic"].isin(project_topics)]
for index, row in progress(df_pg.iterrows(), total = df_pg.shape[0]):
title = row["title_piped"]
topics, similarities = model.find_topics(title, top_n=n)
if len(topics) > 0 :
institute_temp = None
for idx,tupel in enumerate(zip(topics, similarities)):
topic_num, similarity = tupel
institute_series = topics_df[topics_df["Dominant_topic"] == int(topic_num)].groupby("institute_code").size().sort_values(ascending=False).head(n)
institutes_list = institute_series.reset_index().values.tolist()
institutes_predicted = [institute [0] for institute in institutes_list]
institute_origin = row["institute_code"]
for inst in institutes_predicted:
if not inst == institute_origin:
institute_tuple = (institute_origin, inst)
collabs.append(institute_tuple)
list_tuples = [tuple(sorted(i)) for i in collabs]
tuples = Counter(list_tuples)
count_list_tuples = [list(i) for i in tuples.items()]
for tup in count_list_tuples:
source_target = tup[0]
weight = tup[1]
collab_df = collab_df.append({ "source": source_target[0],
"target": source_target[1],
"weight": int(weight)},
ignore_index = True)
collab_df = collab_df.groupby(['source','target']).agg({'weight': 'sum'}).reset_index()
institutes = df["institute_code"].unique().tolist()
institutes.sort()
all = []
for main in institutes:
ins=[]
for inner in institutes:
if main == inner:
ins.append(0)
else:
if collab_df[((collab_df["source"]== main) | (collab_df["target"]== main))
& ((collab_df["target"]==inner)|(collab_df["source"]==inner))].shape[0] > 0:
ins.append(int(collab_df[((collab_df["source"]== main) | (collab_df["target"]== main))
& ((collab_df["target"]==inner)|(collab_df["source"]==inner))]["weight"]))
else:
ins.append(0)
all.append(ins)
df_cm = pd.DataFrame(all, index = [i for i in institutes],columns = [i for i in institutes])
return df_cm
def calculate_return_model(self):
print("-----------initializing testing (test/test set) sequence--------")
df_mix = self.df
df_mix = df_mix[df_mix["title_piped"].notna()]
df_en = df_mix[(df_mix["lang"]=="en")]
df_de = df_mix[(df_mix["lang"]=="de")]
df_en = df_en.sample(frac=1, random_state=42).reset_index(drop=True)
df_de = df_de.sample(frac=1, random_state=42).reset_index(drop=True)
df_mix = df_mix.sample(frac=1, random_state=42).reset_index(drop=True)
print("-----------splitting datasets in test/trainsets--------")
# df_mix_train, df_mix_test = self.generate_test_trainset(df_mix)
# df_en_train, df_en_test = self.generate_test_trainset(df_en)
# df_de_train, df_de_test = self.generate_test_trainset(df_de)
topic_en_nr = self.TOPIC_EN_NR
topic_de_nr = self.TOPIC_DE_NR
topic_mix_nr = self.TOPIC_MIX_NR
print("-----------building BERT models from training corpus(s)--------")
topics_en, probs_en, model_en = self.compute_BERT_model(self.MIN_TOPIC_SIZE, topic_en_nr, df_en)
topics_de, probs_de, model_de = self.compute_BERT_model_multilin(self.MIN_TOPIC_SIZE, topic_de_nr, df_de)
topics_mix, probs_mix, model_mix = self.compute_BERT_model_multilin(self.MIN_TOPIC_SIZE, topic_mix_nr, df_mix)
print("-----------assigning top n topics to every record and normalizing--------")
sample_df = self.assign_highest_n_contribution(df_en, model_en, 10)
sample_de_df = self.assign_highest_n_contribution(df_de, model_de, 10)
sample_mix_df = self.assign_highest_n_contribution(df_mix, model_mix, 10)
self.MODELS["en"]["model"], self.MODELS["en"]["topic_df"] = model_en, sample_df
self.MODELS["de"]["model"], self.MODELS["de"]["topic_df"] = model_de, sample_de_df
self.MODELS["mix"]["model"], self.MODELS["mix"]["topic_df"] = model_mix, sample_mix_df
self.MODELS["en"]["df_test"], self.MODELS["de"]["df_test"], self.MODELS["mix"]["df_test"] = df_en, df_de, df_mix
return self.MODELS
def calculate_return_model_whole(self):
print("-----------initializing testing (test/test set) sequence--------")
df_mix = self.df
df_mix = df_mix[df_mix["title_piped"].notna()]
df_en = df_mix[(df_mix["lang"]=="en")]
df_de = df_mix[(df_mix["lang"]=="de")]
df_en = df_en.sample(frac=1, random_state=42).reset_index(drop=True)
df_de = df_de.sample(frac=1, random_state=42).reset_index(drop=True)
df_mix = df_mix.sample(frac=1, random_state=42).reset_index(drop=True)
topic_en_nr = self.TOPIC_EN_NR
topic_de_nr = self.TOPIC_DE_NR
topic_mix_nr = self.TOPIC_MIX_NR
print("-----------building BERT models from training corpus(s)--------")
topics_en, probs_en, model_en = self.compute_BERT_model(self.MIN_TOPIC_SIZE, topic_en_nr, df_en)
topics_de, probs_de, model_de = self.compute_BERT_model_multilin(self.MIN_TOPIC_SIZE, topic_de_nr, df_de)
topics_mix, probs_mix, model_mix = self.compute_BERT_model_multilin(self.MIN_TOPIC_SIZE, topic_mix_nr, df_mix)
print("-----------assigning top n topics to every record and normalizing--------")
sample_df = self.assign_highest_n_contribution(df_en, model_en, 10)
sample_de_df = self.assign_highest_n_contribution(df_de, model_de, 10)
sample_mix_df = self.assign_highest_n_contribution(df_mix, model_mix, 10)
self.MODELS["en"]["model"], self.MODELS["en"]["topic_df"] = model_en, sample_df
self.MODELS["de"]["model"], self.MODELS["de"]["topic_df"] = model_de, sample_de_df
self.MODELS["mix"]["model"], self.MODELS["mix"]["topic_df"] = model_mix, sample_mix_df
self.MODELS["en"]["df_test"], self.MODELS["de"]["df_test"], self.MODELS["mix"]["df_test"] = df_en, df_de, df_mix
return self.MODELS
@staticmethod
def generate_collabs(origin_df, merged_df, model, highest_n):
collabs = []
df_test = pd.DataFrame()
for index, row in progress(origin_df.iterrows(), total = origin_df.shape[0]):
title = row["title_piped"]
topics, similarities = model.find_topics(title, top_n=highest_n)
if len(topics) > 0 :
institute_temp = None
for idx,tupel in enumerate(zip(topics, similarities)):
topic_num, similarity = tupel
institute_series = merged_df[merged_df["Dominant_topic"] == int(topic_num)].groupby("institute_code").size().sort_values(ascending=False).head(highest_n)
institutes_list = institute_series.reset_index().values.tolist()
institutes_predicted = [institute [0] for institute in institutes_list]
institute_origin = row["institute_code"]
for inst in institutes_predicted:
if not inst == institute_origin:
institute_tuple = (institute_origin, inst)
collabs.append(institute_tuple)
list_tuples = [tuple(sorted(i)) for i in collabs]
tuples = Counter(list_tuples)
count_list_tuples = [list(i) for i in tuples.items()]
for tup in count_list_tuples:
source_target = tup[0]
weight = tup[1]
df_test = df_test.append({ "source": source_target[0],
"target": source_target[1],
"weight": int(weight)},
ignore_index = True)
df_test = df_test.groupby(['source','target']).agg({'weight': 'sum'}).reset_index()
return df_test
@staticmethod
def generate_confusion_matrix(pred_df, origin_df, institutes):
actual_df = BertUtilClass.build_collab_frame(origin_df)
return BertUtilClass.build_cm_dfs(pred_df, institutes), BertUtilClass.build_cm_dfs_origin(actual_df, institutes)
@staticmethod
def build_cm_dfs(collab_df, institutes):
all = []
for main in institutes:
ins=[]
for inner in institutes:
if main == inner:
ins.append(0)
else:
if collab_df[((collab_df["source"]== main) | (collab_df["target"]== main))
& ((collab_df["target"]==inner)|(collab_df["source"]==inner))].shape[0] > 0:
ins.append(int(collab_df[((collab_df["source"]== main) | (collab_df["target"]== main))
& ((collab_df["target"]==inner)|(collab_df["source"]==inner))]["weight"]))
else:
ins.append(0)
all.append(ins)
df_cm = pd.DataFrame(all, index = [i for i in institutes],columns = [i for i in institutes])
return df_cm
@staticmethod
def build_cm_dfs_origin(collab_df, institutes):
all = []
for main in institutes:
ins=[]
for inner in institutes:
if str(main) == str(inner):
ins.append(0)
else:
if collab_df[((collab_df["source"]== str(main)) | (collab_df["target"]== str(main)))
& ((collab_df["target"]==str(inner))|(collab_df["source"]==str(inner)))].shape[0] > 0:
ins.append(int(collab_df[((collab_df["source"]== str(main)) | (collab_df["target"]== str(main)))
& ((collab_df["target"]==str(inner))|(collab_df["source"]==str(inner)))]["weight"]))
else:
ins.append(0)
all.append(ins)
df_cm = pd.DataFrame(all, index = [i for i in institutes],columns = [i for i in institutes])
return df_cm
@staticmethod
def build_collab_frame(input_df):
input_df["collab_insts_tupel"] = input_df["collabs_institutes_code"].apply(lambda x: tuple(x.split("|")))
df_test = pd.DataFrame()
unique_pubids = set(input_df["_id"].tolist())
for id in unique_pubids:
list_tuples = [tuple(sorted(i)) for i in list(input_df[input_df["_id"] == id]["collab_insts_tupel"])]
tuples = Counter(list_tuples)
count_list_tuples = [list(i) for i in tuples.items()]
for tup in count_list_tuples:
source_target = tup[0]
weight = tup[1]
df_test = df_test.append({ "source": source_target[0],
"target": source_target[1],
"weight": int(weight)},
ignore_index = True)
df_test = df_test.groupby(['source','target']).agg({'weight': 'sum'}).reset_index()
return df_test
@staticmethod
def generate_test_trainset(df):
np.random.shuffle(df.values)
train_set = df[:int((len(df) + 1) * 0.8)]
test_set = df[int((len(df) + 1) * 0.8):]
return train_set, test_set
#a method for evaluating the model on the training data itself
def self_evaluation_method_projects(self):
print("-----------initializing self test(training set) sequence--------")
df_mix = self.df
keep_duplicates = self.KEEP_DUPLICATES
df_mix = df_mix[df_mix["title_piped"].notna()]
df_en = df_mix[(df_mix["lang"]=="en")]
df_de = df_mix[(df_mix["lang"]=="de")]
if not keep_duplicates:
df_mix = df_mix.drop_duplicates(subset=["_id"])
df_en = df_en.drop_duplicates(subset=["_id"])
df_de = df_de.drop_duplicates(subset=["_id"])
df_en = df_en.sample(frac=1, random_state=42).reset_index(drop=True)
df_de = df_de.sample(frac=1, random_state=42).reset_index(drop=True)
df_mix = df_mix.sample(frac=1, random_state=42).reset_index(drop=True)
projects_df = self.projects_df
# df_projects_mix = projects_df[["description_piped", "institute", "institute_code"]]
# df_projects_en = projects_df[["en_description_piped", "institute", "institute_code"]]
# df_projects_de = projects_df[projects_df["language"] == "de"][["description_piped", "institute","institute_code"]]
df_projects_mix = projects_df[["description", "institute", "institute_code"]]
df_projects_en = projects_df[["en_description", "institute", "institute_code"]]
df_projects_de = projects_df[projects_df["language"] == "de"][["description", "institute","institute_code"]]
topic_en_nr = self.TOPIC_EN_NR
topic_de_nr = self.TOPIC_DE_NR
topic_mix_nr = self.TOPIC_MIX_NR
print("-----------building BERT models from training corpus(s)--------")
topics_en, probs_en, model_en = self.compute_BERT_model(self.MIN_TOPIC_SIZE, topic_en_nr, df_en)
topics_de, probs_de, model_de = self.compute_BERT_model_multilin(self.MIN_TOPIC_SIZE, topic_de_nr, df_de)
topics_mix, probs_mix, model_mix = self.compute_BERT_model_multilin(self.MIN_TOPIC_SIZE, topic_mix_nr, df_mix)
print("-----------assigning top n topics to every record and normalizing--------")
sample_df = self.assign_highest_n_contribution(df_en, model_en, 10)
sample_de_df = self.assign_highest_n_contribution(df_de, model_de, 10)
sample_mix_df = self.assign_highest_n_contribution(df_mix, model_mix, 10)
print("-----------predicting highest n classes--------")
self.LABELS["y_test_en"], self.LABELS["y_pred_en"] = self.predict_highest_n_classes_projects(df_projects_en, sample_df, model_en, 5)
self.LABELS["y_test_de"], self.LABELS["y_pred_de"] = self.predict_highest_n_classes_projects(df_projects_de, sample_de_df, model_de, 5)
self.LABELS["y_test_mix"], self.LABELS["y_pred_mix"] = self.predict_highest_n_classes_projects(df_projects_mix, sample_mix_df, model_mix, 5)
print("-----------classification accuracy report-------")
print("----------- english model stats-----------------")
self.generate_classification_report(self.LABELS["y_test_en"], self.LABELS["y_pred_en"])
print("----------- german model stats-----------------")
self.generate_classification_report(self.LABELS["y_test_de"], self.LABELS["y_pred_de"])
print("----------- mix model stats-----------------")
self.generate_classification_report(self.LABELS["y_test_mix"], self.LABELS["y_pred_mix"])
#a method for evaluating the model on the training data itself
def self_evaluation_method(self):
print("-----------initializing self test(training set) sequence--------")
df_mix = self.df
keep_duplicates = self.KEEP_DUPLICATES
df_mix = df_mix[df_mix["title_piped"].notna()]
df_en = df_mix[(df_mix["lang"]=="en")]
df_de = df_mix[(df_mix["lang"]=="de")]
if not keep_duplicates:
df_mix = df_mix.drop_duplicates(subset=["_id"])
df_en = df_en.drop_duplicates(subset=["_id"])
df_de = df_de.drop_duplicates(subset=["_id"])
df_en = df_en.sample(frac=1, random_state=42).reset_index(drop=True)
df_de = df_de.sample(frac=1, random_state=42).reset_index(drop=True)
df_mix = df_mix.sample(frac=1, random_state=42).reset_index(drop=True)
topic_en_nr = self.TOPIC_EN_NR
topic_de_nr = self.TOPIC_DE_NR
topic_mix_nr = self.TOPIC_MIX_NR
print("-----------building BERT models from training corpus(s)--------")
topics_en, probs_en, model_en = self.compute_BERT_model(self.MIN_TOPIC_SIZE, topic_en_nr, df_en)
topics_de, probs_de, model_de = self.compute_BERT_model_multilin(self.MIN_TOPIC_SIZE, topic_de_nr, df_de)
topics_mix, probs_mix, model_mix = self.compute_BERT_model_multilin(self.MIN_TOPIC_SIZE, topic_mix_nr, df_mix)
print("-----------assigning top n topics to every record and normalizing--------")
sample_df = self.assign_highest_n_contribution(df_en, model_en, 10)
sample_de_df = self.assign_highest_n_contribution(df_de, model_de, 10)
sample_mix_df = self.assign_highest_n_contribution(df_mix, model_mix, 10)
print("-----------predicting highest n classes--------")
self.LABELS["y_test_en"], self.LABELS["y_pred_en"] = self.predict_highest_n_classes(df_en, sample_df, model_en, 5)
self.LABELS["y_test_de"], self.LABELS["y_pred_de"] = self.predict_highest_n_classes(df_de, sample_de_df, model_de, 5)
self.LABELS["y_test_mix"], self.LABELS["y_pred_mix"] = self.predict_highest_n_classes(df_mix, sample_mix_df, model_mix, 5)
print("-----------classification accuracy report-------")
print("----------- english model stats-----------------")
self.generate_classification_report(self.LABELS["y_test_en"], self.LABELS["y_pred_en"])
print("----------- german model stats-----------------")
self.generate_classification_report(self.LABELS["y_test_de"], self.LABELS["y_pred_de"])
print("----------- mix model stats-----------------")
self.generate_classification_report(self.LABELS["y_test_mix"], self.LABELS["y_pred_mix"])
@staticmethod
def generate_classification_report(y_test, y_pred):
print(classification_report(y_test, y_pred))
#a method for spliting the dataset in train/test and evaluating the test set based on trained training data
def train_test_evaluation_projects(self):
print("-----------initializing testing (test/test set) sequence--------")
df_mix = self.df
keep_duplicates = self.KEEP_DUPLICATES
df_mix = df_mix[df_mix["title_piped"].notna()]
df_en = df_mix[(df_mix["lang"]=="en")]
df_de = df_mix[(df_mix["lang"]=="de")]
if not keep_duplicates:
df_mix = df_mix.drop_duplicates(subset=["_id"])
df_en = df_en.drop_duplicates(subset=["_id"])
df_de = df_de.drop_duplicates(subset=["_id"])
df_en = df_en.sample(frac=1, random_state=42).reset_index(drop=True)
df_de = df_de.sample(frac=1, random_state=42).reset_index(drop=True)
df_mix = df_mix.sample(frac=1, random_state=42).reset_index(drop=True)
print("-----------splitting datasets in test/trainsets--------")
df_mix_train, df_mix_test = self.generate_test_trainset(df_mix)
df_en_train, df_en_test = self.generate_test_trainset(df_en)
df_de_train, df_de_test = self.generate_test_trainset(df_de)
topic_en_nr = self.TOPIC_EN_NR
topic_de_nr = self.TOPIC_DE_NR
topic_mix_nr = self.TOPIC_MIX_NR
projects_df = self.projects_df
# df_projects_mix = projects_df[["description_piped", "institute", "institute_code"]]
# df_projects_en = projects_df[["en_description_piped", "institute", "institute_code"]]
# df_projects_de = projects_df[projects_df["language"] == "de"][["description_piped", "institute","institute_code"]]
df_projects_mix = projects_df[["description", "institute", "institute_code"]]
df_projects_en = projects_df[["en_description", "institute", "institute_code"]]
df_projects_de = projects_df[projects_df["language"] == "de"][["description", "institute","institute_code"]]
print("-----------building BERT models from training corpus(s)--------")
topics_en, probs_en, model_en = self.compute_BERT_model(self.MIN_TOPIC_SIZE, topic_en_nr, df_en_train)
topics_de, probs_de, model_de = self.compute_BERT_model_multilin(self.MIN_TOPIC_SIZE, topic_de_nr, df_de_train)
topics_mix, probs_mix, model_mix = self.compute_BERT_model_multilin(self.MIN_TOPIC_SIZE, topic_mix_nr, df_mix_train)
self.MODEL_PROPs["en_model"] = model_en
self.MODEL_PROPs["de_model"] = model_de
self.MODEL_PROPs["mix_model"] = model_mix
print("-----------assigning top n topics to every record and normalizing--------")
sample_df = self.assign_highest_n_contribution(df_en_test, model_en, 17)
sample_de_df = self.assign_highest_n_contribution(df_de_test, model_de, 17)
sample_mix_df = self.assign_highest_n_contribution(df_mix_test, model_mix, 17)
print("-----------predicting highest n classes--------")
self.LABELS["y_test_en"], self.LABELS["y_pred_en"] = self.predict_highest_n_classes_projects(df_projects_en, sample_df, model_en, 5)
self.LABELS["y_test_de"], self.LABELS["y_pred_de"] = self.predict_highest_n_classes_projects(df_projects_de, sample_de_df, model_de, 5)
self.LABELS["y_test_mix"], self.LABELS["y_pred_mix"] = self.predict_highest_n_classes_projects(df_projects_mix, sample_mix_df, model_mix, 5)
print("-----------classification accuracy report-------")
print("----------- english model stats-----------------")
self.generate_classification_report(self.LABELS["y_test_en"], self.LABELS["y_pred_en"])
print("----------- german model stats-----------------")
self.generate_classification_report(self.LABELS["y_test_de"], self.LABELS["y_pred_de"])
print("----------- mix model stats-----------------")
self.generate_classification_report(self.LABELS["y_test_mix"], self.LABELS["y_pred_mix"])
#a method for spliting the dataset in train/test and evaluating the test set based on trained training data
def train_test_evaluation(self):
print("-----------initializing testing (test/test set) sequence--------")
df_mix = self.df
keep_duplicates = self.KEEP_DUPLICATES
df_mix = df_mix[df_mix["title_piped"].notna()]
df_en = df_mix[(df_mix["lang"]=="en")]
df_de = df_mix[(df_mix["lang"]=="de")]
if not keep_duplicates:
df_mix = df_mix.drop_duplicates(subset=["_id"])
df_en = df_en.drop_duplicates(subset=["_id"])
df_de = df_de.drop_duplicates(subset=["_id"])
df_en = df_en.sample(frac=1, random_state=42).reset_index(drop=True)
df_de = df_de.sample(frac=1, random_state=42).reset_index(drop=True)
df_mix = df_mix.sample(frac=1, random_state=42).reset_index(drop=True)
print("-----------splitting datasets in test/trainsets--------")
df_mix_train, df_mix_test = self.generate_test_trainset(df_mix)
df_en_train, df_en_test = self.generate_test_trainset(df_en)
df_de_train, df_de_test = self.generate_test_trainset(df_de)
topic_en_nr = self.TOPIC_EN_NR
topic_de_nr = self.TOPIC_DE_NR
topic_mix_nr = self.TOPIC_MIX_NR
print("-----------building BERT models from training corpus(s)--------")
topics_en, probs_en, model_en = self.compute_BERT_model(self.MIN_TOPIC_SIZE, topic_en_nr, df_en_train)
topics_de, probs_de, model_de = self.compute_BERT_model_multilin(self.MIN_TOPIC_SIZE, topic_de_nr, df_de_train)
topics_mix, probs_mix, model_mix = self.compute_BERT_model_multilin(self.MIN_TOPIC_SIZE, topic_mix_nr, df_mix_train)
self.MODEL_PROPs["en_model"] = model_en
self.MODEL_PROPs["de_model"] = model_de
self.MODEL_PROPs["mix_model"] = model_mix
print("-----------assigning top n topics to every record and normalizing--------")
sample_df = self.assign_highest_n_contribution(df_en_test, model_en, 10)
sample_de_df = self.assign_highest_n_contribution(df_de_test, model_de, 10)
sample_mix_df = self.assign_highest_n_contribution(df_mix_test, model_mix, 10)
print("-----------predicting highest n classes--------")
self.LABELS["y_test_en"], self.LABELS["y_pred_en"] = self.predict_highest_n_classes(df_en_test, sample_df, model_en, 5)
self.LABELS["y_test_de"], self.LABELS["y_pred_de"] = self.predict_highest_n_classes(df_de_test, sample_de_df, model_de, 5)
self.LABELS["y_test_mix"], self.LABELS["y_pred_mix"] = self.predict_highest_n_classes(df_mix_test, sample_mix_df, model_mix, 5)
print("-----------classification accuracy report-------")
print("----------- english model stats-----------------")
self.generate_classification_report(self.LABELS["y_test_en"], self.LABELS["y_pred_en"])
print("----------- german model stats-----------------")
self.generate_classification_report(self.LABELS["y_test_de"], self.LABELS["y_pred_de"])
print("----------- mix model stats-----------------")
self.generate_classification_report(self.LABELS["y_test_mix"], self.LABELS["y_pred_mix"])
def generate_threshold_stats_self_evaluation(self):
stats = pd.DataFrame()
stats_dic = {"threshold" : [] ,
"ACC_EN": [],
"PREC_EN": [],
"REC_EN": [],
"F1_EN": [],
"F1_MAC_EN": [],
"F1_WEI_EN": [],
"ACC_DE": [],
"PREC_DE": [],
"REC_DE": [],
"F1_DE": [],
"F1_MAC_DE": [],
"F1_WEI_DE": [],
"ACC_MIX": [],
"PREC_MIX": [],
"REC_MIX": [],
"F1_MIX": [],
"F1_MAC_MIX": [],
"F1_WEI_MIX": []
}
print("-----------initializing self test(training set) sequence--------")
df_mix = self.df
keep_duplicates = self.KEEP_DUPLICATES
df_mix = df_mix[df_mix["title_piped"].notna()]
df_en = df_mix[(df_mix["lang"]=="en")]
df_de = df_mix[(df_mix["lang"]=="de")]
if not keep_duplicates:
df_mix = df_mix.drop_duplicates(subset=["_id"])
df_en = df_en.drop_duplicates(subset=["_id"])
df_de = df_de.drop_duplicates(subset=["_id"])
df_en = df_en.sample(frac=1, random_state=42).reset_index(drop=True)
df_de = df_de.sample(frac=1, random_state=42).reset_index(drop=True)
df_mix = df_mix.sample(frac=1, random_state=42).reset_index(drop=True)
topic_en_nr = self.TOPIC_EN_NR
topic_de_nr = self.TOPIC_DE_NR
topic_mix_nr = self.TOPIC_MIX_NR
print("-----------building BERT models from training corpus(s)--------")
topics_en, probs_en, model_en = self.compute_BERT_model(self.MIN_TOPIC_SIZE, topic_en_nr, df_en)
topics_de, probs_de, model_de = self.compute_BERT_model_multilin(self.MIN_TOPIC_SIZE, topic_de_nr, df_de)
topics_mix, probs_mix, model_mix = self.compute_BERT_model_multilin(self.MIN_TOPIC_SIZE, topic_mix_nr, df_mix)
print("-----------assigning top n topics to every record and normalizing--------")
sample_df = self.assign_highest_n_contribution(df_en, model_en, 16)
sample_de_df = self.assign_highest_n_contribution(df_de, model_de, 16)
sample_mix_df = self.assign_highest_n_contribution(df_mix, model_mix, 16)
for thresh in range(1,16):
print(f"-----------predicting highest '{thresh}' classes--------")
self.LABELS["y_test_en"], self.LABELS["y_pred_en"] = self.predict_highest_n_classes(df_en, sample_df, model_en, thresh )
self.LABELS["y_test_de"], self.LABELS["y_pred_de"] = self.predict_highest_n_classes(df_de, sample_de_df, model_de, thresh)
self.LABELS["y_test_mix"], self.LABELS["y_pred_mix"] = self.predict_highest_n_classes(df_mix, sample_mix_df, model_mix, thresh)
stats_dic["threshold"].append(thresh)
stats_dic["ACC_EN"].append(accuracy_score(self.LABELS["y_test_en"], self.LABELS["y_pred_en"]))
stats_dic["PREC_EN"].append(precision_score(self.LABELS["y_test_en"], self.LABELS["y_pred_en"], average='micro'))
stats_dic["REC_EN"].append(recall_score(self.LABELS["y_test_en"], self.LABELS["y_pred_en"], average='micro'))
stats_dic["F1_EN"].append(f1_score(self.LABELS["y_test_en"], self.LABELS["y_pred_en"], average='micro'))
stats_dic["F1_MAC_EN"].append(f1_score(self.LABELS["y_test_en"], self.LABELS["y_pred_en"], average='macro'))
stats_dic["F1_WEI_EN"].append(f1_score(self.LABELS["y_test_en"], self.LABELS["y_pred_en"], average='weighted'))
stats_dic["ACC_DE"].append(accuracy_score(self.LABELS["y_test_de"], self.LABELS["y_pred_de"]))
stats_dic["PREC_DE"].append(precision_score(self.LABELS["y_test_de"], self.LABELS["y_pred_de"], average='micro'))
stats_dic["REC_DE"].append(recall_score(self.LABELS["y_test_de"], self.LABELS["y_pred_de"], average='micro'))
stats_dic["F1_DE"].append(f1_score(self.LABELS["y_test_de"], self.LABELS["y_pred_de"], average='micro'))
stats_dic["F1_MAC_DE"].append(f1_score(self.LABELS["y_test_de"], self.LABELS["y_pred_de"], average='macro'))
stats_dic["F1_WEI_DE"].append(f1_score(self.LABELS["y_test_de"], self.LABELS["y_pred_de"], average='weighted'))
stats_dic["ACC_MIX"].append(accuracy_score(self.LABELS["y_test_mix"], self.LABELS["y_pred_mix"]))
stats_dic["PREC_MIX"].append(precision_score(self.LABELS["y_test_mix"], self.LABELS["y_pred_mix"], average='micro'))
stats_dic["REC_MIX"].append(recall_score(self.LABELS["y_test_mix"], self.LABELS["y_pred_mix"], average='micro'))
stats_dic["F1_MIX"].append(f1_score(self.LABELS["y_test_mix"], self.LABELS["y_pred_mix"], average='micro'))
stats_dic["F1_MAC_MIX"].append(f1_score(self.LABELS["y_test_mix"], self.LABELS["y_pred_mix"], average='macro'))
stats_dic["F1_WEI_MIX"].append(f1_score(self.LABELS["y_test_mix"], self.LABELS["y_pred_mix"], average='weighted'))
return stats.from_dict(stats_dic)
def generate_threshold_stats_tt_evaluation(self):
stats = pd.DataFrame()
stats_dic = {"threshold" : [] ,
"ACC_EN": [],
"PREC_EN": [],
"REC_EN": [],
"F1_EN": [],
"F1_MAC_EN": [],
"F1_WEI_EN": [],
"ACC_DE": [],
"PREC_DE": [],
"REC_DE": [],
"F1_DE": [],
"F1_MAC_DE": [],
"F1_WEI_DE": [],
"ACC_MIX": [],
"PREC_MIX": [],
"REC_MIX": [],
"F1_MIX": [],
"F1_MAC_MIX": [],
"F1_WEI_MIX": []
}
print("-----------initializing self test(training set) sequence--------")
df_mix = self.df
keep_duplicates = self.KEEP_DUPLICATES
df_mix = df_mix[df_mix["title_piped"].notna()]
df_en = df_mix[(df_mix["lang"]=="en")]
df_de = df_mix[(df_mix["lang"]=="de")]
if not keep_duplicates:
df_mix = df_mix.drop_duplicates(subset=["_id"])
df_en = df_en.drop_duplicates(subset=["_id"])
df_de = df_de.drop_duplicates(subset=["_id"])
df_en = df_en.sample(frac=1, random_state=42).reset_index(drop=True)
df_de = df_de.sample(frac=1, random_state=42).reset_index(drop=True)
df_mix = df_mix.sample(frac=1, random_state=42).reset_index(drop=True)
print("-----------splitting datasets in test/trainsets--------")
df_mix_train, df_mix_test = self.generate_test_trainset(df_mix)
df_en_train, df_en_test = self.generate_test_trainset(df_en)
df_de_train, df_de_test = self.generate_test_trainset(df_de)
topic_en_nr = self.TOPIC_EN_NR
topic_de_nr = self.TOPIC_DE_NR
topic_mix_nr = self.TOPIC_MIX_NR
print("-----------building BERT models from training corpus(s)--------")
topics_en, probs_en, model_en = self.compute_BERT_model(self.MIN_TOPIC_SIZE, topic_en_nr, df_en_train)
topics_de, probs_de, model_de = self.compute_BERT_model_multilin(self.MIN_TOPIC_SIZE, topic_de_nr, df_de_train)
topics_mix, probs_mix, model_mix = self.compute_BERT_model_multilin(self.MIN_TOPIC_SIZE, topic_mix_nr, df_mix_train)
print("-----------assigning top n topics to every record and normalizing--------")
sample_df = self.assign_highest_n_contribution(df_en_test, model_en, 16)
sample_de_df = self.assign_highest_n_contribution(df_de_test, model_de, 16)
sample_mix_df = self.assign_highest_n_contribution(df_mix_test, model_mix, 16)
for thresh in range(1,16):
print(f"-----------predicting highest '{thresh}' classes--------")
self.LABELS["y_test_en"], self.LABELS["y_pred_en"] = self.predict_highest_n_classes(df_en_test, sample_df, model_en, thresh)
self.LABELS["y_test_de"], self.LABELS["y_pred_de"] = self.predict_highest_n_classes(df_de_test, sample_de_df, model_de, thresh)
self.LABELS["y_test_mix"], self.LABELS["y_pred_mix"] = self.predict_highest_n_classes(df_mix_test, sample_mix_df, model_mix, thresh)
stats_dic["threshold"].append(thresh)
stats_dic["ACC_EN"].append(accuracy_score(self.LABELS["y_test_en"], self.LABELS["y_pred_en"]))
stats_dic["PREC_EN"].append(precision_score(self.LABELS["y_test_en"], self.LABELS["y_pred_en"], average='micro'))
stats_dic["REC_EN"].append(recall_score(self.LABELS["y_test_en"], self.LABELS["y_pred_en"], average='micro'))
stats_dic["F1_EN"].append(f1_score(self.LABELS["y_test_en"], self.LABELS["y_pred_en"], average='micro'))
stats_dic["F1_MAC_EN"].append(f1_score(self.LABELS["y_test_en"], self.LABELS["y_pred_en"], average='macro'))
stats_dic["F1_WEI_EN"].append(f1_score(self.LABELS["y_test_en"], self.LABELS["y_pred_en"], average='weighted'))
stats_dic["ACC_DE"].append(accuracy_score(self.LABELS["y_test_de"], self.LABELS["y_pred_de"]))
stats_dic["PREC_DE"].append(precision_score(self.LABELS["y_test_de"], self.LABELS["y_pred_de"], average='micro'))
stats_dic["REC_DE"].append(recall_score(self.LABELS["y_test_de"], self.LABELS["y_pred_de"], average='micro'))
stats_dic["F1_DE"].append(f1_score(self.LABELS["y_test_de"], self.LABELS["y_pred_de"], average='micro'))
stats_dic["F1_MAC_DE"].append(f1_score(self.LABELS["y_test_de"], self.LABELS["y_pred_de"], average='macro'))
stats_dic["F1_WEI_DE"].append(f1_score(self.LABELS["y_test_de"], self.LABELS["y_pred_de"], average='weighted'))
stats_dic["ACC_MIX"].append(accuracy_score(self.LABELS["y_test_mix"], self.LABELS["y_pred_mix"]))
stats_dic["PREC_MIX"].append(precision_score(self.LABELS["y_test_mix"], self.LABELS["y_pred_mix"], average='micro'))
stats_dic["REC_MIX"].append(recall_score(self.LABELS["y_test_mix"], self.LABELS["y_pred_mix"], average='micro'))
stats_dic["F1_MIX"].append(f1_score(self.LABELS["y_test_mix"], self.LABELS["y_pred_mix"], average='micro'))
stats_dic["F1_MAC_MIX"].append(f1_score(self.LABELS["y_test_mix"], self.LABELS["y_pred_mix"], average='macro'))
stats_dic["F1_WEI_MIX"].append(f1_score(self.LABELS["y_test_mix"], self.LABELS["y_pred_mix"], average='weighted'))
return stats.from_dict(stats_dic)
def generate_threshold_stats_tt_evaluation_projects(self):
stats = pd.DataFrame()
stats_dic = {"threshold" : [] ,
"ACC_EN": [],
"PREC_EN": [],
"REC_EN": [],
"F1_EN": [],
"F1_MAC_EN": [],
"F1_WEI_EN": [],
"ACC_DE": [],
"PREC_DE": [],
"REC_DE": [],
"F1_DE": [],
"F1_MAC_DE": [],
"F1_WEI_DE": [],
"ACC_MIX": [],
"PREC_MIX": [],
"REC_MIX": [],
"F1_MIX": [],
"F1_MAC_MIX": [],
"F1_WEI_MIX": []
}
print("-----------initializing self test(training set) sequence--------")
df_mix = self.df
keep_duplicates = self.KEEP_DUPLICATES
df_mix = df_mix[df_mix["title_piped"].notna()]
df_en = df_mix[(df_mix["lang"]=="en")]
df_de = df_mix[(df_mix["lang"]=="de")]
if not keep_duplicates:
df_mix = df_mix.drop_duplicates(subset=["_id"])
df_en = df_en.drop_duplicates(subset=["_id"])
df_de = df_de.drop_duplicates(subset=["_id"])
df_en = df_en.sample(frac=1, random_state=42).reset_index(drop=True)
df_de = df_de.sample(frac=1, random_state=42).reset_index(drop=True)
df_mix = df_mix.sample(frac=1, random_state=42).reset_index(drop=True)
print("-----------splitting datasets in test/trainsets--------")
# df_mix_train, df_mix_test = self.generate_test_trainset(df_mix)
# df_en_train, df_en_test = self.generate_test_trainset(df_en)
# df_de_train, df_de_test = self.generate_test_trainset(df_de)
topic_en_nr = self.TOPIC_EN_NR
topic_de_nr = self.TOPIC_DE_NR
topic_mix_nr = self.TOPIC_MIX_NR
projects_df = self.projects_df
# df_projects_mix = projects_df[["description_piped", "institute", "institute_code"]]
# df_projects_en = projects_df[["en_description_piped", "institute", "institute_code"]]
# df_projects_de = projects_df[projects_df["language"] == "de"][["description_piped", "institute","institute_code"]]
df_projects_mix = projects_df[["description", "institute", "institute_code"]]
df_projects_en = projects_df[["en_description", "institute", "institute_code"]]
df_projects_de = projects_df[projects_df["language"] == "de"][["description", "institute","institute_code"]]
print("-----------building BERT models from training corpus(s)--------")
topics_en, probs_en, model_en = self.compute_BERT_model(self.MIN_TOPIC_SIZE, topic_en_nr, df_en)
topics_de, probs_de, model_de = self.compute_BERT_model_multilin(self.MIN_TOPIC_SIZE, topic_de_nr, df_de)
topics_mix, probs_mix, model_mix = self.compute_BERT_model_multilin(self.MIN_TOPIC_SIZE, topic_mix_nr, df_mix)
print("-----------assigning top n topics to every record and normalizing--------")
sample_df = self.assign_highest_n_contribution(df_en, model_en, 16)
sample_de_df = self.assign_highest_n_contribution(df_de, model_de, 16)
sample_mix_df = self.assign_highest_n_contribution(df_mix, model_mix, 16)
for thresh in range(1,16):
print(f"-----------predicting highest '{thresh}' classes--------")
self.LABELS["y_test_en"], self.LABELS["y_pred_en"] = self.predict_highest_n_classes_projects(df_projects_en, sample_df, model_en, thresh)
self.LABELS["y_test_de"], self.LABELS["y_pred_de"] = self.predict_highest_n_classes_projects(df_projects_de, sample_de_df, model_de, thresh)
self.LABELS["y_test_mix"], self.LABELS["y_pred_mix"] = self.predict_highest_n_classes_projects(df_projects_mix, sample_mix_df, model_mix, thresh)
stats_dic["threshold"].append(thresh)
stats_dic["ACC_EN"].append(accuracy_score(self.LABELS["y_test_en"], self.LABELS["y_pred_en"]))
stats_dic["PREC_EN"].append(precision_score(self.LABELS["y_test_en"], self.LABELS["y_pred_en"], average='micro'))
stats_dic["REC_EN"].append(recall_score(self.LABELS["y_test_en"], self.LABELS["y_pred_en"], average='micro'))
stats_dic["F1_EN"].append(f1_score(self.LABELS["y_test_en"], self.LABELS["y_pred_en"], average='micro'))
stats_dic["F1_MAC_EN"].append(f1_score(self.LABELS["y_test_en"], self.LABELS["y_pred_en"], average='macro'))
stats_dic["F1_WEI_EN"].append(f1_score(self.LABELS["y_test_en"], self.LABELS["y_pred_en"], average='weighted'))
stats_dic["ACC_DE"].append(accuracy_score(self.LABELS["y_test_de"], self.LABELS["y_pred_de"]))
stats_dic["PREC_DE"].append(precision_score(self.LABELS["y_test_de"], self.LABELS["y_pred_de"], average='micro'))
stats_dic["REC_DE"].append(recall_score(self.LABELS["y_test_de"], self.LABELS["y_pred_de"], average='micro'))
stats_dic["F1_DE"].append(f1_score(self.LABELS["y_test_de"], self.LABELS["y_pred_de"], average='micro'))
stats_dic["F1_MAC_DE"].append(f1_score(self.LABELS["y_test_de"], self.LABELS["y_pred_de"], average='macro'))
stats_dic["F1_WEI_DE"].append(f1_score(self.LABELS["y_test_de"], self.LABELS["y_pred_de"], average='weighted'))
stats_dic["ACC_MIX"].append(accuracy_score(self.LABELS["y_test_mix"], self.LABELS["y_pred_mix"]))
stats_dic["PREC_MIX"].append(precision_score(self.LABELS["y_test_mix"], self.LABELS["y_pred_mix"], average='micro'))
stats_dic["REC_MIX"].append(recall_score(self.LABELS["y_test_mix"], self.LABELS["y_pred_mix"], average='micro'))
stats_dic["F1_MIX"].append(f1_score(self.LABELS["y_test_mix"], self.LABELS["y_pred_mix"], average='micro'))
stats_dic["F1_MAC_MIX"].append(f1_score(self.LABELS["y_test_mix"], self.LABELS["y_pred_mix"], average='macro'))
stats_dic["F1_WEI_MIX"].append(f1_score(self.LABELS["y_test_mix"], self.LABELS["y_pred_mix"], average='weighted'))
return stats.from_dict(stats_dic)