Skip to content

Instantly share code, notes, and snippets.

@linggom
Created May 8, 2015 16:48
Show Gist options
  • Save linggom/c1f1b265dd7002d4a476 to your computer and use it in GitHub Desktop.
Save linggom/c1f1b265dd7002d4a476 to your computer and use it in GitHub Desktop.
1 10 1.0
1 11 2.0
1 12 5.0
1 13 5.0
1 14 5.0
1 15 4.0
1 16 5.0
1 17 1.0
1 18 5.0
2 10 1.0
2 11 2.0
2 15 5.0
2 16 4.5
2 17 1.0
2 18 5.0
3 11 2.5
3 12 4.5
3 13 4.0
3 14 3.0
3 15 3.5
3 16 4.5
3 17 4.0
3 18 5.0
4 10 5.0
4 11 5.0
4 12 5.0
4 13 0.0
4 14 2.0
4 15 3.0
4 16 1.0
4 17 4.0
4 18 1.0
package com.company;
import lombok.extern.slf4j.Slf4j;
import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator;
import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import org.apache.mahout.cf.taste.impl.neighborhood.NearestNUserNeighborhood;
import org.apache.mahout.cf.taste.impl.neighborhood.ThresholdUserNeighborhood;
import org.apache.mahout.cf.taste.impl.recommender.CachingRecommender;
import org.apache.mahout.cf.taste.impl.recommender.GenericItemBasedRecommender;
import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;
import org.apache.mahout.cf.taste.impl.similarity.GenericItemSimilarity;
import org.apache.mahout.cf.taste.impl.similarity.PearsonCorrelationSimilarity;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.neighborhood.UserNeighborhood;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.apache.mahout.cf.taste.recommender.Recommender;
import org.apache.mahout.cf.taste.similarity.ItemSimilarity;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;
import org.slf4j.LoggerFactory;
import org.slf4j.spi.LoggerFactoryBinder;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
/**
* Created by goman on 05/05/15.
*/
public class UserBasedRecommendation {
public static void main(String[] args) {
userBasedRecommendation();
}
static void userBasedRecommendation() {
try {
DataModel model = new FileDataModel(new File("data.csv"));
UserSimilarity userSimilarity = new PearsonCorrelationSimilarity(model);
UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.1, userSimilarity, model);
Recommender recommender = new GenericUserBasedRecommender(model, neighborhood, userSimilarity);
Recommender cachingRecommender = new CachingRecommender(recommender);
LongPrimitiveIterator iterator = model.getUserIDs();
while (iterator.hasNext()) {
long uID = iterator.peek();
List<RecommendedItem> recommendations =
cachingRecommender.recommend(uID, 3);
System.out.println("User-Based for UserID " + uID + " : " + recommendations);
iterator.next();
}
} catch (IOException e) {
e.printStackTrace();
} catch (TasteException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment