Pages

2013/11/27

Simple recommender engine

This example is from Mahout in Action book.

Step1:
Create a new Maven project in the Mahout main directory, in my environment is ~/Mahout/trunk
This command creates a new "project" directory. And add a new package "com.smims.recommenderintro" in its.
And create a new java file "App.java".
smims@ubuntu:~/mahout/trunk$mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.smims.recommenderintro -DartifactId=project

smims@ubuntu:~/mahout/trunk$mvn archetype:create -DarchetypeGroupId=org.apache.maven.archetypes -DgroupId=com.smims.recommenderintro -DartifactId=project
[INFO] ----------------------------------------------------------------------------
[INFO] Using following parameters for creating project from Old (1.x) Archetype: maven-archetype-quickstart:RELEASE
[INFO] ----------------------------------------------------------------------------
[INFO] Parameter: groupId, Value: com.smims.recommenderintro
[INFO] Parameter: packageName, Value: com.smims.recommenderintro
[INFO] Parameter: package, Value: com.smims.recommenderintro
[INFO] Parameter: artifactId, Value: project
[INFO] Parameter: basedir, Value: /home/smims/mahout/trunk
[INFO] Parameter: version, Value: 1.0-SNAPSHOT
[INFO] project created from Old (1.x) Archetype in dir: /home/smims/mahout/trunk/project
[INFO] ------------------------------------------------------------------------
[INFO] Reactor Summary:
[INFO] 
[INFO] Mahout Build Tools ................................ SKIPPED
[INFO] Apache Mahout ..................................... SUCCESS [22.584s]
[INFO] Mahout Math ....................................... SKIPPED
[INFO] Mahout Core ....................................... SKIPPED
[INFO] Mahout Integration ................................ SKIPPED
[INFO] Mahout Examples ................................... SKIPPED
[INFO] Mahout Release Package ............................ SKIPPED
[INFO] Mahout Math/Scala wrappers ........................ SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 33.614s
[INFO] Finished at: Wed Nov 27 11:00:53 CST 2013
[INFO] Final Memory: 18M/44M
[INFO] ------------------------------------------------------------------------

Step2:
Change to the project directory. Test the new project, it should print the "Hello world!" message.
smims@ubuntu:~/mahout/trunk/project$mvn compile
smims@ubuntu:~/mahout/trunk/project$mvn exec:java -Dexec.mainClass="com.smims.recommenderintro.App"

smims@ubuntu:~/mahout/trunk/project$mvn compile
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building project 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ project ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/smims/mahout/trunk/project/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ project ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /home/smims/mahout/trunk/project/target/classes
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.181s
[INFO] Finished at: Wed Nov 27 11:09:03 CST 2013
[INFO] Final Memory: 9M/22M
[INFO] ------------------------------------------------------------------------ 

smims@ubuntu:~/mahout/trunk/project$mvn exec:java -Dexec.mainClass="com.smims.recommenderintro.App"
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building project 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) @ project >>>
[INFO] 
[INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) @ project <<<
[INFO] 
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ project ---
Hello World!
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.945s
[INFO] Finished at: Wed Nov 27 11:13:45 CST 2013
[INFO] Final Memory: 11M/26M
[INFO] ------------------------------------------------------------------------

Step3:
Create a text file "intro.csv" in the project directory, same as the pom.xml.
For example:User 1 has preference value 3.0 for item 102.
 
1,101,5.0
1,102,3.0
1,103,2.5
2,101,2.0
2,102,2.5
2,103,5.0
2,104,2.0
3,101,2.5
3,104,4.0
3,105,4.5
3,107,5.0
4,101,5.0
4,103,3.0
4,104,4.5
4,106,4.0
5,101,4.0
5,102,3.0
5,103,2.0
5,104,4.0
5,105,3.5
5,106,4.0

Step4:
Create a recommender engine in java code, open and edit "App.java".
package com.smims.recommenderintro;

import org.apache.mahout.cf.taste.impl.model.file.*;
import org.apache.mahout.cf.taste.impl.neighborhood.*;
import org.apache.mahout.cf.taste.impl.recommender.*;
import org.apache.mahout.cf.taste.impl.similarity.*;
import org.apache.mahout.cf.taste.model.*;
import org.apache.mahout.cf.taste.neighborhood.*;
import org.apache.mahout.cf.taste.recommender.*;
import org.apache.mahout.cf.taste.similarity.*;
import java.io.*;
import java.util.*;

class App 
{
 public static void main(String[] args) throws Exception 
 {
  // Load data file
  DataModel model = new FileDataModel (new File("intro.csv")); 
  UserSimilarity similarity = new PearsonCorrelationSimilarity (model);
  UserNeighborhood neighborhood = new NearestNUserNeighborhood (2, similarity, model);
  
  // Create recommender engine
  Recommender recommender = new GenericUserBasedRecommender (model, neighborhood, similarity); 
  
  // For user 1, recommend 1 item
  List<RecommendedItem> recommendations = recommender.recommend(1, 1); 
  
  for (RecommendedItem recommendation : recommendations) 
  {
   System.out.println(recommendation);
  }
 }
} 

Step5:
Before compile the code, we need setting the Mahout dependencies in the pom.xml.
Add these lines under the <dependencies> section in the pom.xml.
    
      org.apache.mahout
      mahout-core
      0.9-SNAPSHOT
    
    
      org.apache.mahout
      mahout-math
      0.9-SNAPSHOT
    
    
      org.apache.mahout
      mahout-math
      0.9-SNAPSHOT
      test-jar
      test
    
    
      org.apache.mahout
      mahout-utils
      0.5
    

Step6:
Compile and run the code.
smims@ubuntu:~/mahout/trunk/project$mvn compile
smims@ubuntu:~/mahout/trunk/project$mvn exec:java -Dexec.mainClass="com.smims.recommenderintro.App"
smims@ubuntu:~/mahout/trunk/project$mvn compile
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building project 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ project ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /home/smims/mahout/trunk/project/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ project ---
[INFO] Nothing to compile - all classes are up to date
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.995s
[INFO] Finished at: Wed Nov 27 12:10:57 CST 2013
[INFO] Final Memory: 7M/18M
[INFO] ------------------------------------------------------------------------
smims@ubuntu:~/mahout/trunk/project$mvn exec:java -Dexec.mainClass="com.smims.recommenderintro.App"
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building project 1.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) @ project >>>
[INFO] 
[INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) @ project <<<
[INFO] 
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ project ---
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
RecommendedItem[item:104, value:4.257081]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.254s
[INFO] Finished at: Wed Nov 27 12:13:01 CST 2013
[INFO] Final Memory: 12M/29M
[INFO] ------------------------------------------------------------------------


The recommend result for the user 1 is the 104 item.

No comments:

Post a Comment