Mahout Recommendation Java Example

posted on Nov 20th, 2016

Apache Mahout

Apache Mahout is a project of the Apache Software Foundation to produce free implementations of distributed or otherwise scalable machine learning algorithms focused primarily in the areas of collaborative filtering, clustering and classification. Many of the implementations use the Apache Hadoop platform. Mahout also provides Java libraries for common maths operations (focused on linear algebra and statistics) and primitive Java collections.

While Mahout's core algorithms for clustering, classification and batch based collaborative filtering are implemented on top of Apache Hadoop using the map/reduce paradigm, it does not restrict contributions to Hadoop-based implementations.

Pre Requirements

1) A machine with Ubuntu 14.04 LTS operating system.

2) Apcahe Hadoop pre installed (How to install Hadoop on Ubuntu 14.04)

3) Apcahe Mahout pre installed (How to install Mahout on Ubuntu 14.04)

Mahout Recommendation Example

Mahout has a non-distributed, non-Hadoop-based recommender engine. You should pass a text document having user preferences for items. And the output of this engine would be the estimated preferences of a particular user for other items.

Add these libraries to your java project build path.

/usr/local/mahout/mahout-core-0.9.jar
/usr/local/mahout/mahout-core-0.9-job.jar
/usr/local/mahout/mahout-integration-0.9.jar
/usr/local/mahout/mahout-math-0.9.jar

dataset.csv

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

Recommender.java

import java.io.File;
import java.util.List;
import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import org.apache.mahout.cf.taste.impl.neighborhood.ThresholdUserNeighborhood;
import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;
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.UserBasedRecommender;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;

public class Recommender {
	public static void main(String args[]) {
		try {
			// Creating data model
			DataModel datamodel = new FileDataModel(new File("/home/hduser/Desktop/MAHOUT/dataset.csv")); // data
			// Creating UserSimilarity object.
			UserSimilarity usersimilarity = new PearsonCorrelationSimilarity(
					datamodel);
			// Creating UserNeighbourHHood object.
			UserNeighborhood userneighborhood = new ThresholdUserNeighborhood(
					0.1, usersimilarity, datamodel);
			// Create UserRecomender
			UserBasedRecommender recommender = new GenericUserBasedRecommender(
					datamodel, userneighborhood, usersimilarity);
			List<RecommendedItem> recommendations = recommender.recommend(2, 3);
			
			for (RecommendedItem recommendation : recommendations) {
				System.out.println(recommendation);
			}
		} catch (Exception e) {
		}
	}
}

Please share this blog post and follow me for latest updates on

facebook             google+             twitter             feedburner

Previous Post                                                                                          Next Post

Labels : Apache Mahout Installation on Ubuntu