Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中使用加权平均值进行分级_Php_Weighted Average - Fatal编程技术网

在php中使用加权平均值进行分级

在php中使用加权平均值进行分级,php,weighted-average,Php,Weighted Average,我如何用php给这个测试打分?我需要一个百分比分数 我有一系列问题,包括正确/不正确的bool和相应的权重 我需要先找到正确答案的平均值吗 方程式是什么 $questions = array( 0=>array( 'Question'=>"Some Question", 'Correct'=>true, 'Weight'=>5, ), 1=>array( 'Question'=&

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

我有一系列问题,包括正确/不正确的bool和相应的权重

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

方程式是什么

$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'];
    }
}
通常,平均值(加权或非加权)是所有可能的事物的总和。若它是加权的,那个通常意味着每件事物不是一件事物,但实际上是事物的权重

例如:

$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;
通常,平均值(加权或非加权)是所有可能的事物的总和。若它是加权的,那个通常意味着每件事物不是一件事物,但实际上是事物的权重

例如:

$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;

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

然后,您可以将候选人得分除以总分:

得分=候选人得分/总分

从中可以计算百分比:

百分比=分数*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;
$totalQuestions=0;
$totalWeights=0;
$correctAnswers=0;
$weightsEarned=0;
foreach($问题作为$问题){
$totalQuestions++;
$totalWeights+=$weights[$question['Weight']];
如果($问题['Correct'])){
$correctAnswers++;
$weightsarned+=$weights[$question['Weight']];
}
}
回声“分数概述:”;
echo“
赢得的权重:”$重量变重; 回声“
正确答案:”$正确答案; echo“
可能的总重量:”$总重量; echo“
获得的百分比:”。($weightserned/$totalWeights)*100;
您可以计算候选人获得的权重(即您的分数),然后计算可能的总权重(即满分)

然后,您可以将候选人得分除以总分:

得分=候选人得分/总分

从中可以计算百分比:

百分比=分数*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;
$totalQuestions=0;
$totalWeights=0;
$correctAnswers=0;
$weightsEarned=0;
foreach($问题作为$问题){
$totalQuestions++;
$totalWeights+=$weights[$question['Weight']];
如果($问题['Correct'])){
$correctAnswers++;
$weightsarned+=$weights[$question['Weight']];
}
}
回声“分数概述:”;
echo“
赢得的权重:”$重量变重; 回声“
正确答案:”$正确答案; echo“
可能的总重量:”$总重量; echo“
获得的百分比:”。($weightserned/$totalWeights)*100;
可以优化,但以下是完整的等式:

$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);
$weights=数组(
0=>0,
1=>0,
2=>.05,
3=>.20,
4=>.25,
5=>.50,
);
$byWeight=array();
foreach($问题作为$问题){
//$totalQuestions++;
$byWeight[$question['Weight']['TotalNumberOfQuestionsForWeight']++;
如果($问题['Correct'])){
$byWeight[$question['Weight']['CorrectAnswers']++;
}
}
$totalWeightsSum=0;
foreach($weight作为$weight=>$data按重量计算){
$TotalWeightsum=$TotalWeightsum+($data['CorrectAnswers']/$data['TotalNumberOfQuestionsOverweight'])*$weights[$weight]);
}
echo.''var_导出($按重量,1)。'';
echo$totalweightsum/array\u和($weights);

可以优化,但以下是完整的等式:

$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);
$weights=数组(
0=>0,
1=>0,
2=>.05,
3=>.20,
4=>.25,
5=>.50,
);
$byWeight=array();
foreach($问题作为$问题){
//$totalQuestions++;
$byWeight[$question['Weight']['TotalNumberOfQuestionsForWeight']++;
如果($问题['Correct'])){
$byWeight[$question['Weight']['CorrectAnswers']++;
}
}
$totalWeightsSum=0;
foreach($weight作为$weight=>$data按重量计算){
$TotalWeightsum=$TotalWeightsum+($data['CorrectAnswers']/$data['TotalNumberOfQuestionsOverweight'])*$weights[$weight]);
}
echo.''var_导出($按重量,1)。'';
echo$totalweightsum/array\u和($weights);