[英]Why doesn't my testing script (PHP) work?
我有一个包含推文的数据库。 此外,我将这些推文归类为“负面”,“中立”或“正面”。 我是手动完成的,现在尝试根据朴素贝叶斯分类器来计算计算机对它们进行分类的程度。
为了测试分类的准确性(计算机分类的推文数量与我手动划分的总量相同),已编写了脚本。
但是,我在使用此PHP脚本时遇到了问题。 运行它时,出现错误“在C:\\ wamp \\ -on-on中被零除”。 这可能是由于计数器未更新的事实。 此外,“正确类别”的数量似乎也没有更新。 这两个部分至关重要,因为准确性的公式是:“正确的类别”除以“计数器”。
我的问题是:查看脚本时,您认为问题出在哪里? 我怎么可能修复它?
测试脚本:
$test_array = array();
$counter = 0;
$timer1 = microtime(true);
$right_classes = 0;
foreach ($test_set as $test_item) {
$tweet_id = $test_item['tweet_id'];
$class_id_shouldbe = $test_item['class_id'];
$tweet = Tweets::loadOne($tweet_id);
// # Preprocess if not done already
// $steps->processTweet($tweet_id, $tweet);
// $tweet = Tweets::loadOne($tweet_id);
if ((int) $tweet['classified'] > 0 || !$tweet['valid']) continue;
if (strlen($tweet['processed_text']) == 0) {
$steps->processTweet($tweet_id, $tweet);
$tweet = Tweets::loadOne($tweet_id);
if (strlen($tweet['processed_text']) == 0) {
echo "Kon tweet '$tweet_id' niet processen. <br>";
continue;
}
}
$class_id = $classifier->classify($tweet['processed_text']);
# Add tweets in database
// Tweets::addClassId($tweet_id, $class_id_shouldbe);
$test_array[$tweet_id] = array(
'what_human_said' => $class_id_shouldbe,
'what_classifier_said' => $class_id,
);
if ($class_id_shouldbe == $class_id) $right_classes++;
$counter++;
if ($counter > 936) break;
echo "$tweet_id,$class_id_shouldbe,$class_id<br>";
}
$timer2 = microtime(true);
echo '<br><br>klaar in '.round($timer2-$timer1, 3).' sec<br>';
echo ($right_classes/$counter)*100 .' %';
exit();
首先只是修复错误,然后尝试验证为什么$counter
为零。 要修复$counter
只需在除法之前进行验证:
if($counter!=0) echo ($right_classes/$counter)*100 .' %'; else echo '0 %';
然后查看您的代码,使用continue在foreach
获取下一个项目,然后不能保证达到$counter
,然后得到Division by zero
错误。
希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.