【问题标题】:Grading using weighted averages in php在php中使用加权平均值评分
【发布时间】:2016-07-11 14:57:20
【问题描述】:

我如何用 php 给这个测试评分?我需要一个百分比分数...

我有一系列问题,其中包含正确/不正确的布尔值和相应的权重。

我需要先找到正确答案的平均值吗?

等式是什么?

$questions = array(
    0=>array(
        'Question'=>"Some Question",
        'Correct'=>true,
        'Weight'=>5,
    ),
    1=>array(
        'Question'=>"Some Question",
        'Correct'=>false,
        'Weight'=>5,
    ),
    2=>array(
        'Question'=>"Some Question",
        'Correct'=>true,
        'Weight'=>4,
    ),
    3=>array(
        'Question'=>"Some Question",
        'Correct'=>true,
        'Weight'=>0,
    ),
    4=>array(
        'Question'=>"Some Question",
        'Correct'=>false,
        'Weight'=>5,
    ),
    5=>array(
        'Question'=>"Some Question",
        'Correct'=>true,
        'Weight'=>4,
    ),
);
$weights = array(
    0=>0
    1=>0
    2=>.05
    3=>.20
    4=>.25
    5=>.50
);
$totalQuestions=0;
$correctAnswers=0;
$points=0;
foreach($questions as $question){
    $totalQuestions++;
    if($question['Correct']){
        $correctAnswers++;
        $points = $points = $weights[$question['Weight'];
    }
}

【问题讨论】:

    标签: php weighted-average


    【解决方案1】:

    您可以计算候选人获得的权重(即您拥有的分数),然后计算可能的总权重(即满分)。

    然后你可以将候选分数除以总分:

    分数 = 候选人分数 / 总分

    从那里你可以计算百分比:

    百分比 = 分数 * 100

    使用您的代码:

    $totalQuestions=0;
    $totalWeights=0;
    $correctAnswers=0;
    $weightsEarned=0;
    foreach($questions as $question){
        $totalQuestions++;
        $totalWeights+=$weights[$question['Weight']];
        if($question['Correct']){
            $correctAnswers++;
            $weightsEarned += $weights[$question['Weight']];
        }
    }
    
    echo "Score Overview: ";
    echo "<br/>Weights Earned: " . $weightsEarned;
    echo "<br/>Correct Answers: " . $correctAnswers;
    echo "<br/>Total Weights Possible : " . $totalWeights;
    echo "<br/>Percentage Earned: " . ($weightsEarned / $totalWeights) * 100;
    

    【讨论】:

      【解决方案2】:

      通常情况下,平均值(加权或不加权)是所有可能事物的总和。如果它是加权的,这通常意味着每个事物不是 1 事物,而是实际上是 weightOfThing 事物。

      例子:

      $totalQuestions = count($questions); //No need to increment
      $totalWeight = 0; //Could do a sum here but no need
      $weightedSum = 0;
      foreach($questions as $question){
          $totalWeight += isset($question["Weight"])?$question["Weight"]:0; //Assume a question with no weight has 0 weight, i.e., doesn't count. Adjust accordingly
          if($question['Correct']){
              $weightedSum += isset($question["Weight"])?$question["Weight"]:0;
          }
      }
      $weightedAverage = $weightedSum / $totalWeight;
      

      【讨论】:

        【解决方案3】:

        可以优化,但这里是完整的方程式:

        $weights = array(
            0=>0,
            1=>0,
            2=>.05,
            3=>.20,
            4=>.25,
            5=>.50,
        );
        
        $byWeight = array();
        foreach($questions as $question){
            //$totalQuestions++;
            $byWeight[$question['Weight']]['TotalNumberOfQuestionsForWeight']++;
            if($question['Correct']){
                $byWeight[$question['Weight']]['CorrectAnswers']++;
            }
        }
        
        $totalWeightsSum = 0;
        foreach($byWeight as $weight => $data){
             $totalWeightsSum = $totalWeightsSum + (($data['CorrectAnswers'] / $data['TotalNumberOfQuestionsForWeight']) * $weights[$weight]);
        }
        
        echo '<pre>'.var_export($byWeight,1).'</pre>';
        echo $totalWeightsSum / array_sum($weights);
        

        【讨论】:

          猜你喜欢
          • 2013-07-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-12-03
          • 1970-01-01
          • 2013-03-31
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多