Summary Files Commits Branches @commit @diff @stats
path: root / Year 4 / Artificial Intelligence / hw05 / src / main.rs
blob: 2ba39808c419ca8fdf4e89a4e5b9f14d5fc845a3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
}

static folds: usize = 10;

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!("");

    println!("{}-Fold Cross-Validation Results:", folds);
    let mut accuracy = Vec::new();
    for i in 1..=folds {
        let (training, testing) = data::get_data_fold(i-1, folds, &training);
        accuracy.push(test(&prob, &testing));
        println!("  Accuracy Fold {}: {:.2}%", i, accuracy.last().unwrap() * 100.0);
    }
    println!("");

    let avg = accuracy.clone().into_iter().sum::<f64>();
    println!("  Average Accuracy: {:.2}%", avg / folds as f64 * 100.0);
    println!("  Standard Deviation: {:.2}%", (accuracy.into_iter().map(|x| (x - avg).powi(2)).sum::<f64>() / folds as f64).sqrt());

    println!("");
    println!("2. Test Set Accuracy:");
    println!("   Accuracy: {:.2}%", test(&prob, &testing) * 100.0);
}