bigml/resource/evaluation.rs
1//! An evaluation of how well a model (or ensemble) predicts the data.
2
3use serde::de::DeserializeOwned;
4use serde::{Deserialize, Serialize};
5use std::fmt;
6
7use super::id::*;
8use super::status::*;
9use super::{Resource, ResourceCommon};
10
11/// An evaluation of how well a model (or ensemble) predicts the data.
12///
13/// TODO: Still lots of missing fields.
14#[derive(Clone, Debug, Deserialize, Resource, Serialize)]
15#[serde(bound(deserialize = ""))]
16#[api_name = "evaluation"]
17#[non_exhaustive]
18pub struct Evaluation<R: Result> {
19 /// Common resource information. These fields will be serialized at the
20 /// top-level of this structure by `serde`.
21 #[serde(flatten)]
22 pub common: ResourceCommon,
23
24 /// The ID of this resource.
25 pub resource: Id<Evaluation<R>>,
26
27 /// The status of this resource.
28 pub status: GenericStatus,
29
30 /// The result of this evaluation.
31 pub result: R,
32}
33
34/// The result of an evaluation.
35///
36/// TODO: I'm not sure we want to shadow `Result`. But this name will
37/// basically always be qualified, so maybe it's OK.
38pub trait Result:
39 fmt::Debug + DeserializeOwned + Send + Serialize + Sized + Sync + 'static
40{
41}
42
43/// The result of evaluating a classifier.
44#[derive(Debug, Deserialize, Serialize)]
45#[non_exhaustive]
46pub struct ClassificationResult {
47 /// The names of our classifications.
48 pub class_names: Vec<String>,
49
50 /// According to BigML, "Measures the performance of the classifier
51 /// that predicts the mode class for all the instances in the dataset."
52 pub mode: DetailedClassificationResult,
53
54 /// The performance of this model.
55 pub model: DetailedClassificationResult,
56
57 /// According to BigML, "Measures the performance of the classifier
58 /// that predicts a random class for all the instances in the dataset."
59 pub random: DetailedClassificationResult,
60}
61
62impl Result for ClassificationResult {}
63
64/// The detailed result of an evaluation using specific criteria.
65#[derive(Debug, Deserialize, Serialize)]
66#[non_exhaustive]
67pub struct DetailedClassificationResult {
68 /// The portion of instances we classified correctly.
69 pub accuracy: f64,
70 /// The average f-measure over all classes.
71 pub average_f_measure: f64,
72 /// The average phi over all classes.
73 pub average_phi: f64,
74 /// The average precision over all classes.
75 pub average_precision: f64,
76 /// The average recall over all classes.
77 pub average_recall: f64,
78 /// A list of rows of the confusion matrix for this model.
79 pub confusion_matrix: Vec<Vec<f64>>,
80 /// Statistics for each of the individidual classes.
81 pub per_class_statistics: Vec<ClassificationPerClassStatistics>,
82}
83
84/// The detailed result of an evaluation using specific criteria.
85#[derive(Debug, Deserialize, Serialize)]
86#[non_exhaustive]
87pub struct ClassificationPerClassStatistics {
88 /// The portion of instances in this class that were correctly
89 /// classified.
90 pub accuracy: f64,
91 /// The the of this class.
92 pub class_name: String,
93 /// The harmonic mean of precision and recall.
94 pub f_measure: f64,
95 /// See
96 /// [Wikipedia](http://en.wikipedia.org/wiki/Matthews_correlation_coefficient).
97 pub phi_coefficient: f64,
98 /// The fraction of positives that were true positives. (TP / (TP + FP))
99 pub precision: f64,
100 /// The number of true positives over the number of actual positives in
101 /// the dataset. (TP / (TP + FN))
102 pub recall: f64,
103}
104
105// TODO: RegressionResult.