feraiur/recommender_system_php

PHP语言的协同推荐过滤和Apriori规则关联

dev-master 2020-08-13 16:10 UTC

This package is auto-updated.

Last update: 2024-09-14 01:17:25 UTC


README

PHP推荐器是一个库,允许根据

  • 协同过滤。
  • Apriori关联规则。

如何使用

使用composer包含库,然后使用以下示例

include __DIR__.'/vendor/autoload.php';

use feraiur\recommender_system_php\Predictor;
use feraiur\recommender_system_php\AssociationRules;


// Collaborative Filering

$predictor = new Predictor();
$dataseRatings = [
                [1, 1, 3],
                [2, 2, 5],
                [3, 3, 5],
                [4, 4, 5],
                [5, 5, 5],
                [2, 1, 2],
                [3, 1, 2],
                [4, 1, 2],
                [5, 1, 2],
              ];

try {
    $predictor->setDataset($dataseRatings);
    $recommendations = $predictor->predict();
} catch(Exception $e) {
    var_dump($e->getMessage());
    $recommendations = [];
}


echo("<h3>Recommendations:</h3>");
echo("<p>Some recommendations for users about items.</p>");
var_dump($recommendations);


// Association Rules

$associationRules = new AssociationRules(20, 50);
$datasetTransactions = [
                [1, 3],
                [3, 1],
                [2, 3],
                [3, 4],
                ["i1", 4],
                ["i2", 4],
                [1, 4],
                ["i2", 3],
                ["i2", 3, 4],
                ["i2", 3, "i1"],
              ];

try {
    $associationRules->setDataset($datasetTransactions);
    $supportsK1 = $associationRules->removeUnwantedSupports($associationRules->getSupports());
    $supportsK2 = $associationRules->removeUnwantedSupports($associationRules->getK2Supports($associationRules->getItemsetsWithK2($supportsK1)));
    $finalPairsItemsets = $associationRules->getFinalPairsItemsets($supportsK2);
    $confidenceRules = $associationRules->getConfidenceRules($finalPairsItemsets, $supportsK2, $supportsK1);

} catch(Exception $e) {
    var_dump($e->getMessage());
    $rules = [];
}

echo("<h3>Associations Rules:</h3>");
echo("<p>Ex: People who has item {A} has item {B} too.</p>");
var_dump($confidenceRules);

数据集格式

  • 用于评分预测的数据集应该是一个包含3个整数列的数组:user_id、item_id和评分值。(对于同一用户的物品不应重复)。
  • 用于Apriori规则的数据集应该是一个包含另一个包含物品ID的数组的数组。(每个事务中的物品不应重复)。

作者

Eng. Fernando Sánchez

单元测试库

单元测试库在"/tests"文件夹中

  • 运行 php phpunit.phar --verbose RecommendationTest.php