Don't like ads? PRO users don't see any ads ;-)
Guest

KNN_3NN

By: a guest on May 6th, 2012  |  syntax: PHP  |  size: 2.57 KB  |  hits: 22  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. <?php
  2.  
  3. // Simulated ratings for books from input_database sample
  4.  // array (ratings:(0-->5), 'Name of book")
  5. $input_data = array(                     // 2D array[indexes]
  6.     array(0, 'Van Helsing'),      //  [0]
  7.     array(3, 'Dracula'),           // [1]
  8.     array(2, 'The Pearl'),        //  [2]
  9.     array(4, 'Pocahontas'),      //   [3]
  10.     array(0, 'How to be good'),   //  [4]
  11.     array(4, 'I know how to love'),  // [5]
  12.     array(2, 'How to be millionaire'),  // [6]
  13.     array(5, 'Eat, pray and love'),   // [7]
  14.     array(4, 'How to follow God'),   // [8]
  15.         array(5, 'Intro to Philosophy'), // [9]
  16.         array(2, 'Intro Algorithms'),   // [10]
  17.         array(2, 'How to happy'),      // [11]
  18.         array(4, 'How to have sex'),   // [12]
  19.         array(0, 'Quantum Mechanics'),  //[13]
  20.         array(3, 'Purpose Driven live') // [14]
  21. );
  22.  
  23.  
  24. // Calculat the distances or differences of points
  25. $distance_matrix = $input_data;
  26.  
  27. //array_walk — Apply a user function to every member of an array
  28. array_walk($distance_matrix, 'Euclid_Distance', $input_data);
  29.  
  30.  
  31.  
  32. // Specify the data point index, it would compute the #3NN automatically
  33. $neighbors = KNN($distance_matrix, 13);
  34. echo assoc_book_name($input_data, $neighbors); // red
  35.  
  36.  
  37.  
  38.  
  39. // This is necessary for calculating the distance between ratings values
  40. function Euclid_Distance(&$sourceCoords, $sourceKey, $input_data)
  41. {  
  42.     $distance_matrix = array();
  43.     list ($Xi, $Yi) = $sourceCoords;
  44.        
  45.     foreach ($input_data as $destinationKey => $destinationCoords)
  46.         {
  47.         // Same point, ignore
  48.         if ($sourceKey == $destinationKey)
  49.                 {
  50.             continue;
  51.         }
  52.         list ($X_2, $Y_2) = $destinationCoords;
  53.         //$distance_matrix[$destinationKey] = abs(pow($x1 - $x2, 2) + pow($y1 - $y2, 2));
  54.                 $distance_matrix[$destinationKey] = sqrt(pow($Xi - $X_2, 2) + pow($Yi - $Y_2, 2));
  55.     }
  56.     asort($distance_matrix);
  57.     $sourceCoords = $distance_matrix;
  58. }
  59.  
  60. // Returns n-nearest neighbors
  61.  
  62. function KNN($distance_matrix, $N)
  63. {                                             // 3NN, K = 3
  64.     return array_slice($distance_matrix[$N], 0, 3, true);
  65. }
  66.  
  67. /*
  68.   Gets result label from associated input_data
  69.  */
  70. function assoc_book_name($input_data, $neighbors)
  71. {
  72.   $results = array();
  73.   $neighbors = array_keys($neighbors);
  74.    
  75.   foreach ($neighbors as $neighbor)
  76.    {                                 // 2 (xi, yi) before, look at 1 data point
  77.         $results[] = $input_data[$neighbor][1];
  78.    }
  79.  
  80.     $val = array_count_values($results);
  81.     $val = array_flip($val);
  82.     ksort($val);
  83.     return array_pop($val);
  84. }