概要
閲覧数:1081
投稿日:2016-01-02
更新日:2016-01-02
書籍からの変更点
・sim_distance関数の最後の行
return 1/(1 + sum_of_squares)
↓
return 1/(1 + sqrt(sum_of_squares))
リンク先からの変更点
・'Michael Phillips' の 'The Night Listener' 評価を 3.0 から 4.0 へ変更
1つ目の結果確認
・書籍
2つ目の結果確認
https://sites.google.com/site/prgclctintelligence/errata
Noticeエラー: Undefined index:
・非表示対応方法不明
コード
$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)); //return 1/(1 + $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; } // person以外の全ユーザの評点の重み付き平均を使い。personへの推薦を算出する function getRecommendations($prefs, $person, $similarity = "sim_pearson"){ $totals = array(); $simSums = array(); foreach ($prefs as $other => $list) { // 自分自身とは比較しない if ($other === $person){ continue; } switch($similarity){ case "sim_distance": $sim = sim_distance($prefs,$person,$other); break; case "sim_pearson": $sim = sim_pearson($prefs,$person,$other); break; } // 0以下のスコアは無視 if ($sim <= 0) { continue; } foreach ($prefs["$other"] as $item => $scor) { if (! isset($prefs["$person"]["$item"]) || $prefs["$person"]["$item"] == 0) { // 類似度 * スコア $other_item_scor = isset($prefs["$other"]["$item"]) ? $prefs["$other"]["$item"] : 0; $totals["$item"] += $other_item_scor * $sim; // 類似度を合計 $simSums["$item"] += $sim; } } } // 正規化したリストを作る $rankings = array(); foreach ($totals as $item => $total){ $rankings[] = array( ($total / $simSums["$item"]), $item); } // ソート済みのリストを返す sort($rankings); rsort($rankings); return $rankings; } var_dump(getRecommendations($critics, 'Toby')); var_dump(getRecommendations($critics, 'Toby', $similarity = 'sim_distance'));
結果
<br /> <b>Notice</b>: Undefined index: Lady in the Water in <b>[...][...]</b> on line <b>151</b><br /> <br /> <b>Notice</b>: Undefined index: Lady in the Water in <b>[...][...]</b> on line <b>154</b><br /> <br /> <b>Notice</b>: Undefined index: Just My Luck in <b>[...][...]</b> on line <b>151</b><br /> <br /> <b>Notice</b>: Undefined index: Just My Luck in <b>[...][...]</b> on line <b>154</b><br /> <br /> <b>Notice</b>: Undefined index: The Night Listener in <b>[...][...]</b> on line <b>151</b><br /> <br /> <b>Notice</b>: Undefined index: The Night Listener in <b>[...][...]</b> on line <b>154</b><br /> array(3) { [0]=> array(2) { [0]=> float(3.3477895267131) [1]=> string(18) "The Night Listener" } [1]=> array(2) { [0]=> float(2.8325499182642) [1]=> string(17) "Lady in the Water" } [2]=> array(2) { [0]=> float(2.5309807037656) [1]=> string(12) "Just My Luck" } } <br /> <b>Notice</b>: Undefined index: Lady in the Water in <b>[...][...]</b> on line <b>151</b><br /> <br /> <b>Notice</b>: Undefined index: Lady in the Water in <b>[...][...]</b> on line <b>154</b><br /> <br /> <b>Notice</b>: Undefined index: Just My Luck in <b>[...][...]</b> on line <b>151</b><br /> <br /> <b>Notice</b>: Undefined index: Just My Luck in <b>[...][...]</b> on line <b>154</b><br /> <br /> <b>Notice</b>: Undefined index: The Night Listener in <b>[...][...]</b> on line <b>151</b><br /> <br /> <b>Notice</b>: Undefined index: The Night Listener in <b>[...][...]</b> on line <b>154</b><br /> array(3) { [0]=> array(2) { [0]=> float(3.4571286944914) [1]=> string(18) "The Night Listener" } [1]=> array(2) { [0]=> float(2.7785840038149) [1]=> string(17) "Lady in the Water" } [2]=> array(2) { [0]=> float(2.4224820423619) [1]=> string(12) "Just My Luck" } }