Apache Mahout is an open source project that is primarily used in producing scalable machine learning algorithms. This tutorial will show you how to install Apache Mahout in Eclipse.

Requirement:
Eclipse IDE
Maven - http://download.eclipse.org/technology/m2e/releases/

Apache Mahout

Steps for installing Eclipse:

1. Installing Maven in Eclipse:
  1. Open Eclipse -> Help -> Install new software
  2. In text box paste this URL - http://download.eclipse.org/technology/m2e/releases/
  3. Click add -> type the name [Example: m2eclipse] -> OK
  4. Click on check box of Maven Integration for Eclipse -> Next -> Agree the Licenses -> Finish
  5. Restart the Eclipse
2. Maven Setup for Mahout
  1. File -> New -> Project -> Select Maven -> Maven Project -> Next -> Next
  2. In Group ID -> com.predictionmarketing [Any name]
  3. Artifact ID -> Name of your project [RecommenderApp]
  4. Finish 
 If you are Getting build path problem in Problem Tab
  1. Right Click on Project 
  2. Properties
  3. Java Build Path -> Libraries -> Add Library 
  4. Select JRE System Library -> Next -> Workspace Default Library -> Finish
  5. Delete JRE System Library [J2SE 1.5] 
  6. OK
3. Now lets edit pom.xml file
  1. Click project and you will see pom.xml -> Open it
  2. Beside Overview tab there will be pom.xml -> click it
  3. Add the Dependency inside <dependencies>  </dependencies>
    <dependency>
        <groupId>org.apache.mahout</groupId>
        <artifactId>mahout-math</artifactId>
        <version>${mahout.version}</version>
    </dependency>
    
  4. Change ${mahout.version} with your mahout version [Example: 0.13.0]
  5. Save and close the file
Done!!!

Recommendation Java Code

Creating Dataset for project
  1. Copy the Dataset from https://mahout.apache.org/users/recommender/userbased-5-minutes.html
  2. Project -> Right Click -> New -> Folder -> Folder Name "data" -> Finish
  3. Right click on "data" Folder -> New -> File 
  4. File name as "dataset.csv" -> paste the Dataset you have copied from the above website and save
Add JAR files 
  1. Extract the Apache Mahout Files -> Inside Mahout Folder -> Copy All Jar files
  2. Right Click on Project -> And paste all jar files
  3. One by one  right click jar file -> Build Path -> Add to Build Path
In src/main/PackageName/App 


package com.predictionmarketing.RecommenderApp;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.log4j.PropertyConfigurator;
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;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args ) throws Exception
    {
     String log4jConfPath = "LOG4J/log4j.properties";
     PropertyConfigurator.configure(log4jConfPath);
     
     DataModel model = new FileDataModel(new File("data/dataset.csv"));
     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, 3);
     for (RecommendedItem recommendation : recommendations) {
       System.out.println(recommendation);
     }
     
    }
}



Run the project

IF you getting warn for log4j then 
  1. Create a folder in project as LOG4J
  2. Create a file in folder LOG4J as "log4j.properties" and paste the code
    log4j.rootLogger=INFO, stdout
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.Target=System.out
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{2}: %m%n
    
  3. And add the below code
    String log4jConfPath = "/path/to/log4j.properties";
    PropertyConfigurator.configure(log4jConfPath);
    
  4.  
You might ask yourself, how to make sure that your recommender returns good results.

EvaluationRecommender.java

package com.predictionmarketing.RecommenderApp;

import java.io.File;
import java.io.IOException;

import org.apache.mahout.cf.taste.common.TasteException;
import org.apache.mahout.cf.taste.eval.RecommenderBuilder;
import org.apache.mahout.cf.taste.eval.RecommenderEvaluator;
import org.apache.mahout.cf.taste.impl.eval.AverageAbsoluteDifferenceRecommenderEvaluator;
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.Recommender;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;

public class EvaluateRecommender {

 public static void main(String[] args) throws Exception {
  
  DataModel model = new FileDataModel(new File("data/dataset.csv"));
  RecommenderEvaluator evaluator = new AverageAbsoluteDifferenceRecommenderEvaluator();
  RecommenderBuilder builder = new MyRecommenderBuilder();
  double result = evaluator.evaluate(builder, null, model, 0.9, 1.0);
  System.out.println(result);

 }

}

class MyRecommenderBuilder implements RecommenderBuilder {
 
 public Recommender buildRecommender(DataModel dataModel) throws TasteException{
  UserSimilarity similarity = new PearsonCorrelationSimilarity(dataModel);
  UserNeighborhood neighborhood = new ThresholdUserNeighborhood(0.1, similarity, dataModel);
  return new GenericUserBasedRecommender(dataModel, neighborhood, similarity);
  
 }
 
}


References: https://mahout.apache.org/users/recommender/userbased-5-minutes.html
https://www.youtube.com/watch?v=63k560Livmg