|
5 | 5 |
|
6 | 6 | from checkmates.objectives.regression_objective import RegressionObjective
|
7 | 7 | from checkmates.utils import classproperty
|
| 8 | +from checkmates.objectives.binary_classification_objective import BinaryClassificationObjective |
| 9 | +from checkmates.objectives.multiclass_classification_objective import MulticlassClassificationObjective |
| 10 | + |
| 11 | + |
| 12 | +class LogLossBinary(BinaryClassificationObjective): |
| 13 | + """Log Loss for binary classification. |
| 14 | +
|
| 15 | + Example: |
| 16 | + >>> y_true = pd.Series([0, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1]) |
| 17 | + >>> y_pred = pd.Series([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]) |
| 18 | + >>> np.testing.assert_almost_equal(LogLossBinary().objective_function(y_true, y_pred), 19.6601745) |
| 19 | + """ |
| 20 | + |
| 21 | + name = "Log Loss Binary" |
| 22 | + greater_is_better = False |
| 23 | + score_needs_proba = True |
| 24 | + perfect_score = 0.0 |
| 25 | + is_bounded_like_percentage = False # Range [0, Inf) |
| 26 | + expected_range = [0, 1] |
| 27 | + |
| 28 | + def objective_function( |
| 29 | + self, |
| 30 | + y_true, |
| 31 | + y_predicted, |
| 32 | + y_train=None, |
| 33 | + X=None, |
| 34 | + sample_weight=None, |
| 35 | + ): |
| 36 | + """Objective function for log loss for binary classification.""" |
| 37 | + return metrics.log_loss(y_true, y_predicted, sample_weight=sample_weight) |
| 38 | + |
| 39 | +class LogLossMulticlass(MulticlassClassificationObjective): |
| 40 | + """Log Loss for multiclass classification. |
| 41 | +
|
| 42 | + Example: |
| 43 | + >>> y_true = [0, 1, 2, 0, 2, 1] |
| 44 | + >>> y_pred = [[0.7, 0.2, 0.1], |
| 45 | + ... [0.3, 0.5, 0.2], |
| 46 | + ... [0.1, 0.3, 0.6], |
| 47 | + ... [0.9, 0.1, 0.0], |
| 48 | + ... [0.3, 0.1, 0.6], |
| 49 | + ... [0.5, 0.5, 0.0]] |
| 50 | + >>> np.testing.assert_almost_equal(LogLossMulticlass().objective_function(y_true, y_pred), 0.4783301) |
| 51 | + """ |
| 52 | + |
| 53 | + name = "Log Loss Multiclass" |
| 54 | + greater_is_better = False |
| 55 | + score_needs_proba = True |
| 56 | + perfect_score = 0.0 |
| 57 | + is_bounded_like_percentage = False # Range [0, Inf) |
| 58 | + expected_range = [0, 1] |
| 59 | + |
| 60 | + def objective_function( |
| 61 | + self, |
| 62 | + y_true, |
| 63 | + y_predicted, |
| 64 | + y_train=None, |
| 65 | + X=None, |
| 66 | + sample_weight=None, |
| 67 | + ): |
| 68 | + """Objective function for log loss for multiclass classification.""" |
| 69 | + return metrics.log_loss(y_true, y_predicted, sample_weight=sample_weight) |
| 70 | + |
| 71 | +class R2(RegressionObjective): |
| 72 | + """Coefficient of determination for regression. |
| 73 | +
|
| 74 | + Example: |
| 75 | + >>> y_true = pd.Series([1.5, 2, 3, 1, 0.5, 1, 2.5, 2.5, 1, 0.5, 2]) |
| 76 | + >>> y_pred = pd.Series([1.5, 2.5, 2, 1, 0.5, 1, 3, 2.25, 0.75, 0.25, 1.75]) |
| 77 | + >>> np.testing.assert_almost_equal(R2().objective_function(y_true, y_pred), 0.7638036) |
| 78 | + """ |
| 79 | + |
| 80 | + name = "R2" |
| 81 | + greater_is_better = True |
| 82 | + score_needs_proba = False |
| 83 | + perfect_score = 1 |
| 84 | + is_bounded_like_percentage = False # Range (-Inf, 1] |
| 85 | + expected_range = [-1, 1] |
| 86 | + |
| 87 | + def objective_function( |
| 88 | + self, |
| 89 | + y_true, |
| 90 | + y_predicted, |
| 91 | + y_train=None, |
| 92 | + X=None, |
| 93 | + sample_weight=None, |
| 94 | + ): |
| 95 | + """Objective function for coefficient of determination for regression.""" |
| 96 | + return metrics.r2_score(y_true, y_predicted, sample_weight=sample_weight) |
| 97 | + |
| 98 | +class MedianAE(RegressionObjective): |
| 99 | + """Median absolute error for regression. |
| 100 | +
|
| 101 | + Example: |
| 102 | + >>> y_true = pd.Series([1.5, 2, 3, 1, 0.5, 1, 2.5, 2.5, 1, 0.5, 2]) |
| 103 | + >>> y_pred = pd.Series([1.5, 2.5, 2, 1, 0.5, 1, 3, 2.25, 0.75, 0.25, 1.75]) |
| 104 | + >>> np.testing.assert_almost_equal(MedianAE().objective_function(y_true, y_pred), 0.25) |
| 105 | + """ |
| 106 | + |
| 107 | + name = "MedianAE" |
| 108 | + greater_is_better = False |
| 109 | + score_needs_proba = False |
| 110 | + perfect_score = 0.0 |
| 111 | + is_bounded_like_percentage = False # Range [0, Inf) |
| 112 | + expected_range = [0, float("inf")] |
| 113 | + |
| 114 | + def objective_function( |
| 115 | + self, |
| 116 | + y_true, |
| 117 | + y_predicted, |
| 118 | + y_train=None, |
| 119 | + X=None, |
| 120 | + sample_weight=None, |
| 121 | + ): |
| 122 | + """Objective function for median absolute error for regression.""" |
| 123 | + return metrics.median_absolute_error( |
| 124 | + y_true, |
| 125 | + y_predicted, |
| 126 | + sample_weight=sample_weight, |
| 127 | + ) |
| 128 | + |
8 | 129 |
|
9 | 130 |
|
10 | 131 | class RootMeanSquaredLogError(RegressionObjective):
|
|
0 commit comments