I want to calculate the average percentage hit rate of the true class and the adjacent classes and implement it in my cross validation.
#Example of my classification problem (in total 9 classes)
y_true = [0, 0, 1, 5, 3, 4]
y_pred = [0, 1, 0, 8, 6, 5]
The regular accuracy would result in 16,67 (the first prediction is the only one that's true). However, I would like to get the 'adjacent accuracy' which would be 66,67% in this case (the three first predictions are 'correct', together with the last one).
The formula would be like this: adjacent accuracy formula
where Pi stands for the total number of samples classified as class i, g is the total number of classes (= here 9), and n is the total number of samples.
I have already looked at this other question but it isn't particularly helpful since I would like to incorporate this scoring measure into a cross_validate function.
This is my current code:
scoringX = {'acc': 'accuracy',
'prec_macro': 'precision_macro',
'rec_macro': 'recall_macro',
'auc': 'roc_auc_ovr_weighted'}
cv_scores_rf = cross_validate(clf, X, y, cv=kcv, scoring = scoringX)
cv_predict_rf = cross_val_predict(clf, X, y, cv=kcv)
This is would I would ideally like to end up with
scoringX = {'acc': 'accuracy',
'prec_macro': 'precision_macro',
'rec_macro': 'recall_macro',
'auc': 'roc_auc_ovr_weighted',
'adjacent_auc': make_scorer(custom_adjacent_accuracy_score)}
cv_scores_rf = cross_validate(clf, X, y, cv=kcv, scoring = scoringX)
cv_predict_rf = cross_val_predict(clf, X, y, cv=kcv)
Thanks in advance!