Created
July 12, 2013 16:54
-
-
Save Ryokuchaneko/5985957 to your computer and use it in GitHub Desktop.
transformPrefsでスコアのディクショナリをアイテムごとのディクショナリに変換して アイテムごとにループを回して各アイテムに似ているアイテムをランキングでとってくる 各ユーザーの評価で映画のタイトルのスコアに重みづけをしてアイテムベースでユーザーにオススメをする
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function calculateSimilarItems($critics, $n) { | |
$result = array(); | |
$itemPrefs = transformPrefs($critics); | |
$c = 0; | |
foreach($itemPrefs as $item => $v) { | |
$c++; | |
if( $c%100 == 0){ | |
echo sprintf("%d / %d", $c, count($itemPrefs)); | |
} | |
$scores = topMatches($itemPrefs, $item, $n, 'distance'); | |
$result[$item] = $scores; | |
} | |
return $result; | |
} | |
$n = 10; | |
$itemsim = calculateSimilarItems($critics, $n); | |
function getRecommendedItems($critics, $itemMatch, $user) { | |
$userRatings = $critics[$user]; | |
$scores = array(); | |
$totalSim = array(); | |
foreach ($userRatings as $item => $rating) { | |
foreach ($itemMatch[$item] as $item2 => $similarity) { | |
if ( array_key_exists($item2, $userRatings)) { | |
continue; | |
} | |
if( !isset($scores[$item2]) ){ | |
$scores[$item2] = $similarity*$rating; | |
}else{ | |
$scores[$item2] = $scores[$item2] + $similarity*$rating; | |
} | |
if (!isset($totalSim[$item2])) { | |
$totalSim[$item2] = $similarity; | |
} else { | |
$totalSim[$item2] = $totalSim[$item2] + $similarity; | |
} | |
} | |
} | |
foreach ($scores as $item => $score) { | |
$rankings[$item] = $score/$totalSim[$item]; | |
} | |
arsort($rankings); | |
return $rankings; | |
} | |
$recommendation = getRecommendedItems($critics, $itemsim, 'Toby'); | |
var_dump($recommendation); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment