I would like to know if there´s any issue behind using sklearn
's precision/recall
metric functions and coding up from scratch in a multiclass
classification task. I noticed some researchers go by implementing this from scratch (multiclass) when it is clear such experience researcher cannot be unaware of sklearn
's provided functions.
For example in this, a 5-class classification task. The research calculates precision and recall like so:
Pred = model.predict(Test_X, batch_size=32)
Pred_Label = np.argmax(Pred, axis=1)
ActualPositive = []
for i in range(NoClass):
AA = np.where(Test_Y_ori == i)[0]
ActualPositive.append(AA)
PredictedPositive = []
for i in range(NoClass):
AA = np.where(Pred_Label == i)[0]
PredictedPositive.append(AA)
TruePositive = []
FalsePositive = []
for i in range(NoClass):
AA = []
BB = []
for j in PredictedPositive[i]:
if Pred_Label[j] == Test_Y_ori[j]:
AA.append(j)
else:
BB.append(j)
TruePositive.append(AA)
FalsePositive.append(BB)
Precision = []
Recall = []
for i in range(NoClass):
Precision.append(len(TruePositive[i]) * 1./len(PredictedPositive[i]))
Recall.append(len(TruePositive[i]) * 1./len(ActualPositive[i]))
When he could probably use:
sklearn.metrics.precision_score(y_true, y_pred,..)
sklearn.metrics.recall_score(y_true, y_pred,...)
But the researcher computes confusion matrix
using scikit-learn API like so:
ConfusionM = confusion_matrix(list(Test_Y_ori), Pred_Label, labels=[0, 1, 2, 3, 4])