You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@mahout.apache.org by Giuseppe <gi...@uniba.it> on 2014/01/16 12:02:53 UTC

problem with recommendation algorithm

Hi guys,

I'm new with mahout. I'm using it for an experimentation with  
recommender system.
I'm using this code:

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.*;
import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;

class Example5_GroupLensRecommender {

   private Example5_GroupLensRecommender() {
   }

   public static void main(String[] args) throws Exception {

     // Istanzia il DataModel e crea alcune statistiche
     DataModel model = new FileDataModel(new  
File("/Users/giuseppe/NetBeansProjects/MyFirsRS/src/Mrating.csv"));
     System.out.println("\nItems:"+model.getNumItems());
     System.out.println("Users:"+model.getNumUsers());

     // Preferences for User 1
     //PreferenceArray p = model.getPreferencesFromUser(1);
     //    System.out.println("\nPreferences for User 1 ("+p.length()+")");
     //for(int i=0; i<p.length(); i++) {
     //    System.out.println(p.getItemID(i)+"\t"+p.getValue(i));
     //}

     // Definisce i meccanismi di calcolo della similarita
     //UserSimilarity similarity = new PearsonCorrelationSimilarity(model);
     UserSimilarity similarity = new SpearmanCorrelationSimilarity(model);
     //UserSimilarity similarity = new EuclideanDistanceSimilarity(model);

     System.out.println("\nSimilarity between User 1 and 250'");
     System.out.println(similarity.userSimilarity(1, 2));

     System.out.println("\nSimilarity between User 1 and 500");
     System.out.println(similarity.userSimilarity(55, 50));

     // Calcolo dei Neighbors
     UserNeighborhood neighborhood =
       new NearestNUserNeighborhood(3, similarity, model);
       //new ThresholdUserNeighborhood(0.2, similarity, model);

     // Mostra i neighbor
     System.out.println("\nNeighbors for User 1");

     long[] neighbors = neighborhood.getUserNeighborhood(1);
     for(int i=0; i<neighbors.length; i++) {
         System.out.println("User "+neighbors[i]+"\tsim:  
"+similarity.userSimilarity(1, neighbors[i]));
     }

     // Istanzia il motore di raccomandazione
     Recommender recommender = new GenericUserBasedRecommender(
         model, neighborhood, similarity);

     // Stima del Ratings
     System.out.println("\nPreference Estimation for Item 103 and User1: "
             +recommender.estimatePreference(1, 103));

     // Calcolo delle Raccomandazioni
     List<RecommendedItem> recommendations =
         recommender.recommend(1, 10);

     // Top-1 Recommendation
     System.out.println("\nTop-1 recommendation:  
"+recommendations.get(0).getItemID()+"\t"
     +"Score: "+recommendations.get(0).getValue());

     // Stampa tutte le raccomandazioni
     for (RecommendedItem recommendation : recommendations) {
       System.out.println("\n"+recommendation);
     }

   }

}

If I run this file (I'm using Mahout under Netbeans) I receive this error:

Similarity between User 1 and 250'
Exception in thread "main"  
org.apache.mahout.cf.taste.common.NoSuchUserException: 1
         at  
org.apache.mahout.cf.taste.impl.model.GenericDataModel.getPreferencesFromUser(GenericDataModel.java:213)
         at  
org.apache.mahout.cf.taste.impl.model.file.FileDataModel.getPreferencesFromUser(FileDataModel.java:642)
         at  
org.apache.mahout.cf.taste.impl.similarity.SpearmanCorrelationSimilarity.userSimilarity(SpearmanCorrelationSimilarity.java:49)
         at  
it.uniba.dib.swap.recsys.mahout.Example5_GroupLensRecommender.main(Example5_GroupLensRecommender.java:39)
Java Result: 1

Can someone help me to understand what is the problem?
Thanks.

Dott. Giuseppe Ricci
Dottorando in Informatica XXVI ciclo
Dipartimento di Informatica
4° piano Stanza Lab. SWAP
Telefono: +39-080-5442298
E-mail: giuseppe.ricci@uniba.it


Re: problem with recommendation algorithm

Posted by Sebastian Schelter <ss...@googlemail.com>.
Does the csv file that you load contain user with id 1 ?



On 01/16/2014 12:02 PM, Giuseppe wrote:
> Hi guys,
>
> I'm new with mahout. I'm using it for an experimentation with
> recommender system.
> I'm using this code:
>
> 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.*;
> import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
>
> class Example5_GroupLensRecommender {
>
>    private Example5_GroupLensRecommender() {
>    }
>
>    public static void main(String[] args) throws Exception {
>
>      // Istanzia il DataModel e crea alcune statistiche
>      DataModel model = new FileDataModel(new
> File("/Users/giuseppe/NetBeansProjects/MyFirsRS/src/Mrating.csv"));
>      System.out.println("\nItems:"+model.getNumItems());
>      System.out.println("Users:"+model.getNumUsers());
>
>      // Preferences for User 1
>      //PreferenceArray p = model.getPreferencesFromUser(1);
>      //    System.out.println("\nPreferences for User 1 ("+p.length()+")");
>      //for(int i=0; i<p.length(); i++) {
>      //    System.out.println(p.getItemID(i)+"\t"+p.getValue(i));
>      //}
>
>      // Definisce i meccanismi di calcolo della similarita
>      //UserSimilarity similarity = new PearsonCorrelationSimilarity(model);
>      UserSimilarity similarity = new SpearmanCorrelationSimilarity(model);
>      //UserSimilarity similarity = new EuclideanDistanceSimilarity(model);
>
>      System.out.println("\nSimilarity between User 1 and 250'");
>      System.out.println(similarity.userSimilarity(1, 2));
>
>      System.out.println("\nSimilarity between User 1 and 500");
>      System.out.println(similarity.userSimilarity(55, 50));
>
>      // Calcolo dei Neighbors
>      UserNeighborhood neighborhood =
>        new NearestNUserNeighborhood(3, similarity, model);
>        //new ThresholdUserNeighborhood(0.2, similarity, model);
>
>      // Mostra i neighbor
>      System.out.println("\nNeighbors for User 1");
>
>      long[] neighbors = neighborhood.getUserNeighborhood(1);
>      for(int i=0; i<neighbors.length; i++) {
>          System.out.println("User "+neighbors[i]+"\tsim:
> "+similarity.userSimilarity(1, neighbors[i]));
>      }
>
>      // Istanzia il motore di raccomandazione
>      Recommender recommender = new GenericUserBasedRecommender(
>          model, neighborhood, similarity);
>
>      // Stima del Ratings
>      System.out.println("\nPreference Estimation for Item 103 and User1: "
>              +recommender.estimatePreference(1, 103));
>
>      // Calcolo delle Raccomandazioni
>      List<RecommendedItem> recommendations =
>          recommender.recommend(1, 10);
>
>      // Top-1 Recommendation
>      System.out.println("\nTop-1 recommendation:
> "+recommendations.get(0).getItemID()+"\t"
>      +"Score: "+recommendations.get(0).getValue());
>
>      // Stampa tutte le raccomandazioni
>      for (RecommendedItem recommendation : recommendations) {
>        System.out.println("\n"+recommendation);
>      }
>
>    }
>
> }
>
> If I run this file (I'm using Mahout under Netbeans) I receive this error:
>
> Similarity between User 1 and 250'
> Exception in thread "main"
> org.apache.mahout.cf.taste.common.NoSuchUserException: 1
>          at
> org.apache.mahout.cf.taste.impl.model.GenericDataModel.getPreferencesFromUser(GenericDataModel.java:213)
>
>          at
> org.apache.mahout.cf.taste.impl.model.file.FileDataModel.getPreferencesFromUser(FileDataModel.java:642)
>
>          at
> org.apache.mahout.cf.taste.impl.similarity.SpearmanCorrelationSimilarity.userSimilarity(SpearmanCorrelationSimilarity.java:49)
>
>          at
> it.uniba.dib.swap.recsys.mahout.Example5_GroupLensRecommender.main(Example5_GroupLensRecommender.java:39)
>
> Java Result: 1
>
> Can someone help me to understand what is the problem?
> Thanks.
>
> Dott. Giuseppe Ricci
> Dottorando in Informatica XXVI ciclo
> Dipartimento di Informatica
> 4° piano Stanza Lab. SWAP
> Telefono: +39-080-5442298
> E-mail: giuseppe.ricci@uniba.it
>