text_plain_output_1.png
from sklearn.metrics import classification_report

from sklearn.model_selection import train_test_split

import pandas as pd

df = pd.read_csv('../input/misoimprovedta/ta-misogyny-train (3).csv', header=None, sep='\t')
df_eval = pd.read_csv('../input/misoimprovedta/ta-misogyny-dev (2).csv', header=None, sep='\t')
df_test = pd.read_csv('../input/misoimprovedta/ta-misogyny-test (2).csv', header=None)

def create_labels(sentence):
    splits = sentence.split('\t')
    return splits[0]
def change_sentence(sentence):
    splits = sentence.split('\t')
    return splits[1]
df_eval.rename(columns={0: 'Labels', 1: 'Text'}, inplace=True)
df_eval = df_eval[['Text', 'Labels']]
df.rename(columns={0: 'Labels', 1: 'Text'}, inplace=True)
df = df[['Text', 'Labels']]
df_test.rename(columns={0: 'Text'}, inplace=True)

from sklearn.model_selection import train_test_split
X_test, X_dev, y_test, y_dev = train_test_split(df_eval['Text'], df_eval['Labels'], random_state=0)
df_test_ = pd.concat([X_test, y_test], axis=1)
df_dev = pd.concat([X_dev, y_dev], axis=1)
df_dev

def oversample(df):
    classes = df['Labels'].value_counts().to_dict()
    most = max(classes.values())
    classes_list = []
    for key in classes:
        classes_list.append(df[df['Labels'] == key])
    classes_sample = []
    for i in range(1, len(classes_list)):
        classes_sample.append(classes_list[i].sample(most, replace=True))
    df_maybe = pd.concat(classes_sample)
    final_df = pd.concat([df_maybe, classes_list[0]], axis=0)
    final_df = final_df.reset_index(drop=True)
    return pd.DataFrame({'Text': final_df['Text'].tolist(), 'Labels': final_df['Labels'].tolist()})

def over_under_sample(df):
    unq_labels = list(set(df['Labels'].tolist()))
    texts = df['Text'].tolist()
    labels = df['Labels'].tolist()
    data_dict = dict()
    for l in unq_labels:
        data_dict[l] = []
    for i in range(len(texts)):
        data_dict[labels[i]].append(texts[i])
    req_len = len(labels) // len(unq_labels)
    for label in data_dict:
        if len(data_dict[label]) > req_len:
            data_dict[label] = data_dict[label][:req_len]
        new_texts = []
        new_labels = []
        for l in data_dict:
            new_texts += data_dict[l]
            new_labels += [l] * len(data_dict[l])
        return oversample(pd.DataFrame({'Text': new_texts, 'Labels': new_labels}))

from sklearn.metrics import classification_report
result_dict = classification_report(df_test_['Labels'], predictions, output_dict=True)
report = pd.DataFrame(result_dict).transpose()

report