- 论坛徽章:
- 0
|
构建基于相似用户的推荐
计算用户相似度:两个用户相似度就简单等同两者一致性评分次数除以两者共同评分过的条目
02data.php
Php代码- 1.<?php
- 2.$data = array(
- 3. 'Frank'=>array(
- 4. 'Tears'=>5,
- 5. 'La'=>4,
- 6. 'Robinson'=>5,
- 7. 'Yesterday'=>4,
- 8. 'Wizard'=>5,
- 9. 'Mozart'=>5,
- 10. 'Bethoven'=>5,
- 11. ),
- 12. 'Constantine'=>array(
- 13. 'Tears'=>5,
- 14. 'Fiddler'=>5,
- 15. 'Robinson'=>5,
- 16. 'Wonderful World'=>4,
- 17. 'Wizard'=>4,
- 18. 'Let It Be'=>5,
- 19. 'Mozart'=>5
- 20. ),
- 21. 'Catherine'=>array(
- 22. 'Tears'=>1,
- 23. 'Robinson'=>2,
- 24. 'Yesterday'=>2,
- 25. 'Beethoven'=>3,
- 26. 'Sunnday'=>1,
- 27. 'Let It Be'=>2,
- 28. ),
- 29. 'David'=>array(
- 30. 'Tears'=>1,
- 31. 'Robinson'=>2,
- 32. 'Yesterday'=>2,
- 33. 'Let It Be'=>2,
- 34. 'Bethoven'=>5,
- 35. 'love'=>1,
- 36. 'world'=>1
- 37. ),
- 38. 'lei'=>array(
- 39. 'love'=>1,
- 40. 'Bethoven'=>5
- 41. )
- 42.);
- 43.?>
- <?php
- $data = array(
- 'Frank'=>array(
- 'Tears'=>5,
- 'La'=>4,
- 'Robinson'=>5,
- 'Yesterday'=>4,
- 'Wizard'=>5,
- 'Mozart'=>5,
- 'Bethoven'=>5,
- ),
- 'Constantine'=>array(
- 'Tears'=>5,
- 'Fiddler'=>5,
- 'Robinson'=>5,
- 'Wonderful World'=>4,
- 'Wizard'=>4,
- 'Let It Be'=>5,
- 'Mozart'=>5
- ),
- 'Catherine'=>array(
- 'Tears'=>1,
- 'Robinson'=>2,
- 'Yesterday'=>2,
- 'Beethoven'=>3,
- 'Sunnday'=>1,
- 'Let It Be'=>2,
- ),
- 'David'=>array(
- 'Tears'=>1,
- 'Robinson'=>2,
- 'Yesterday'=>2,
- 'Let It Be'=>2,
- 'Bethoven'=>5,
- 'love'=>1,
- 'world'=>1
- ),
- 'lei'=>array(
- 'love'=>1,
- 'Bethoven'=>5
- )
- );
- ?>
-
复制代码 03.php
Php代码- 1.<?php
- 2./**
- 3. *
- 4. * 基于相似用户的推荐
- 5. * @author lei
- 6. *
- 7. */
- 8.class SimilarUsers
- 9.{
- 10. public function estimateUserBasedRating($user, $anotherUser)
- 11. {
- 12. $pMatrix = array();
- 13. $sMatrix = array();
- 14. foreach ($anotherUser as $key=>$value){
- 15. $sim = $this->getSimilar($user, $value);
- 16. if($sim!=0){
- 17. //遍历对相似度不为0的其它用户,并对$user没有打分的项进行预测
- 18. foreach ($value as $key2=>$value2){
- 19. if(!isset($user[$key2])){
- 20. //根据相似度对分值加权
- 21. isset($pMatrix[$key2])?($pMatrix[$key2]+=$sim*$value2):($pMatrix[$key2]=$sim*$value2);
- 22. isset($sMatrix[$key2])?($sMatrix[$key2]+=$sim):($sMatrix[$key2]=$sim);
- 23. }
- 24. }
- 25. }
- 26. }
- 27. foreach ($pMatrix as $key=>$value){
- 28. //以加权值与直接和之比作为预测分值
- 29. $estimatedRating[$key] = (double)$pMatrix[$key]/(double)$sMatrix[$key];
- 30. }
- 31. arsort($estimatedRating);
- 32. return $estimatedRating;
- 33. }
- 34.
- 35.
- 36. private function simMatrix($user1, $user2)
- 37. {
- 38. //假设网站打分区间1-5分
- 39. $matrix = array_fill(1, 5, array_fill(1, 5, 0));
- 40. foreach ($user1 as $key1=>$value1){
- 41. if (isset($user2[$key1]))
- 42. $matrix[$value1][$user2[$key1]]++;
- 43. }
- 44. return $matrix;
- 45. }
- 46.
- 47. /*
- 48. * 两个用户相似度就简单等于两者一致性评分的次数除以两者共同评分过的条目
- 49. */
- 50. private function getSimilar($user1, $user2)
- 51. {
- 52. $sim = 0;
- 53. $matrix = $this->simMatrix($user1, $user2);
- 54. $total = $this->getTotalCount($matrix);
- 55. $agreement = $this->getAgreementCount($matrix);
- 56. if($total!=0)
- 57. $sim = (double)$agreement/(double)$total;
- 58. return $sim;
- 59. }
- 60.
- 61. /*
- 62. * 不同计数器辅助方法
- 63. */
- 64. private function getTotalCount($matrix)
- 65. {
- 66. $ratingCount = 0;
- 67. foreach ($matrix as $key=>$value){
- 68. $ratingCount += array_sum($value);
- 69. }
- 70. return $ratingCount;
- 71. }
- 72.
- 73. private function getAgreementCount($matrix)
- 74. {
- 75. $ratingCount = 0;
- 76. $n = count($matrix);
- 77. for($i=1; $i<=$n; $i++){
- 78. $ratingCount += $matrix[$i][$i];
- 79. }
- 80. return $ratingCount;
- 81. }
- 82.
- 83.
- 84.}
- 85.include_once '02data.php';
- 86.$user = $data['Frank'];
- 87.unset($data['Frank']);
- 88.$another = $data;
- 89.$estimate = new SimilarUsers;
- 90.$p = $estimate->estimateUserBasedRating($user,$data);
- 91.var_dump($p);
- 92.
- 93.?>
复制代码 |
|