6

I have a dataset on which I wish to perform multiclass classification using sklearn.linear_model.LogisticRegression(). After fitting the model, I want to get the precission, recall and f1 score for each of the classes for each fold of cross validation.

  • According to the docs, there exists sklearn.metrics.precision_recall_fscore_support(), in which I can provide average=None as a parameter to get the precision, recall, fscore per class.

  • There exists sklearn.model_selection.cross_val_score(), in which I can provide a custom function in the scoring parameter. However, the function has to return a number. In my case, I wish to return a list, which is not possible.

  • There also exists sklearn.model_selection.cross_validate(), in which I can give multiple functions as list to the scoring parameter. This would require me to create 3*NUMBER_OF_CLASSES different functions and pass these to the scoring parameter.

Is there a simpler method available?

humble
  • 2,016
  • 4
  • 27
  • 36
  • Do you want to just print the results? If yes, use a single function, print all the values in that, and that return any single value. – Vivek Kumar Aug 24 '18 at 10:23
  • Yup, that is one workaround, that is print the result in the function that I feed to `scoring` parameter of `sklearn.model_selection.cross_val_score`. But I wish to know, if there is an **official/proper** way provided by sklearn. – humble Aug 24 '18 at 10:25
  • I dont think so. `cross_val_score` or `cross_validate` just iterate over all the folds from `cv` and record the scores. You can always iterate the loops yourself. – Vivek Kumar Aug 24 '18 at 10:46
  • Check https://stackoverflow.com/questions/54201464/cross-validation-metrics-in-scikit-learn-for-each-data-split/54202609#54202609 for some adaptable ideas. – desertnaut Apr 11 '19 at 11:11

0 Answers0