User Recommendations Using Apache Mahout
As web developers we must always ask ourselves how we can improve the user experience. One key aspect of this is better, more personalized content. Every visitor has different tastes and preferences and by leveraging linear algebra (aka artificial intelligence), we can personalize the content for each of our awesome users.
Welcome to the future. Welcome to Apache Mahout.
The Data
User preferences are composed of three components:
- User Id
- Item Id
- Rating
The standard format is to have one user preference per line in CSV format:
userId,itemId,preference/rating
Ex Dataset:
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
The Recommendations
DataModel model = new FileDataModel(new File("ratings.txt")); UserSimilarity similarity = new PearsonCorrelationSimilarity(model); UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.1, similarity, model); UserBasedRecommender recommender = new GenericUserBasedRecommender(model, neighborhood, similarity); List<RecommendedItem> recommendations = recommender.recommend(2, 10); for (RecommendedItem recommendation : recommendations) { System.out.println(recommendation.getItemID() + ": " + recommendation.getValue()); }