77using System . Linq ;
88using Microsoft . Data . DataView ;
99using Microsoft . ML . Data ;
10+ using Microsoft . ML . Data . Evaluators . Metrics ;
1011using Microsoft . ML . Functional . Tests . Datasets ;
1112using Xunit ;
1213
@@ -160,13 +161,86 @@ public static void AssertEqual(TypeTestData testType1, TypeTestData testType2)
160161 Assert . True ( testType1 . Ug . Equals ( testType2 . Ug ) ) ;
161162 }
162163
164+ /// <summary>
165+ /// Check that a <see cref="AnomalyDetectionMetrics"/> object is valid.
166+ /// </summary>
167+ /// <param name="metrics">The metrics object.</param>
168+ public static void AssertMetrics ( AnomalyDetectionMetrics metrics )
169+ {
170+ Assert . InRange ( metrics . Auc , 0 , 1 ) ;
171+ Assert . InRange ( metrics . DrAtK , 0 , 1 ) ;
172+ }
173+
174+ /// <summary>
175+ /// Check that a <see cref="BinaryClassificationMetrics"/> object is valid.
176+ /// </summary>
177+ /// <param name="metrics">The metrics object.</param>
178+ public static void AssertMetrics ( BinaryClassificationMetrics metrics )
179+ {
180+ Assert . InRange ( metrics . Accuracy , 0 , 1 ) ;
181+ Assert . InRange ( metrics . Auc , 0 , 1 ) ;
182+ Assert . InRange ( metrics . Auprc , 0 , 1 ) ;
183+ Assert . InRange ( metrics . F1Score , 0 , 1 ) ;
184+ Assert . InRange ( metrics . NegativePrecision , 0 , 1 ) ;
185+ Assert . InRange ( metrics . NegativeRecall , 0 , 1 ) ;
186+ Assert . InRange ( metrics . PositivePrecision , 0 , 1 ) ;
187+ Assert . InRange ( metrics . PositiveRecall , 0 , 1 ) ;
188+ }
189+
190+ /// <summary>
191+ /// Check that a <see cref="CalibratedBinaryClassificationMetrics"/> object is valid.
192+ /// </summary>
193+ /// <param name="metrics">The metrics object.</param>
194+ public static void AssertMetrics ( CalibratedBinaryClassificationMetrics metrics )
195+ {
196+ Assert . InRange ( metrics . Entropy , double . NegativeInfinity , 1 ) ;
197+ Assert . InRange ( metrics . LogLoss , double . NegativeInfinity , 1 ) ;
198+ Assert . InRange ( metrics . LogLossReduction , double . NegativeInfinity , 100 ) ;
199+ AssertMetrics ( metrics as BinaryClassificationMetrics ) ;
200+ }
201+
202+ /// <summary>
203+ /// Check that a <see cref="ClusteringMetrics"/> object is valid.
204+ /// </summary>
205+ /// <param name="metrics">The metrics object.</param>
206+ public static void AssertMetrics ( ClusteringMetrics metrics )
207+ {
208+ Assert . True ( metrics . AvgMinScore >= 0 ) ;
209+ Assert . True ( metrics . Dbi >= 0 ) ;
210+ if ( ! double . IsNaN ( metrics . Nmi ) )
211+ Assert . True ( metrics . Nmi >= 0 && metrics . Nmi <= 1 ) ;
212+ }
213+
214+ /// <summary>
215+ /// Check that a <see cref="MultiClassClassifierMetrics"/> object is valid.
216+ /// </summary>
217+ /// <param name="metrics">The metrics object.</param>
218+ public static void AssertMetrics ( MultiClassClassifierMetrics metrics )
219+ {
220+ Assert . InRange ( metrics . AccuracyMacro , 0 , 1 ) ;
221+ Assert . InRange ( metrics . AccuracyMicro , 0 , 1 ) ;
222+ Assert . True ( metrics . LogLoss >= 0 ) ;
223+ Assert . InRange ( metrics . TopKAccuracy , 0 , 1 ) ;
224+ }
225+
226+ /// <summary>
227+ /// Check that a <see cref="RankerMetrics"/> object is valid.
228+ /// </summary>
229+ /// <param name="metrics">The metrics object.</param>
230+ public static void AssertMetrics ( RankerMetrics metrics )
231+ {
232+ foreach ( var dcg in metrics . Dcg )
233+ Assert . True ( dcg >= 0 ) ;
234+ foreach ( var ndcg in metrics . Ndcg )
235+ Assert . InRange ( ndcg , 0 , 100 ) ;
236+ }
237+
163238 /// <summary>
164239 /// Check that a <see cref="RegressionMetrics"/> object is valid.
165240 /// </summary>
166241 /// <param name="metrics">The metrics object.</param>
167242 public static void AssertMetrics ( RegressionMetrics metrics )
168243 {
169- // Perform sanity checks on the metrics.
170244 Assert . True ( metrics . Rms >= 0 ) ;
171245 Assert . True ( metrics . L1 >= 0 ) ;
172246 Assert . True ( metrics . L2 >= 0 ) ;
@@ -179,7 +253,6 @@ public static void AssertMetrics(RegressionMetrics metrics)
179253 /// <param name="metric">The <see cref="MetricStatistics"/> object.</param>
180254 public static void AssertMetricStatistics ( MetricStatistics metric )
181255 {
182- // Perform sanity checks on the metrics.
183256 Assert . True ( metric . StandardDeviation >= 0 ) ;
184257 Assert . True ( metric . StandardError >= 0 ) ;
185258 }
@@ -190,7 +263,6 @@ public static void AssertMetricStatistics(MetricStatistics metric)
190263 /// <param name="metrics">The metrics object.</param>
191264 public static void AssertMetricsStatistics ( RegressionMetricsStatistics metrics )
192265 {
193- // The mean can be any float; the standard deviation and error must be >=0.
194266 AssertMetricStatistics ( metrics . Rms ) ;
195267 AssertMetricStatistics ( metrics . L1 ) ;
196268 AssertMetricStatistics ( metrics . L2 ) ;
0 commit comments