Mahout (3) - SVDRecommender -
※以前検証したのを現時点のバージョンであるMahout0.8で修正してます (使い方が変わっている為)
import java.io.File;
import java.util.List;
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.recommender.svd.Factorizer;
import org.apache.mahout.cf.taste.impl.recommender.svd.SVDPlusPlusFactorizer;
import org.apache.mahout.cf.taste.impl.recommender.svd.SVDRecommender;
import org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.apache.mahout.cf.taste.recommender.Recommender;
public class Client {
public static void main(String[] args) throws Exception {
File dataFile = new File(Client.class.getResource("data.csv").getFile());
DataModel model = new FileDataModel(dataFile);
Factorizer factorizer = new SVDPlusPlusFactorizer(
model,
3, // numFeature
10 // numIteration
);
Recommender recommender = new SVDRecommender(model, factorizer);
LongPrimitiveIterator users = model.getUserIDs();
while(users.hasNext()) {
long user = users.nextLong();
List<RecommendedItem> recommends = recommender.recommend(user, 10);
for(RecommendedItem recommend : recommends) {
System.out.println(user + " -> " + recommend.toString());
}
// 個別に評価値を取得する場合
LongPrimitiveIterator items = model.getItemIDs();
while(items.hasNext()) {
long item = items.nextLong();
float value = recommender.estimatePreference(user, item);
System.out.println(user + " -> " + item + " = " + value);
}
}
}
}