diff options
hw(AI/5): Add initial implementation
Not everything is done, no laplace, no logic on input 1, no folds
1 files changed, 40 insertions, 0 deletions
| -rw-r--r-- | Year 4/Artificial Intelligence/hw05/src/main.rs | 40 |
diff --git a/Year 4/Artificial Intelligence/hw05/src/main.rs b/Year 4/Artificial Intelligence/hw05/src/main.rs new file mode 100644 index 0000000..6a53ee8 --- /dev/null +++ b/Year 4/Artificial Intelligence/hw05/src/main.rs @@ -0,0 +1,40 @@ +use text_io::read; + +mod data; +use data::{Person, Party::republican}; +mod probabilities; +use probabilities::PMaps; + +fn test_single(prob: &PMaps, person: &Person) -> bool { + let is_republican = P!(Party = republican | person, prob) >= 0.5; + is_republican == (person.party == republican) +} + +fn test(prob: &PMaps, people: &Vec<Person>) -> f64 { + let mut count_succ = 0.0; + for p in people { + if test_single(&prob, &p) { + count_succ += 1.0; + } + } + count_succ / people.len() as f64 +} + +fn main() { + let fill: u8 = read!(); + let fill = fill == 1; + + let (training, testing) = data::get_data(); + let prob = if fill { + probabilities::value_2_probabilities(&training) + } else { + probabilities::value_3_probabilities(&training) + }; + + // No laplace, so that's probably why it's not 92.80% + println!("1. Train Set Accuracy:"); + println!(" Accuracy: {:.2}%", test(&prob, &training) * 100.0); + + println!("2. Test Set Accuracy:"); + println!(" Accuracy: {:.2}%", test(&prob, &testing) * 100.0); +} |
