[英]Recommendation Engine with PHP-ML and regression
当我想向当前客户推荐一些项目时,我会尝试找出如何使用PHP-ML 。
我的数据集(编号只是行数):
作为客户,我在过去购买了产品 1。所以通常我会在我的推荐框中期望产品 2,因为 3 个人与产品 1 一起购买了它。
我想我在这里需要一些回归算法,它给我产品 X 和产品 Y 之间的一些相关值。
我想过线性 SVR 算法,但我不知道如何训练它?
// Step 1: Load the Dataset
// Step 2: Prepare the Dataset
// Step 3: Generate the training/testing Dataset
$samples = [[1,2], [1,2], [1,3], [1,2], [2,4], [X,Y..]];
$targets = [?, ?, ? , ? , ? , ?];
$regression = new LeastSquares();
// Step 4: Train the classifier
$regression->train($samples, $targets);
echo $regression->predict([1,2]);
在我看来,我应该得到一些价值,例如 0.25 -> 25% 购买产品 1 的客户也购买了产品 2。然后我可以订购我的预测并将订单放入我的推荐框中。 我的主要问题是,我应该用什么火车? 我是否理解完全错误?
谢谢
首先,这里不需要线性回归,如果需要, 则必须转换分类数据才能进行数字预测。 通常您会使用虚拟变量,这意味着您的表将从以下内容转换:
| Product A | Product B |
|-----------|-----------|
| 1 | 2 |
| 1 | 2 |
| 1 | 3 |
| 1 | 2 |
| 2 | 4 |
类似于:
| Product 1 | Product 2 | Product 3 | Product 4 |
|------------|-----------|-----------|-----------|
| 1 | 1 | 0 | 0 |
| 1 | 1 | 0 | 0 |
| 1 | 0 | 1 | 0 |
| 1 | 1 | 0 | 0 |
| 0 | 1 | 0 | 1 |
有关更多信息,请参阅https://datascience.stackexchange.com/questions/28306/transform-categorical-variables-into-numerical 。 遗憾的是,我认为 PHP-ML 目前不支持分类数据编码。 如果你不转换分类数据,你可能会得到 1.6 作为预测,这并不意味着任何有用的东西。
但是在 PHP-ML 中有一种更简单的方法可以做到这一点。 您可以使用 Apriori 关联器。 这可以了解哪些关联更频繁并预测它们。 在下面你可以看到它的作用。
use Phpml\Association\Apriori;
$samples = [[1,2], [1,2], [1,3], [1,2], [2,4]];
$labels = [];
$associator = new Apriori($support = 0.5, $confidence = 0.5);
$associator->train($samples, $labels);
var_export($associator->predict([1]));
// outputs [[ 2 ]]; The right prediction!
此外,在使用机器学习时,将数据拆分为所谓的训练集和测试集很有用。 这样你就可以直接测试你的机器学习模型。 它也是在 PHP-ML 中实现的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.