概要
閲覧数:1325
投稿日:2016-01-03
更新日:2016-01-03
書籍からの変更点
・sim_distance関数の最後の行
return 1/(1 + sum_of_squares)
↓
return 1/(1 + sqrt(sum_of_squares))
リンク先からの変更点
・'Michael Phillips' の 'The Night Listener' 評価を 3.0 から 4.0 へ変更
コード
$critics = array( 'Lisa Rose' => array( 'Lady in the Water' => 2.5, 'Snakes on a Plane' => 3.5, 'Just My Luck' => 3.0, 'Superman Returns' => 3.5, 'You, Me and Dupree' => 2.5, 'The Night Listener' => 3.0, ), 'Gene Seymour' => array( 'Lady in the Water' => 3.0, 'Snakes on a Plane' => 3.5, 'Just My Luck' => 1.5, 'Superman Returns' => 5.0, 'You, Me and Dupree' => 3.5, 'The Night Listener' => 3.0, ), 'Michael Phillips' => array( 'Lady in the Water' => 2.5, 'Snakes on a Plane' => 3.0, 'Superman Returns' => 3.5, 'The Night Listener' => 4.0, ), 'Claudia Puig' => array( 'Snakes on a Plane' => 3.5, 'Just My Luck' => 3.0, 'Superman Returns' => 4.0, 'You, Me and Dupree' => 2.5, 'The Night Listener' => 4.5, ), 'Mick LaSalle' => array( 'Lady in the Water' => 3.0, 'Snakes on a Plane' => 4.0, 'Just My Luck' => 2.0, 'Superman Returns' => 3.0, 'You, Me and Dupree' => 2.0, 'The Night Listener' => 3.0, ), 'Jack Matthews' => array( 'Lady in the Water' => 3.0, 'Snakes on a Plane' => 4.0, 'Superman Returns' => 5.0, 'You, Me and Dupree' => 3.5, 'The Night Listener' => 3.0, ), 'Toby' => array( 'Snakes on a Plane' => 4.5, 'Superman Returns' => 4.0, 'You, Me and Dupree' => 1.0, ), ); // person1とperson2の距離を基にした類似性スコアを返す //返り値は0-1の範囲で1に近いほど類似性がある function sim_distance($prefs, $person1, $person2){ $si = array(); //二人とも評価しているアイテムのリストを得る foreach($prefs["$person1"] as $item => $val){ if(isset($prefs["$person2"]["$item"])){ $si["$item"] = 1; } } if(count($si) == 0){ return 0;} //すべての差の平方を足し合わせる //上のループでできる $sum_of_squares = 0; foreach($prefs["$person1"] as $item => $val){ if(isset($prefs["$person2"]["$item"])){ $sum_of_squares += pow( ( $prefs["$person1"]["$item"] - $prefs["$person2"]["$item"]),2); } } return 1/(1 + sqrt($sum_of_squares)); } //P1とP2のピアソン相関係数を返す。 function sim_pearson($prefs,$p1,$p2){ //両者が互いに評価しているアイテムのリストを取得 $si = array(); foreach($prefs["$p1"] as $item => $val){ if(isset($prefs["$p2"]["$item"])){ $si["$item"] = 1; } } //要素の数を調べる。 $n = count($si); //すべての嗜好,平方,積を合計する //嗜好の合計 $sum1 = 0; $sum2 = 0; //平方の合計 $sum1Sq = 0; $sum2Sq = 0; //積の合計 $pSum = 0; foreach($si as $item => $val){ $sum1 += $prefs["$p1"]["$item"]; $sum2 += $prefs["$p2"]["$item"]; $sum1Sq += pow($prefs["$p1"]["$item"],2); $sum2Sq += pow($prefs["$p2"]["$item"],2); $pSum += $prefs["$p1"]["$item"] * $prefs["$p2"]["$item"]; } //ピアソンスコアを計算する $num = $pSum - ($sum1 * $sum2 / $n); $den = sqrt(($sum1Sq - pow($sum1,2) / $n) * ($sum2Sq - pow($sum2,2) / $n)); if($den == 0){ return 0;} $r = $num / $den; return $r; } //ディクショナリprefsからpersonにもっともマッチするものたちを返す //結果の数と類似性関数はオプションのパラメータ function topMatches($prefs, $person, $n=5, $similarity="sim_pearson"){ $scores = array(); foreach($prefs as $other => $list){ if($other != $person){ switch($similarity){ case "sim_distance": $scores[] = array(sim_distance($prefs,$person,$other),$other); break; case "sim_pearson": $scores[] = array(sim_pearson($prefs,$person,$other),$other); break; } } } sort($scores); rsort($scores); $res = array(); for($i = 0;$i < $n;$i++){ $res[] = array_shift($scores); } return $res; } function transformPrefs($prefs){ $result = array(); foreach($prefs as $person => $items){ foreach($prefs["$person"] as $item => $scor){ $result["$item"]["$person"] = $prefs["$person"]["$item"]; } } return $result; } $movies = transformPrefs($critics); var_dump($movies); var_dump(topMatches($movies,'Superman Returns'));
結果
array(6) { ["Lady in the Water"]=> array(5) { ["Lisa Rose"]=> float(2.5) ["Gene Seymour"]=> float(3) ["Michael Phillips"]=> float(2.5) ["Mick LaSalle"]=> float(3) ["Jack Matthews"]=> float(3) } ["Snakes on a Plane"]=> array(7) { ["Lisa Rose"]=> float(3.5) ["Gene Seymour"]=> float(3.5) ["Michael Phillips"]=> float(3) ["Claudia Puig"]=> float(3.5) ["Mick LaSalle"]=> float(4) ["Jack Matthews"]=> float(4) ["Toby"]=> float(4.5) } ["Just My Luck"]=> array(4) { ["Lisa Rose"]=> float(3) ["Gene Seymour"]=> float(1.5) ["Claudia Puig"]=> float(3) ["Mick LaSalle"]=> float(2) } ["Superman Returns"]=> array(7) { ["Lisa Rose"]=> float(3.5) ["Gene Seymour"]=> float(5) ["Michael Phillips"]=> float(3.5) ["Claudia Puig"]=> float(4) ["Mick LaSalle"]=> float(3) ["Jack Matthews"]=> float(5) ["Toby"]=> float(4) } ["You, Me and Dupree"]=> array(6) { ["Lisa Rose"]=> float(2.5) ["Gene Seymour"]=> float(3.5) ["Claudia Puig"]=> float(2.5) ["Mick LaSalle"]=> float(2) ["Jack Matthews"]=> float(3.5) ["Toby"]=> float(1) } ["The Night Listener"]=> array(6) { ["Lisa Rose"]=> float(3) ["Gene Seymour"]=> float(3) ["Michael Phillips"]=> float(4) ["Claudia Puig"]=> float(4.5) ["Mick LaSalle"]=> float(3) ["Jack Matthews"]=> float(3) } } array(5) { [0]=> array(2) { [0]=> float(0.65795169495977) [1]=> string(18) "You, Me and Dupree" } [1]=> array(2) { [0]=> float(0.48795003647427) [1]=> string(17) "Lady in the Water" } [2]=> array(2) { [0]=> float(0.11180339887499) [1]=> string(17) "Snakes on a Plane" } [3]=> array(2) { [0]=> float(-0.17984719479905) [1]=> string(18) "The Night Listener" } [4]=> array(2) { [0]=> float(-0.42289003161103) [1]=> string(12) "Just My Luck" } }