AI-Perceptron-Simple

 view release on metacpan or  search on metacpan

t/08-confusion_matrix.t  view on Meta::CPAN

{
    local $@;
    eval {
        $perceptron->get_confusion_matrix( { 
            full_data_file => NON_BINARY_FILE,
            actual_output_header => "brand",
            predicted_output_header => "predicted",
        } );
    };

    like ( $@, qr/Something\'s wrong\!/, "Croaked! Found non-binary values in file");
}

my $piece;
my @pieces = ('A: ', 'P: ', 'actual', 'predicted', 'entries', 'Accuracy', 'Sensitivity', 'MP520', 'Yi Lin');

for $piece ( @pieces ) {
    stdout_like {
    
        ok ( $perceptron->display_exam_results( \%c_matrix, { zero_as => "MP520", one_as => "Yi Lin"  } ),
            "display_exam_results is working");
        
    } qr /(?:$piece)/, "$piece displayed";

}

{
    local $@;
    
    eval {
        $perceptron->display_confusion_matrix( \%c_matrix, { one_as => "Yi Lin" } );
    };
    
    like ( $@, qr/zero_as/, "Missing keys found: zero_as!" );
    unlike ( $@, qr/one_as/, "Confirmed one_as is present but not zero_as" );
}

{
    local $@;
    
    eval {
        $perceptron->display_confusion_matrix( \%c_matrix, { zero_as => "MP520" } );
    };
    
    like ( $@, qr/one_as/, "Missing keys found: one_as!" );
    unlike ( $@, qr/zero_as/, "Confirmed zero_as is present but not one_as" );
}

{
    local $@;
    
    eval {
        $perceptron->display_confusion_matrix( \%c_matrix );
    };
    
    like ( $@, qr/zero_as one_as/, "Both keys not found" );
}

# more_stats enabled

subtest "More stats" => sub {

    my %c_matrix_more_stats = $perceptron->get_confusion_matrix( { 
            full_data_file => TEST_FILE, 
            actual_output_header => "brand",
            predicted_output_header => "predicted",
            more_stats => 1,
        } );

    like ( $c_matrix_more_stats{ precision }, qr/66.66/, "Precision seems correct to me" );
    is ( $c_matrix_more_stats{ specificity }, 80, "Specificity seems correct to me" );
    is ( $c_matrix_more_stats{ F1_Score }, 50, "F1 Score seems correct to me" );
    like ( $c_matrix_more_stats{ negative_predicted_value }, qr/57.142/, "Negative Predicted Value seems correct to me" );
    is ( $c_matrix_more_stats{ false_negative_rate }, 60, "False Negative Rate seems correct to me" );
    is ( $c_matrix_more_stats{ false_positive_rate }, 20, "False positive Rate seems correct to me" );
    like ( $c_matrix_more_stats{ false_discovery_rate }, qr/33.33/, "False Discovery Rate seems correct to me" );
    like ( $c_matrix_more_stats{ false_omission_rate }, qr/42.85/, "False Omission Rate seems correct to me" );
    is ( $c_matrix_more_stats{ balanced_accuracy }, 60, "Balanced Acuracy seems correct to me" );


    my $piece;
    my @pieces = ('A: ', 'P: ', 'actual', 'predicted', 'entries', 'Accuracy', 'Sensitivity', 'MP520', 'Yi Lin', "Precision", "Specificity", "F1 Score", "Negative Predicted Value", "False Negative Rate", "False Positive Rate", "False Discovery Rate", ...

    for $piece ( @pieces ) {
        stdout_like {
        
            ok ( $perceptron->display_exam_results( \%c_matrix_more_stats, { zero_as => "MP520", one_as => "Yi Lin"  } ),
                "display_exam_results is working");
            
        } qr /(?:$piece)/, "$piece displayed";

    }
    $perceptron->display_exam_results( \%c_matrix_more_stats, { 
        zero_as => "MP520", 
        one_as => "Yi Lin"  } );
};

done_testing;

# besiyata d'shmaya






( run in 1.638 second using v1.01-cache-2.11-cpan-39bf76dae61 )