Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
109 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
# Changelog | ||
|
||
## 15.3.0 | ||
- RSS metric added | ||
|
||
## 15.2.4 | ||
- Documentation for classification metrics improved | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import 'package:ml_algo/src/common/exception/matrix_column_exception.dart'; | ||
import 'package:ml_linalg/matrix.dart'; | ||
|
||
void validateMatrixColumns(Iterable<Matrix> matrices) { | ||
final firstInvalidMatrix = matrices | ||
.firstWhere((matrix) => matrix.columnsNum != 1, orElse: () => null); | ||
|
||
if (firstInvalidMatrix == null) { | ||
return; | ||
} | ||
|
||
throw MatrixColumnException(firstInvalidMatrix); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import 'package:ml_algo/src/helpers/validate_matrix_columns.dart'; | ||
import 'package:ml_algo/src/metric/metric.dart'; | ||
import 'package:ml_linalg/matrix.dart'; | ||
|
||
class RssMetric implements Metric { | ||
const RssMetric(); | ||
|
||
@override | ||
double getScore(Matrix predictedLabels, Matrix origLabels) { | ||
validateMatrixColumns([predictedLabels, origLabels]); | ||
|
||
final predicted = predictedLabels | ||
.toVector(); | ||
final original = origLabels | ||
.toVector(); | ||
|
||
return (predicted - original) | ||
.pow(2) | ||
.sum(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import 'package:ml_algo/src/common/exception/matrix_column_exception.dart'; | ||
import 'package:ml_algo/src/metric/regression/rss.dart'; | ||
import 'package:ml_linalg/matrix.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
group('RssMetric', () { | ||
const metric = RssMetric(); | ||
final predictedLabels = Matrix.column([12, 18, 12, 90, 78]); | ||
final originalLabels = Matrix.column([10, 20 , 30, 60, 70]); | ||
|
||
test('should throw an error if predicted labels matrix\'s columns count ' | ||
'is empty', () { | ||
final actual = () => metric.getScore(Matrix.empty(), originalLabels); | ||
|
||
expect(actual, throwsA(isA<MatrixColumnException>())); | ||
}); | ||
|
||
test('should throw an error if predicted labels matrix\'s columns count ' | ||
'is greater than 1', () { | ||
final actual = () => metric.getScore(Matrix.row([1, 2]), originalLabels); | ||
|
||
expect(actual, throwsA(isA<MatrixColumnException>())); | ||
}); | ||
|
||
test('should throw an error if original labels matrix\'s columns count ' | ||
'is empty', () { | ||
final actual = () => metric.getScore(predictedLabels, Matrix.empty()); | ||
|
||
expect(actual, throwsA(isA<MatrixColumnException>())); | ||
}); | ||
|
||
test('should throw an error if original labels matrix\'s columns count ' | ||
'is greater than 1', () { | ||
final actual = () => metric.getScore(predictedLabels, Matrix.row([1, 2])); | ||
|
||
expect(actual, throwsA(isA<MatrixColumnException>())); | ||
}); | ||
|
||
test('should count score', () { | ||
final actual = metric.getScore(predictedLabels, originalLabels); | ||
|
||
expect(actual, 1296); | ||
}); | ||
}); | ||
} |