bolter Posted August 8, 2010 Share Posted August 8, 2010 Ok I guess this is more a algorithm problem than a php problem, but still I hope someone can help me. I'm trying to write some code that starting from two arrays containing observed and expected values will calculate true and false positive and negative rates of a certain event. Let's say I have 30 events and 5 of them are really red while 25 are blue. (each event is identified by an unique id i.e. number) An observer is required to evaluate the 30 events and answer if they are red or blue, their answer (only if blue if stored in the observed array - in the form of a position id) So in the end I have one array that has the observed red events i.e. 4 56 78 44 90 and the expected array with the actual red events i.e. 4 78 33 34 In this case my true positive would be 2 (4 and 78 that are both in the observed and the expected), my false positive would be 3 (observed but not expected), my false negative 2 (expected but not observed) and my true negative 23 (not expected and not observed, obtained substracting the previous three from the total number of events) How can I calculate this four values using PHP, I thought it was easier but as I started to program it I realized I didn't know where to go Thanks to all! Quote Link to comment https://forums.phpfreaks.com/topic/210161-calculating-true-and-false-positives-and-negatives/ Share on other sites More sharing options...
jcbones Posted August 9, 2010 Share Posted August 9, 2010 Try along these lines. <?php $redObserved = array(4, 56, 78, 44, 90); $redExpected = array(4, 78, 33, 34); $redPositive = count(array_intersect($redObserved,$redExpected)); echo $redPositive; ?> Quote Link to comment https://forums.phpfreaks.com/topic/210161-calculating-true-and-false-positives-and-negatives/#findComment-1096843 Share on other sites More sharing options...
bolter Posted August 9, 2010 Author Share Posted August 9, 2010 Thanks, I will try that one, altought that will give me only the true positive, I still need to figure out how to get the false positive and the false negatives, thanks Quote Link to comment https://forums.phpfreaks.com/topic/210161-calculating-true-and-false-positives-and-negatives/#findComment-1096920 Share on other sites More sharing options...
AbraCadaver Posted August 9, 2010 Share Posted August 9, 2010 $truePositive = count(array_intersect($redObserved,$redExpected)); $falsePositive = count(array_diff($redObserved, $redExpected)); $falseNegative = count(array_diff($redExpected, $redObserved)); $trueNegative = $totalEvents - ($truePositive + $falsePositive + $falseNegative); Quote Link to comment https://forums.phpfreaks.com/topic/210161-calculating-true-and-false-positives-and-negatives/#findComment-1097045 Share on other sites More sharing options...
bolter Posted August 9, 2010 Author Share Posted August 9, 2010 thanks, this was very helpful! Quote Link to comment https://forums.phpfreaks.com/topic/210161-calculating-true-and-false-positives-and-negatives/#findComment-1097162 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.