gpt4 book ai didi

php - 价格过滤分组算法

转载 作者:可可西里 更新时间:2023-11-01 12:36:40 25 4
gpt4 key购买 nike

我正在创建一个电子商务网站,但我在开发一个好的算法来对从数据库中提取的产品进行适当的分组排序时遇到了问题。我试过简单地将最高价格分成 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;
}
}

最佳答案

这是一个想法:基本上您会将价格分成 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));

从结果中,可以使用reset和end得到每个bucket的min/max

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

关于php - 价格过滤分组算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3304407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com