定制 wellingtonbarbosa/knn 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

wellingtonbarbosa/knn

Composer 安装命令:

composer require wellingtonbarbosa/knn

包简介

A package to calculate Euclidean distance in a simple way, in addition to comparing results for recommendation quickly and symptomatically, without ifs.

README 文档

README

A package to calculate Euclidean distance in a simple way, in addition to comparing results for recommendation quickly and symptomatically, without ifs.


How does it work?

Imagine that you are building a movie application, such as Netflix.

You want to recommend a movie to the user when the movie ends, but it needs to be a movie that looks like the one he just watched.

Your films in the database must have a percentage of each category, for example: romance, action, adventure, fiction, etc.

For each film, you enter the percentage of each category

A silly example:

[
    "title" => "Robin Hood",
    "categories" => [
        "adventure" => "1" // 100%,
        "romance" => "0.2" // 20%,
        "action" => "0.6", // 60%,
        ...
    ]
]

The algorithm will use these percentages to perform the calculation

To understand how the euclidian calculation does work, please visit this link

Getting Started

1 - Install on your project via composer

composer require wellingtonbarbosa/knn

2 - Use the class in your php file

<?php
require_once(__DIR__ . '\vendor\autoload.php');

use WellingtonBarbosa\Knn\Knn;

3 - Create some items to test
In our example, $defaultItem is the movie the user has just watched, and the $itemsToCompare are movies drawn from the database. Let's see which of these is more like what our user just watched? So let's go!

//The item to compare (four indices)
$defaultItem = [0.4, 0.2, 0.4, 1];

//The items to be compared (four indices in each item)
$itemsToCompare = [
    [
        0.2, 0.3, 0, 1
    ],
    [
        0.4, 0.2, 0.3, 1
    ],
    [
        0.4, 0.2, 0.4, 1
    ],
    [
        0.4, 0.2, 0.4, 1
    ]
];

! Note that all items have 4 indexes. All items must have the same number of indexes, or it will not work

4 - Instantiate the class in a variable
The last parameter passed is the number of indexes that ALL items have.

/**
 * Starts the object with the values
 * 
 * First param -> The item to compare
 * Second param -> The items to be compared
 * Third param ->  Number of indices in each item to be compared
 */
$knn = new Knn($defaultItem, $itemsToCompare, 4);

5 - Performs Euclidean distance calculation for each item

//Performs Euclidean distance calculation
$results = $knn->calculate();

6 - Finally, we will get index (or indexes) of the items to recommend to our user
Note that in addition to the calculated results, there is a second parameter in the recommendation method. This is because there may be equal results in the calculation. If you pass TRUE, the method will return the index all results repeated. If you pass FALSE, the first one found will be returned

/**
 * Returns the index of the item to be recommended
 * 
 * First param -> The results of Euclidean distance calculation for all compared items
 * 
 * If the second param is true, it will return an array of close results.
 * If false, it will return the first index found
 * 
 */
$recomendation = $knn->recomend($results, true);

$recomendation = $knn->recomend($results, false);

7 - Now, just look for the item that we will recommend

//Multiple items
    foreach($recomendation as $key) {
        echo "Euclidian distances for item (" . $key . ") =>> "  . $result[$key];
        echo "<br>";
        foreach($itemsToCompare[$key] as $item) {
            echo $item . " | ";
        }
        echo "<hr>";
    }

//Single item
echo "Recomended item: " . $recomendation;
echo "<br>";
foreach($itemsToCompare[$key] as $item) {
    echo $item . " | ";
}

You can view this complete file by clicking here

Contributing

You can contribute to this package by forking the repository and developing new features and increments and sending the pull request!

Author

Wellington Carneiro Barbosa Wellington Barbosa
Instagram | LinkedIn | Facebook

License

This package is under the MIT License.

Any issue, tell me on github! I will help you.

统计信息

  • 总下载量: 32
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 6
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 5
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-11-16

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固