Я строю набор функций для статистического анализа в PHP. Эта функция занимается поиском режима или режимов в массиве. Иногда я могу быть художником спагетти, поскольку я просто энтузиаст. Я ищу способы сделать это чище или труднее, или использовать любые встроенные функции, о которых я не знаю. Благодарю.
function mode( array $list )
{
// mode - For lists, the mode is a most common (frequent) value. A list can have more than one mode. For histograms, a mode is a relative maximum ("bump").
// this function can accept arrays of numbers and strings but not nested arrays.
$arrayCountValues = array_count_values($list);
arsort( $arrayCountValues );
reset( $arrayCountValues );
$firstKey = key( $arrayCountValues );
$arrayCountValueDulpicates = array();
$arrayCountValueDulpicatesListKeys = array();
foreach ( $arrayCountValues as $key => $value )
{
if( $arrayCountValues[$firstKey] == $value )
{
$arrayCountValueDulpicates[] = $key;
// echo 'key ' . $key . ' value ' . $value . PHP_EOL;
}
}
if( count( $arrayCountValueDulpicates ) != count( $list ) )
{
for ( $i=0; $i < count( $arrayCountValueDulpicates ) ; $i++ )
{
$temp = array();
foreach ($list as $key => $value)
{
if ( $value == $arrayCountValueDulpicates[$i] )
{
array_push( $temp, $key );
$arrayCountValueDulpicatesListKeys[$i] = $temp;
// echo 'key ' . $key . ' value ' . $value . PHP_EOL;
}
}
}
$modes = array( 'modetype' => count( $arrayCountValueDulpicates ), 'modes' => $arrayCountValueDulpicates, 'observation' => count($arrayCountValueDulpicatesListKeys[0]), 'listkeys' => $arrayCountValueDulpicatesListKeys );
}
else
{
$modes = array( 'modetype' => 0, 'modes' => array(), 'observation' => 0, 'listkeys' => array() );
}
return $modes;
// return array ( 'modetype' => 'integer number of modes', 'modes' => array( 'mode values' ), 'observation' => 'integer count of mode observed' , 'listkeys' => array( 'original lists keys that match mode values' ) );
}
Пример данных:
$numbers = array( 77, 32, 40, 100, 33, 98, 58, 63, 12, 35, 82, 61, 89, 70, 68, 24, 94, 73, 19, 18, 12, 35, 55, 96, 93, 37, 4, 77, 29, 47, 13, 63, 11, 84, 89, 6, 1, 63, 94, 36, 33, 1, 26, 18, 3, 41, 48, 99, 71, 37, 10, 69, 86, 46, 52, 53, 45, 49, 50, 97, 73, 18, 23, 39, 33, 53, 36, 52, 33, 31, 82, 43, 9, 11, 50, 46, 81, 35, 70, 77, 93, 50, 96, 42, 17, 40, 19, 45, 57, 36, 50, 20, 98, 96, 7, 80, 39, 30, 49, 24 );
$words = array( 'bake' => 'memorise', 'pets' => 'harmony', 'rampant' => 'capricious', 'rose' => 'hurry', 'shame' => 'craven', 'soak' => 'title', 'white' => 'shiver', 'example' => 'amusing', 'quarrelsome' => 'endurable', 'willing' => 'punish', 'neat' => 'coherent', 'tax' => 'learned', 'smooth' => 'title', 'boil' => 'aromatic', 'clumsy' => 'dolls', 'fear' => 'enter', 'zoom' => 'sleep', 'simple' => 'geese', 'turn' => 'title', 'pink' => 'whip', 'guiltless' => 'mice', 'thoughtless' => 'delicious', 'exotic' => 'oafish', 'capricious' => 'learned', 'action' => 'polish', 'show' => 'drum', 'choke' => 'title', 'encouraging' => 'brash', 'trust' => 'close', 'stimulating' => 'drum', 'ragged' => 'paper', 'dad' => 'line', 'careful' => 'spiteful', 'tedious' => 'history', 'bikes' => 'drum', 'shelter' => 'wrist', 'chalk' => 'hammer', 'grade' => 'part', 'anxious' => 'drum', 'nifty' => 'stem', 'appreciate' => 'attractive', 'crawl' => 'border', 'nest' => 'harmonious', 'mother' => 'abounding', 'quack' => 'spotty', 'undesirable' => 'crabby', 'tightfisted' => 'voice', 'supply' => 'acoustic', 'tick' => 'powder', 'expect' => 'need' );
Пример использования
print_r(mode($numbers));
print_r(mode($words));
Функция выхода:
Array
(
[modetype] => 2
[modes] => Array
(
[0] => 50
[1] => 33
)
[observation] => 4
[listkeys] => Array
(
[0] => Array
(
[0] => 58
[1] => 74
[2] => 81
[3] => 90
)
[1] => Array
(
[0] => 4
[1] => 40
[2] => 64
[3] => 68
)
)
)
Array
(
[modetype] => 2
[modes] => Array
(
[0] => drum
[1] => title
)
[observation] => 4
[listkeys] => Array
(
[0] => Array
(
[0] => show
[1] => stimulating
[2] => bikes
[3] => anxious
)
[1] => Array
(
[0] => soak
[1] => smooth
[2] => turn
[3] => choke
)
)
)