简体   繁体   English

带有 PHP-ML 和回归的推荐引擎

[英]Recommendation Engine with PHP-ML and regression

I try to find out how to work with PHP-ML when i want to recommend some items to current customer.当我想向当前客户推荐一些项目时,我会尝试找出如何使用PHP-ML

My dataset (numeration is only the number of the row):我的数据集(编号只是行数):

  1. Product 1 was purchased together with Product 2产品 1 与产品 2 一起购买
  2. Product 1 was purchased together with Product 2产品 1 与产品 2 一起购买
  3. Product 1 was purchased together with Product 3产品 1 与产品 3 一起购买
  4. Product 1 was purchased together with Product 2产品 1 与产品 2 一起购买
  5. Product 2 was purchased together with Product 4产品 2 与产品 4 一起购买
  6. Product Y.. was purchased together with Product X..产品 Y.. 与产品 X.. 一起购买

As a customer i had bought in the past Product 1. So normally i would expect in my recommendation box product 2 because 3 people bought it together with product 1.作为客户,我在过去购买了产品 1。所以通常我会在我的推荐框中期望产品 2,因为 3 个人与产品 1 一起购买了它。

I think i need here some regression algorythm which give me some correlation value between product X and product Y.我想我在这里需要一些回归算法,它给我产品 X 和产品 Y 之间的一些相关值。

I thought about the linear SVR algorythm but i have no idea how to train it?我想过线性 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]);

In my mind i should get some value like 0.25 -> 25% percent of customers who bought product 1 also bought product 2. Then i could order my predictions and have the order in my recommendation box.在我看来,我应该得到一些价值,例如 0.25 -> 25% 购买产品 1 的客户也购买了产品 2。然后我可以订购我的预测并将订单放入我的推荐框中。 My main question is, what should i use for train?我的主要问题是,我应该用什么火车? Do I understand something completely wrong?我是否理解完全错误?

Thank you谢谢

First of all you don't need linear regression here and if you needed that you would have to convert the categorical data in order to do a numeric prediction .首先,这里不需要线性回归,如果需要, 则必须转换分类数据才能进行数字预测 Typically you would use dummy variables, that means that your table would convert from:通常您会使用虚拟变量,这意味着您的表将从以下内容转换:

| Product A | Product B |
|-----------|-----------|
|         1 |         2 |
|         1 |         2 |
|         1 |         3 |
|         1 |         2 |
|         2 |         4 |

to something like :类似于:

| 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 |

See https://datascience.stackexchange.com/questions/28306/transform-categorical-variables-into-numerical for more info.有关更多信息,请参阅https://datascience.stackexchange.com/questions/28306/transform-categorical-variables-into-numerical Sadly I think PHP-ML does not have support for categorical data encoding at this moment.遗憾的是,我认为 PHP-ML 目前不支持分类数据编码。 If you don't convert the categorical data you would get maybe 1.6 as a prediction, at that wouldn't mean anything useful.如果你不转换分类数据,你可能会得到 1.6 作为预测,这并不意味着任何有用的东西。

But there is an easier way to do this in PHP-ML.但是在 PHP-ML 中有一种更简单的方法可以做到这一点。 You can use an Apriori associator.您可以使用 Apriori 关联器。 That can learn which associations are more frequent and predict them.这可以了解哪些关联更频繁并预测它们。 In the following you can see that in action.在下面你可以看到它的作用。

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!

In adition when working in machine learning is useful to split your data into what is called the training and the test set.此外,在使用机器学习时,将数据拆分为所谓的训练集和测试集很有用。 That way you can directly test your ML model.这样你就可以直接测试你的机器学习模型。 It is also implemented in PHP-ML 它也是在 PHP-ML 中实现的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM