如何在PHP中找到数组的模式 [英] How to find the mode of an array in PHP

查看:46
本文介绍了如何在PHP中找到数组的模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从低到高排序的数组,其中包含超过 260k 个值.我已经找到了数组的均值(平均值)和中位数,只需要找出众数?

I have an array that has been sorted from low to high which has over 260k values in. I have found out the mean(average) and median of the array just need to find out the mode?

我不能使用 PHP 的任何数学函数,必须全部手动完成.

I cannot use any mathematical functions that PHP has, it has to be all done manually.

我希望它可以只有一个值作为模式,但可以有多个值作为模式.我还需要能够记录存储值的次数.例如,数字 51 出现了 6 次,所以我可以打印这两个值.

I would like it so there could be just one value that is the mode but then there can be multiple values that can be the mode. I also need to be able to record the number of times that the value is stored. For example the number 51 appears 6 times so I can print both values.

这是我目前的代码:

$amountRecords = 0;
$totalValue = 0;
$valueArray = array();

// reads in csv file
$handle = fopen('Task1-DataForMeanMedianMode.csv', 'r');
// to skip the header names/values
fgetcsv($handle);

// creates array containing variables of csv file in ascending order
while(($row = fgetcsv($handle, "\r")) != FALSE)
{

    // if amountRecords equals 0
    if($amountRecords == 0)
    {

        // adds value from csv to array
        $valueArray[] = $row[1];

    } // else amountRecords does not equal 0
    else 
    {

        // if the value in array location before is greater than the current value from the csv file
        if($valueArray[$amountRecords - 1] > $row[1])
        {

             // the current array location becomes the one in the location before
             $valueArray[] = $valueArray[$amountRecords - 1];
             // add the value from the csv to the location before
             $valueArray[$amountRecords - 1] = $row[1];

         } // if the value in the location before is not greater than the current value in the csv file
         else 
         {

             // adds value from csv to array
             $valueArray[] = $row[1];

         }

    }

    // calculates the total value of the values in csv file
    $totalValue = $totalValue + $row[1];
    // calculates the number of values in the csv file
    $amountRecords++;

}    

// calculate average value of payments
$averageValue = $totalValue / $amountRecords;
// limit integer to 2 decimal place
$average = number_format($averageValue,2,'.','');

// finds middle value
$middle = floor(($amountRecords / 2) - 1);

// calculates the median value
// if array size is even
if($amountRecords % 2 == 0)
{

    // calculates median
    $median = $valueArray[$middle];

} 
else // if array size is odd
{

    // calculate low and high values
    $low = $valueArray[$middle];
    $high = $valueArray[$middle + 1];
    // calculates median
    $median = (($low + $high) / 2);

}

// works out mode
// creates array count
$count = array();
// for each value in the valueArray
foreach( $valueArray as $value )
{

    if( isset( $count[$value] ))
    {

        $count[$value]++;

    }
    else
    {

        $count[$value] = 1;

    }

}

$mostCommon = "";
$iter = 0;

foreach( $count as $k => $v )
{

     if( $v > $iter )
     {

         $mostCommon = $k;
         $iter = $v;

     }

}

$modeArray = array( "mode" => $mostCommon , "count" => $iter );

推荐答案

数值集合的众数是最常出现的数.您可以使用类似于以下代码的 PHP 执行此操作:

The mode of a numerical set is the number that occurs the most often. You can do this with PHP using code similar to the following:

$values = array_count_values($valueArray); 
$mode = array_search(max($values), $values);

这篇关于如何在PHP中找到数组的模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆