Saturday 29 August 2015

Mahout: GenericDataModel


GenericDataModel class provides following constructors to instantiate objects.

public GenericDataModel(FastByIDMap<PreferenceArray> userData)
Creates a new GenericDataModel from the given users (and their preferences)

public GenericDataModel(FastByIDMap<PreferenceArray> userData, FastByIDMap<FastByIDMap<Long>> timestamps)
Creates a new GenericDataModel from the given users (and their preferences). User IDs are mapped to maps of item IDs to Long timestamps.
import org.apache.mahout.cf.taste.impl.common.FastByIDMap;
import org.apache.mahout.cf.taste.impl.common.FastIDSet;
import org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator;
import org.apache.mahout.cf.taste.impl.model.GenericDataModel;
import org.apache.mahout.cf.taste.impl.model.GenericPreference;
import org.apache.mahout.cf.taste.impl.model.GenericUserPreferenceArray;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.model.Preference;
import org.apache.mahout.cf.taste.model.PreferenceArray;

public class GenericDataModelEx {

 public static DataModel getDataModel() {
  FastByIDMap<PreferenceArray> preferences = new FastByIDMap<PreferenceArray>();

  PreferenceArray array1 = new GenericUserPreferenceArray(4);
  Preference pref = new GenericPreference(1, 4, 3);
  array1.set(0, pref);

  pref = new GenericPreference(1, 7, 2);
  array1.set(1, pref);

  pref = new GenericPreference(1, 8, 2);
  array1.set(2, pref);

  pref = new GenericPreference(1, 10, 1);
  array1.set(3, pref);

  PreferenceArray array2 = new GenericUserPreferenceArray(5);
  pref = new GenericPreference(2, 3, 2);
  array2.set(0, pref);

  pref = new GenericPreference(2, 4, 3);
  array2.set(1, pref);

  pref = new GenericPreference(2, 6, 5);
  array2.set(2, pref);

  pref = new GenericPreference(2, 7, 1);
  array2.set(3, pref);

  pref = new GenericPreference(2, 9, 4);
  array2.set(4, pref);

  preferences.put(1, array1);
  preferences.put(2, array2);

  DataModel model = new GenericDataModel(preferences);
  return model;
 }

 public static void main(String args[]) throws Exception {
  DataModel model = getDataModel();

  System.out.println("Maximum preference : " + model.getMaxPreference());
  System.out.println("Minimum preference : " + model.getMinPreference());
  System.out.println("Number of items : " + model.getNumItems());
  System.out.println("Number Of Users : " + model.getNumUsers());

  System.out.println("Item ids for user 1");
  FastIDSet set = model.getItemIDsFromUser(1);
  LongPrimitiveIterator iter = set.iterator();
  while (iter.hasNext()) {
   System.out.print(iter.nextLong() + " ");
  }

  System.out.println("\nItem ids for user 2");
  set = model.getItemIDsFromUser(2);
  iter = set.iterator();
  while (iter.hasNext()) {
   System.out.print(iter.nextLong() + " ");
  }

 }
}


Output
[main] INFO org.apache.mahout.cf.taste.impl.model.GenericDataModel - Processed 2 users
Maximum preference : 5.0
Minimum preference : 1.0
Number of items : 7
Number Of Users : 2
Item ids for user 1
4 7 8 10 
Item ids for user 2
3 4 6 7 9


 
Prevoius                                                 Next                                                 Home

No comments:

Post a Comment