2.3.2 ピアソン相関によるスコア

集合知プログラミング > 2章

概要

 閲覧数:1781  投稿日:2015-12-31  更新日:2016-01-02  

P1とP2のピアソン相関係数を返す

リンク先からの変更点
・'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,
  ),
);



//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;
}


var_dump( sim_pearson($critics,'Lisa Rose','Gene Seymour') ); //float(0.39605901719067)
var_dump( sim_pearson($critics,'Lisa Rose','Lisa Rose') ); //float(1)
var_dump( sim_pearson($critics,'Lisa Rose','Toby') ); //float(0.99124070716193)




結果

float(0.39605901719067)
float(1)
float(0.99124070716193)



タグ


pow 





2.3.1 ユークリッド距離によるスコア

2.3.4 訳者をランキングする 



週間人気ページランキング / 11-6 → 11-12
順位 ページタイトル抜粋 アクセス数
1 PHPコード 31
2 MySQLのdatetime型カラムより取得した「月日文字列」先頭に0があった場合、削除 | 日付および時刻関連 4
3 Noticeエラー回避 / PHP7以上で使用可能なnull合体演算子(??)を使用 | 演算子 3
3 MySQLのdatetime型カラムに格納している値と比較して、24時間以内かどうかを判定 | 日付および時刻関連 3
3 配列の内容(ひらがな)を、読み(あ行~わ行)で分け、新たな配列へ格納 | 配列(型) 3
3 クッキー カテゴリー 3
3 Ctype 文字タイプチェック(バリデーション) カテゴリー 3
3 MySQLi (MySQL) カテゴリー 3
4 realpath(__DIR__) | パス 2
4 Smartyのテンプレートにincludeしたファイルを表示 | Smarty(テンプレートエンジン) 2
4 指定文字が2回目に出現する位置以降の文字列を取得 | 文字列(テキスト処理) 2
4 数字を3桁でカンマ区切りに | 文字列(テキスト処理) 2
4 ファイル名を取得し、.拡張子部分を削除し、タイトルに表示 | ファイルシステム 2
4 defined | タグ毎エントリー一覧 2
4 現在WebページのURLパスを第2階層まで取得 | パス 2
5 「指定文字」から「指定文字」までの文字列を削除 | マルチバイト文字列(テキスト処理) 1
5 文字列の中で変数展開される要件 | 文字列(テキスト処理) 1
5 文字列を「複数の区切り文字」で配列へ変換 / preg_split使用案。採用 | 文字列(テキスト処理) 1
5 対象文字列へ対して、一致したすべての文字列を置換する標準関数 / str_replace | 文字列(テキスト処理) 1
5 「1週間後の年月日時分秒」を表示 … time() | 日付および時刻関連 1
2026/11/13 5:05 更新