Mahout (6) - ClusteringRecommender -
org.apache.mahout.cf.taste.recommender.ClusteringRecommenderを使用する事でデータをクラスタ化し、そのクラスタ内でレコメンデーションを行う事が出来る
import java.io.File;
import java.util.Iterator;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import org.apache.mahout.cf.taste.impl.similarity.TanimotoCoefficientSimilarity;
import org.apache.mahout.cf.taste.impl.recommender.TreeClusteringRecommender;
import org.apache.mahout.cf.taste.impl.recommender.NearestNeighborClusterSimilarity;
import org.apache.mahout.cf.taste.impl.common.FastIDSet;
import org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;
import org.apache.mahout.cf.taste.recommender.ClusteringRecommender;
public class Client {
public static void main(String[] args) throws Exception {
DataModel model = new FileDataModel(new File("data/data.txt"));
UserSimilarity similar = new TanimotoCoefficientSimilarity(model);
ClusteringRecommender recommender = new TreeClusteringRecommender(
model,
new NearestNeighborClusterSimilarity(similar),
model.getNumUsers() / 2
);
FastIDSet[] clusters = recommender.getClusters();
for(FastIDSet cluster : clusters) {
if(!(cluster.size() > 1)) {
continue;
}
Iterator<Long> users = cluster.iterator();
while(users.hasNext()) {
long user = users.next();
System.out.println(user + " -> " + recommender.recommend(user, 10));
/* 手動でクラスタ内での相関予測を行う場合
LongPrimitiveIterator items = model.getItemIDs();
while(items.hasNext()) {
long item = items.next();
float value = recommender.estimatePreference(user,item);
System.out.println(user + " -> " + item + " = " + value);
}
*/
}
}
}
}