|
| 1 | +--- |
| 2 | +description: How to use a custom evaluation metric in MLJAR AutoML by passing a Python function directly as eval_metric. |
| 3 | +social: |
| 4 | + cards_layout: default/variant |
| 5 | +--- |
| 6 | + |
| 7 | +# Custom eval metric |
| 8 | + |
| 9 | +`mljar-supervised` supports custom evaluation metrics. |
| 10 | + |
| 11 | +You can pass your own Python function directly as the `eval_metric` argument in `AutoML`. |
| 12 | + |
| 13 | +## Basic usage |
| 14 | + |
| 15 | +The function should have this interface: |
| 16 | + |
| 17 | +```python |
| 18 | +def my_custom_metric(y_true, y_predicted, sample_weight=None): |
| 19 | + # compute score |
| 20 | + return score |
| 21 | +``` |
| 22 | + |
| 23 | +Then use it directly: |
| 24 | + |
| 25 | +```python |
| 26 | +from supervised import AutoML |
| 27 | + |
| 28 | +automl = AutoML( |
| 29 | + results_path="AutoML_custom_metric", |
| 30 | + eval_metric=my_custom_metric, |
| 31 | +) |
| 32 | +automl.fit(X, y) |
| 33 | +``` |
| 34 | + |
| 35 | +## Important rule: the metric must be minimized |
| 36 | + |
| 37 | +Custom metrics in `mljar-supervised` are always treated as metrics to minimize. |
| 38 | + |
| 39 | +This means: |
| 40 | + |
| 41 | +- if lower is better, return the value directly |
| 42 | +- if higher is better, return its negative value |
| 43 | + |
| 44 | +For example: |
| 45 | + |
| 46 | +- MSE can be returned directly |
| 47 | +- precision, F1, or AUC should usually return `-value` |
| 48 | + |
| 49 | +## Regression example |
| 50 | + |
| 51 | +```python |
| 52 | +import numpy as np |
| 53 | +from supervised import AutoML |
| 54 | + |
| 55 | +def custom_mse(y_true, y_predicted, sample_weight=None): |
| 56 | + y_true = np.asarray(y_true) |
| 57 | + y_predicted = np.asarray(y_predicted) |
| 58 | + return np.mean((y_true - y_predicted) ** 2) |
| 59 | + |
| 60 | +automl = AutoML( |
| 61 | + results_path="AutoML_regression_custom_metric", |
| 62 | + eval_metric=custom_mse, |
| 63 | +) |
| 64 | +automl.fit(X, y) |
| 65 | +``` |
| 66 | + |
| 67 | +## Classification example |
| 68 | + |
| 69 | +For classification, `y_predicted` can contain probabilities, so you may need to apply thresholding or `argmax` inside your metric. |
| 70 | + |
| 71 | +```python |
| 72 | +import numpy as np |
| 73 | +from sklearn.metrics import precision_score |
| 74 | +from supervised import AutoML |
| 75 | + |
| 76 | +def positive_class_precision(y_true, y_predicted, sample_weight=None): |
| 77 | + y_true = np.asarray(y_true) |
| 78 | + y_predicted = np.asarray(y_predicted) |
| 79 | + |
| 80 | + if y_predicted.ndim == 2 and y_predicted.shape[1] == 1: |
| 81 | + y_predicted = y_predicted.ravel() |
| 82 | + |
| 83 | + if y_predicted.ndim == 1: |
| 84 | + y_predicted = (y_predicted > 0.5).astype(int) |
| 85 | + else: |
| 86 | + y_predicted = np.argmax(y_predicted, axis=1) |
| 87 | + |
| 88 | + value = precision_score(y_true, y_predicted, sample_weight=sample_weight) |
| 89 | + |
| 90 | + # higher precision is better, so return negative value |
| 91 | + return -value |
| 92 | + |
| 93 | +automl = AutoML( |
| 94 | + results_path="AutoML_classification_custom_metric", |
| 95 | + eval_metric=positive_class_precision, |
| 96 | +) |
| 97 | +automl.fit(X, y) |
| 98 | +``` |
| 99 | + |
| 100 | +## Notes |
| 101 | + |
| 102 | +- the metric function must return a single numeric value |
| 103 | +- the metric should handle `sample_weight=None` |
| 104 | +- the metric will be used for early stopping and model selection |
| 105 | +- the metric should be deterministic and reasonably fast |
| 106 | + |
| 107 | +## FAQ |
| 108 | + |
| 109 | +### Can I pass a function directly? |
| 110 | + |
| 111 | +Yes. This is the supported public interface: |
| 112 | + |
| 113 | +```python |
| 114 | +automl = AutoML(eval_metric=my_custom_metric) |
| 115 | +``` |
| 116 | + |
| 117 | +### Should I pass `eval_metric="user_defined_metric"`? |
| 118 | + |
| 119 | +No. That name is used internally. In user code, pass the function itself. |
| 120 | + |
| 121 | +### Can I maximize my metric directly? |
| 122 | + |
| 123 | +No. Convert it to a minimization target, usually by returning `-value`. |
| 124 | + |
| 125 | +### Why do I need thresholding for some classification metrics? |
| 126 | + |
| 127 | +Because many classification metrics such as precision or F1 expect class labels, while model predictions during evaluation can be probabilities. |
| 128 | + |
| 129 | +## Related pages |
| 130 | + |
| 131 | +- [AutoML API](../api.md) |
| 132 | +- [Save and Load models](save-and-load-models.md) |
| 133 | +- [Preprocessing](preprocessing.md) |
0 commit comments