Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Ryokuchaneko/5992678 to your computer and use it in GitHub Desktop.
Save Ryokuchaneko/5992678 to your computer and use it in GitHub Desktop.
MovieLensのデータ・セットを使って商品の推薦を行います。 まず、loadMovieLens関数でMovieLensのデータをロードします。そして、getRecommendations関数を使ってユーザーベースの推薦を行い、calculateSimilarItems関数を使ってアイテム間の相関を計算し、getRecommendedItems関数でアイテムベースの推薦を行います。
function loadMovieLens($path) {
$movies = array();
$fp = fopen($path . "/u.item", "r");
while($line = fgets($fp)){
$line_explode = explode("|", $line);
$movies[$line_explode[0]] = $line_explode[1];
}
fclose($fp);
$prefs = array();
$fp = fopen($path . "/u.data", "r");
while($line = fgets($fp)){
$data_explode = explode("\t",$line);
$user = $data_explode[0];
$movieid = $data_explode[1];
$rating = $data_explode[2];
$ts = $data_explode[3];
if(!isset($prefs[$user])) {
$prefs[$user] = array();
}
$prefs[$user][$movies[$movieid]]=$rating;
}
return $prefs;
}
$lines = loadMovieLens('MovieLensのデータを保存したフォルダ');
$movie_recommend = getRecommendations($lines, '87', 'pearson');
$itemsim = calculateSimilarItems($lines, 50);
$movie_recommend_item = getRecommendedItems($lines, $itemsim, '87');
var_dump($itemsim);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment