Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.Sign up
Log loss metric #318
The following gets sample_weight "working", in the sense that the computation completes:
diff --git a/dask_ml/metrics/classification.py b/dask_ml/metrics/classification.py index 7766370..3025501 100644 --- a/dask_ml/metrics/classification.py +++ b/dask_ml/metrics/classification.py @@ -94,11 +94,13 @@ def accuracy_score(y_true, y_pred, normalize=True, sample_weight=None, compute=T return score -def _log_loss_inner(x, y, **kwargs): +def _log_loss_inner(x, y, sample_weight, **kwargs): # da.map_blocks wasn't able to concatenate together the results # when we reduce down to a scalar per block. So we make an # array with 1 element. - return np.array([sklearn.metrics.log_loss(x, y, **kwargs)]) + if sample_weight is not None: + sample_weight = sample_weight.ravel() + return np.array([sklearn.metrics.log_loss(x, y, sample_weight=sample_weight, **kwargs)]) def log_loss( @@ -107,19 +109,20 @@ def log_loss( if y_true.ndim == 1: y_true = y_true.reshape(-1, 1) if sample_weight is not None: - raise ValueError("sample_weight is not supported.") + # raise ValueError("sample_weight is not supported.") + sample_weight = sample_weight.reshape(-1, 1) assert y_pred.ndim == 2 result = da.map_blocks( _log_loss_inner, y_true, y_pred, + sample_weight, chunks=(1,), drop_axis=1, dtype="f8", eps=eps, normalize=normalize, - sample_weight=sample_weight, labels=labels, ) if normalize: diff --git a/tests/metrics/test_metrics.py b/tests/metrics/test_metrics.py index 795fe7a..0876414 100644 --- a/tests/metrics/test_metrics.py +++ b/tests/metrics/test_metrics.py @@ -86,7 +86,7 @@ def test_pairwise_kernels(kernel): "sample_weight", [ pytest.param( - True, marks=pytest.mark.xfail(reason="sample_weight not implemented") + True, # marks=pytest.mark.xfail(reason="sample_weight not implemented") ), False, ],
Unfortunately, this means the problem is no longer linear, so we can just take a simple sum / mean of the block-wise scores.
Although... I wonder if we could do a final weighted sum of the blockwise scores, weighted by each block's weight?