Thursday 20 August 2015

Mahout: PreferenceArray

PreferenceArray represents Collection of Preferences using Array like API. There are four implementations for PreferenceArray API.
a.   GenericUserPreferenceArray
b.   GenericItemPreferenceArray
c.    BooleanUserPreferenceArray
d.   BooleanItemPreferenceArray

For example, GenericUserPreferenceArray represents all preferences associated with one user. Implementation of GenericUserPreferenceArray maintains two parallel arrays, of item IDs and values.

To represent following preference data, we need four Preference objects.

1,4,3.0
1,7,2.0
1,8,2.0
1,10,1.0


Same data can be represented using one PreferenceArray. Following example uses GenericUserPreferenceArray.
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.Preference;
import org.apache.mahout.cf.taste.model.PreferenceArray;

public class PreferenceArrayEx {
 public static void main(String args[]){
  PreferenceArray array = new GenericUserPreferenceArray(4);
  Preference pref = new GenericPreference(1, 4, 3 );
  array.set(0, pref);
  
  pref = new GenericPreference(1, 7, 2 );
  array.set(1, pref);
  
  pref = new GenericPreference(1, 8, 2 );
  array.set(2, pref);
  
  pref = new GenericPreference(1, 10, 1 );
  array.set(3, pref);

  for(int i=0; i<4; i++){
   System.out.println(array.getUserID(i) + "," + array.getItemID(i) + "," + array.getValue(i));
  }
  
 }
}


Output
1,4,3.0
1,7,2.0
1,8,2.0
1,10,1.0


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment