Skip to content

Instantly share code, notes, and snippets.

@Ryokuchaneko
Last active December 19, 2015 16:28
Show Gist options
  • Save Ryokuchaneko/5983627 to your computer and use it in GitHub Desktop.
Save Ryokuchaneko/5983627 to your computer and use it in GitHub Desktop.
二人の評価者のユークリッド距離によるスコアを算出するsim_distanceとピアソン相関によるスコアを算出するsim_peasonを定義 リストの中から最も好みの似ている評価者を選び出すtopMatchesを定義
function sim_distance($person1, $person2){
$si = array();
foreach ($person1 as $k => $v) {
if (array_key_exists($k, $person2)) {
$si[$k] = 1;
}
}
if (count($si) == 0) {
return 0;
}
$squares = array();
foreach($si as $k => $v) {
$squares[] = pow(($person1[$k] - $person2[$k]), 2);
}
$sum_of_squares = array_sum($squares);
return 1/(1 + $sum_of_squares);
}
function sim_pearson($p1, $p2) {
$si = array();
foreach ($p1 as $k => $v) {
if (array_key_exists($k, $p2)) {
$si[$k] = 1;
}
}
if (count($si) == 0) {
return 0;
}
$sum1 = $sum2 = $sum1Sq = $sum2Sq = $pSum = 0;
foreach ($si as $k => $v) {
$sum1 = $sum1 + $p1[$k];
$sum2 = $sum2 + $p2[$k];
$sum1Sq = $sum1Sq + $p1[$k]*$p1[$k];
$sum2Sq = $sum2Sq + $p2[$k]*$p2[$k];
$pSum = $pSum + $p1[$k]*$p2[$k];
}
$num = $pSum -($sum1*$sum2/count($si));
$den=sqrt(($sum1Sq-pow($sum1, 2)/count($si))*($sum2Sq-pow($sum2, 2)/count($si)));
if($den == 0) {
return 0;
}
$r = $num / $den;
return $r;
}
function topMatches($array, $name1, $number, $type){
foreach($array as $key => $v){
if($key != $name1) {
$other = $key;
if ($type == 'pearson') {
$score[$name1][$other] = sim_pearson($array[$name1], $array[$other]);
} elseif ($tyep == 'distance') {
$score[$name1][$other] = sim_distance($array[$name1], $array[$other]);
}else{
echo '正しい評価方法を指定してください';
exit;
}
}
}
arsort($score[$name1]);
$output = array_slice($score[$name1], 0, $number, true);
return $output;
}
$result = topMatches($critics, 'Toby', 3, 'pearson');
var_dump($result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment