deeprust/metrics/
confusion_matrix.rs1use std::{i32, f32};
10
11pub fn exec() {
13 let sample = Confusionmatrix {
15 true_positive: 100,
16 true_negative: 50,
17 false_positive: 10,
18 false_negative: 5,
19 };
20
21 println!("The total predictions {}", sample.total());
22 println!("Accuracy of the model {:.2}", sample.accuracy());
24 println!("Precision of the model {:.2}", sample.precision());
26 println!("True positive rate of the model {:.2}",
28 sample.true_poitive_rate());
29 println!("False positive rate of the model {:.2}",
31 sample.false_positive_rate());
32 println!("Misclassification rate of the model {:.2}",
34 sample.misclassification_rate());
35 println!("Specificity of the model {:.2}", sample.specificity());
37 println!("Prevalance of the model {:.2}", sample.prevalance());
39
40
41}
42
43struct Confusionmatrix {
45 true_positive: i32,
46 true_negative: i32,
47 false_positive: i32,
48 false_negative: i32,
49}
50
51impl Confusionmatrix {
52 fn total(&self) -> i32 {
54 self.true_positive + self.true_negative + self.false_positive + self.false_negative
55 }
56 fn accuracy(&self) -> f32 {
58 percentage((self.true_positive as f32 + self.true_negative as f32) / (self.total() as f32))
59 }
60 fn precision(&self) -> f32 {
62 percentage((self.true_positive as f32) /
63 (self.true_positive as f32 + self.false_positive as f32))
64 }
65 fn true_poitive_rate(&self) -> f32 {
67 percentage((self.true_positive as f32) /
68 (self.true_positive as f32 + self.false_negative as f32))
69 }
70 fn false_positive_rate(&self) -> f32 {
72 percentage((self.false_positive as f32) /
73 (self.false_positive as f32 + self.true_negative as f32))
74 }
75 fn misclassification_rate(&self) -> f32 {
77 percentage((self.false_positive as f32 + self.false_negative as f32) /
78 (self.total() as f32))
79 }
80 fn specificity(&self) -> f32 {
82 percentage((self.true_negative as f32) /
83 (self.false_positive as f32 + self.true_negative as f32))
84 }
85 fn prevalance(&self) -> f32 {
87 percentage((self.true_positive as f32 + self.false_negative as f32) / (self.total() as f32))
88 }
89}
90
91fn percentage(value: f32) -> f32 {
93 value as f32 * 100.0
94}