|
| 1 | +import 'package:ml_algo/src/metric/classification/log_loss_metric.dart'; |
| 2 | +import 'package:ml_linalg/matrix.dart'; |
| 3 | +import 'package:test/test.dart'; |
| 4 | + |
| 5 | +void main() { |
| 6 | + group('LogLossMetric', () { |
| 7 | + const metric = LogLossMetric(); |
| 8 | + |
| 9 | + test('perfect predictions → loss ≈ 0', () { |
| 10 | + final yTrue = Matrix.column([1, 0, 1, 0]); |
| 11 | + final yPred = Matrix.column([1.0, 0.0, 1.0, 0.0]); |
| 12 | + expect(metric.getScore(yPred, yTrue), closeTo(0.0, 1e-12)); |
| 13 | + }); |
| 14 | + |
| 15 | + test('typical predictions', () { |
| 16 | + final yTrue = Matrix.column([1, 0]); |
| 17 | + final yPred = Matrix.column([0.9, 0.1]); |
| 18 | + expect(metric.getScore(yPred, yTrue), |
| 19 | + closeTo(0.10536051565782628, 1e-6)); // -ln(0.9) |
| 20 | + }); |
| 21 | + |
| 22 | + test('probabilities are clipped', () { |
| 23 | + final yTrue = Matrix.column([1, 0]); |
| 24 | + final yPred = Matrix.column([0.0, 1.0]); |
| 25 | + expect(metric.getScore(yPred, yTrue).isFinite, isTrue); |
| 26 | + }); |
| 27 | + }); |
| 28 | +} |
0 commit comments