价格过滤分组算法

Dav*_* W. 8 php algorithm statistics e-commerce

我正在创建一个电子商务网站,我无法开发一个好的算法来将从数据库中提取的产品分类到适当的中间组.我试过简单地将最高价格划分为4,然后将每个组别除以此.我也尝试了基于均值的标准偏差.两者都可能产生没有产品落入的价格范围,这不是一个有用的过滤选项.

我也尝试过四分之一的产品,但我的问题是价格从1美元到4,000美元不等.4,000美元几乎从未出售过,而且重要性要低得多,但他们的结果却在不断下滑.

有什么想法吗?我应该在统计课上更加注意......

更新:

我最后结合了一些方法.我使用了四分位数/桶方法,但通过硬编码某些范围来破解它,在这些范围内会出现更多的价格组.

//Price range algorithm

sort($prices);

//Divide the number of prices into four groups
$quartilelength = count($prices)/4;

//Round to the nearest ...
$simplifier = 10;

//Get the total range of the prices
$range = max($prices)-min($prices);

//Assuming we actually are working with multiple prices
if ($range>0 )
{
    // If there is a decent spread in price, and there are a decent number of prices, give more price groups
    if ($range>20 && count($prices) > 10) 
    {
        $priceranges[0] = floor($prices[floor($quartilelength)]/$simplifier)*$simplifier;
    }

    // Always grab the median price
    $priceranges[1] = floor($prices[floor($quartilelength*2)]/$simplifier)*$simplifier;

    // If there is a decent spread in price, and there are a decent number of prices, give more price groups
    if ($range>20 && count($this->data->prices) > 10)
    {
        $priceranges[2] = floor($prices[floor($quartilelength*3)]/$simplifier)*$simplifier;
    }
}
Run Code Online (Sandbox Code Playgroud)

tsg*_*ser 3

这里有一个想法:基本上,您可以将价格分为 10 个桶,每个价格作为数组中的键,该值是给定价格点有多少产品的计数:

public function priceBuckets($prices)
{    
    sort($prices);

    $buckets = array(array());
    $a = 0;

    $c = count($prices);
    for($i = 0; $i !== $c; ++$i) {
        if(count($buckets[$a]) === 10) {
            ++$a;
            $buckets[$a] = array();
        }

        if(isset($buckets[$a][$prices[$i]])) {
            ++$buckets[$a][$prices[$i]];
        } else if(isset($buckets[$a - 1][$prices[$i]])) {
            ++$buckets[$a - 1][$prices[$i]];
        } else {
            $buckets[$a][$prices[$i]] = 1;
        }
    }

    return $buckets;
}

//TEST CODE
$prices = array();

for($i = 0; $i !== 50; ++$i) {
    $prices[] = rand(1, 100);
}
var_dump(priceBuckets($prices));
Run Code Online (Sandbox Code Playgroud)

从结果中,您可以使用重置和结束来获取每个桶的最小值/最大值

有点蛮力,但可能有用......