具有 PHP-ML 和回归的推荐引擎
Recommendation Engine with PHP-ML and regression
当我想向当前客户推荐一些商品时,我试图了解如何使用 PHP-ML。
我的数据集(数值只是行数):
- 产品 1 是与产品 2 一起购买的
- 产品 1 是与产品 2 一起购买的
- 产品 1 是与产品 3 一起购买的
- 产品 1 是与产品 2 一起购买的
- 产品 2 是与产品 4 一起购买的
- 产品 Y.. 与产品 X.. 一起购买
作为客户,我过去购买过产品 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。然后我可以订购我的预测并将订单放入我的推荐框中。
我的主要问题是,我应该用什么做火车?我理解完全错误的东西吗?
谢谢
首先你在这里不需要线性回归,如果你需要它你会
必须转换分类数据才能进行数字预测。
通常您会使用虚拟变量,这意味着您的 table 将从:
| 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!
此外,当从事机器学习工作时,将数据拆分为所谓的训练很有用
和测试集。这样您就可以直接测试您的 ML 模型。 It is also implemented in PHP-ML
当我想向当前客户推荐一些商品时,我试图了解如何使用 PHP-ML。
我的数据集(数值只是行数):
- 产品 1 是与产品 2 一起购买的
- 产品 1 是与产品 2 一起购买的
- 产品 1 是与产品 3 一起购买的
- 产品 1 是与产品 2 一起购买的
- 产品 2 是与产品 4 一起购买的
- 产品 Y.. 与产品 X.. 一起购买
作为客户,我过去购买过产品 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。然后我可以订购我的预测并将订单放入我的推荐框中。 我的主要问题是,我应该用什么做火车?我理解完全错误的东西吗?
谢谢
首先你在这里不需要线性回归,如果你需要它你会 必须转换分类数据才能进行数字预测。 通常您会使用虚拟变量,这意味着您的 table 将从:
| 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!
此外,当从事机器学习工作时,将数据拆分为所谓的训练很有用 和测试集。这样您就可以直接测试您的 ML 模型。 It is also implemented in PHP-ML