我是靠谱客的博主 幸福酒窝,最近开发中收集的这篇文章主要介绍php 用户推荐系统曼哈顿算法实现,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

public function index2()
{
$users = '{"Angelica": {"Blues Traveler": 3.5, "Broken Bells": 2.0, "Norah Jones": 4.5, "Phoenix": 5.0, "Slightly Stoopid": 1.5, "The Strokes": 2.5, "Vampire Weekend": 2.0},
"Bill":{"Blues Traveler": 2.0, "Broken Bells": 3.5, "Deadmau5": 4.0, "Phoenix": 2.0, "Slightly Stoopid": 3.5, "Vampire Weekend": 3.0},
"Chan": {"Blues Traveler": 5.0, "Broken Bells": 1.0, "Deadmau5": 1.0, "Norah Jones": 3.0, "Phoenix": 5, "Slightly Stoopid": 1.0},
"Dan": {"Blues Traveler": 3.0, "Broken Bells": 4.0, "Deadmau5": 4.5, "Phoenix": 3.0, "Slightly Stoopid": 4.5, "The Strokes": 4.0, "Vampire Weekend": 2.0},
"Hailey": {"Broken Bells": 4.0, "Deadmau5": 1.0, "Norah Jones": 4.0, "The Strokes": 4.0, "Vampire Weekend": 1.0},
"Jordyn":
{"Broken Bells": 4.5, "Deadmau5": 4.0, "Norah Jones": 5.0, "Phoenix": 5.0, "Slightly Stoopid": 4.5, "The Strokes": 4.0, "Vampire Weekend": 4.0},
"Sam": {"Blues Traveler": 5.0, "Broken Bells": 2.0, "Norah Jones": 3.0, "Phoenix": 5.0, "Slightly Stoopid": 4.0, "The Strokes": 5.0},
"Veronica": {"Blues Traveler": 3.0, "Norah Jones": 5.0, "Phoenix": 4.0, "Slightly Stoopid": 2.5, "The Strokes": 3.0}
}';
$usersArray = json_decode($users, true);
$recommend = $this->recommend('Hailey', $usersArray);
echo "<pre>";
print_r($recommend);
}
//实现推荐
public function recommend($username, $users)
{
//获得最近用户的name
$nearest = $this->computeNearestNeighbor($username, $users);
$nearest = $nearest['0']['user'];
$recommendations = array();
//得到最近用户的推荐列表
$neighborRatings = $users[$nearest];
if ($this->checkArray($neighborRatings)) {
$usernameAction = $users[$username];
foreach ($neighborRatings as $name => $code) {
//读取自己没有的
if (!isset($usernameAction[$name])) {
$recommendations[] = ['name' => $name, 'score' => $code];
}
}
}
$this->sortArrByField($recommendations, 'score', true);
return $recommendations;
}
// 计算曼哈顿距离
private function manhattan($rate1, $rate2)
{
$distance = 0;
if ($this->checkArray($rate1)) {
foreach ($rate1 as $name => $value1) {
if (isset($rate2[$name])) {
$value2 = $rate2[$name];
$distance += abs($value1 - $value2);
}
}
} else {
$distance = -1;
}
return $distance;
}
// 返回最近距离用户
private function computeNearestNeighbor($username, $users)
{
$distances = array();
if ($this->checkArray($users)) {
foreach ($users as $key => $user) {
if ($key == $username) {
continue;
}
$distance = $this->manhattan($user, $users[$username]);
$data = array(
'user' => $key,
'distance' => $distance,
);
$distances[] = $data;
}
}
$this->sortArrByField($distances, 'distance', false);
return $distances;
}
/**
* @param $array
* @return bool
* 检查数组有效性
*/
private function checkArray($array)
{
if (is_array($array) && count($array) > 0) {
return true;
} else {
return false;
}
}
/**
* @param $array
* @param $field
* @param bool $desc
* 根据数组的某个值进行排序
*/
private function sortArrByField(&$array, $field, $desc = false)
{
$fieldArr = array();
foreach ($array as $k => $v) {
$fieldArr[$k] = $v[$field];
}
$sort = $desc == false ? SORT_ASC : SORT_DESC;
array_multisort($fieldArr, $sort, $array);
}

 

最后

以上就是幸福酒窝为你收集整理的php 用户推荐系统曼哈顿算法实现的全部内容,希望文章能够帮你解决php 用户推荐系统曼哈顿算法实现所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(27)

评论列表共有 0 条评论

立即
投稿
返回
顶部