Skip to content

Target is multiclass but average='binary'. Please choose another average setting, one of [None, 'micro', 'macro', 'weighted']. #1566

@MuvvaThriveni

Description

@MuvvaThriveni

I am facing an issue when i am trying to build multiclass classification model
here is my code from starting

import pandas as pd
data=pd.read_csv('/content/Normalized_Data_PBLD.csv')

y=data['label'].tolist()
X_train, X_test, y_train, y_test = train_test_split(data['comment'].tolist(), y, random_state=5, test_size=0.2) #train, test split
#validation split
from sklearn.utils import class_weight
class_weights = class_weight.compute_class_weight(class_weight="balanced",
classes=np.unique(y_train),
y=y_train)
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, random_state=5, test_size=0.1)

list_of_class={'NEG':0,'NTL':1,'POS':2}
y_val=[list_of_class[i.strip()]for i in y_val]
y_train=[list_of_class[i.strip()]for i in y_train]
y_test=[list_of_class[i.strip()]for i in y_test]

d1 = {'comment': X_train, 'label': y_train}
df_train = pd.DataFrame(d1)

d2 = {'comment': X_val, 'label': y_val}
df_val = pd.DataFrame(d2)

d3 = {'comment': X_test, 'label': y_test}
df_test = pd.DataFrame(d3)

calling bert model

model = ClassificationModel('bert', 'bert-base-multilingual-cased', num_labels=3, args={'learning_rate':1e-5, 'num_train_epochs': 2, 'reprocess_input_data': True, 'overwrite_output_dir': True})

model.train_model(df_train)

result, model_outputs, wrong_predictions = model.eval_model(df_val)
when running this line facing below error

ERROR:
ValueError: Target is multiclass but average='binary'. Please choose another average setting, one of [None, 'micro', 'macro', 'weighted'].

n tried another way
from sklearn.metrics import f1_score, accuracy_score

def f1_multiclass(labels, preds):
return f1_score(labels, preds, average='weighted')

result, model_outputs, wrong_predictions = model.eval_model(df_val, f1=f1_multiclass, acc=accuracy_score)

even though same error

ValueError: Target is multiclass but average='binary'. Please choose another average setting, one of [None, 'micro', 'macro', 'weighted'].

anyone please help to solve

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions