org.apache.mahout.cf.taste.impl.model
Class GenericDataModel

java.lang.Object
  extended by org.apache.mahout.cf.taste.impl.model.AbstractDataModel
      extended by org.apache.mahout.cf.taste.impl.model.GenericDataModel
All Implemented Interfaces:
Serializable, Refreshable, DataModel

public final class GenericDataModel
extends AbstractDataModel

A simple DataModel which uses a given List of users as its data source. This implementation is mostly useful for small experiments and is not recommended for contexts where performance is important.

See Also:
Serialized Form

Constructor Summary
GenericDataModel(DataModel dataModel)
          Deprecated. without direct replacement. Consider toDataMap(DataModel) with GenericDataModel(FastByIDMap)
GenericDataModel(FastByIDMap<PreferenceArray> userData)
           Creates a new GenericDataModel from the given users (and their preferences).
GenericDataModel(FastByIDMap<PreferenceArray> userData, FastByIDMap<FastByIDMap<Long>> timestamps)
           Creates a new GenericDataModel from the given users (and their preferences).
 
Method Summary
 LongPrimitiveArrayIterator getItemIDs()
           
 FastIDSet getItemIDsFromUser(long userID)
           
 int getNumItems()
           
 int getNumUsers()
           
 int getNumUsersWithPreferenceFor(long itemID)
           
 int getNumUsersWithPreferenceFor(long itemID1, long itemID2)
           
 PreferenceArray getPreferencesForItem(long itemID)
           
 PreferenceArray getPreferencesFromUser(long userID)
           
 Long getPreferenceTime(long userID, long itemID)
          Retrieves the time at which a preference value from a user and item was set, if known.
 Float getPreferenceValue(long userID, long itemID)
          Retrieves the preference value for a single user and item.
 FastByIDMap<PreferenceArray> getRawItemData()
          This is used mostly internally to the framework, and shouldn't be relied upon otherwise.
 FastByIDMap<PreferenceArray> getRawUserData()
          This is used mostly internally to the framework, and shouldn't be relied upon otherwise.
 LongPrimitiveArrayIterator getUserIDs()
           
 boolean hasPreferenceValues()
           
 void refresh(Collection<Refreshable> alreadyRefreshed)
           Triggers "refresh" -- whatever that means -- of the implementation.
 void removePreference(long userID, long itemID)
           Removes a particular preference for a user.
 void setPreference(long userID, long itemID, float value)
           Sets a particular preference (item plus rating) for a user.
static FastByIDMap<PreferenceArray> toDataMap(DataModel dataModel)
          Exports the simple user IDs and preferences in the data model.
static FastByIDMap<PreferenceArray> toDataMap(FastByIDMap<Collection<Preference>> data, boolean byUser)
          Swaps, in-place, Lists for arrays in Map values .
 String toString()
           
 
Methods inherited from class org.apache.mahout.cf.taste.impl.model.AbstractDataModel
getMaxPreference, getMinPreference, setMaxPreference, setMinPreference
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

GenericDataModel

public GenericDataModel(FastByIDMap<PreferenceArray> userData)

Creates a new GenericDataModel from the given users (and their preferences). This DataModel retains all this information in memory and is effectively immutable.

Parameters:
userData - users to include; (see also toDataMap(FastByIDMap, boolean))

GenericDataModel

public GenericDataModel(FastByIDMap<PreferenceArray> userData,
                        FastByIDMap<FastByIDMap<Long>> timestamps)

Creates a new GenericDataModel from the given users (and their preferences). This DataModel retains all this information in memory and is effectively immutable.

Parameters:
userData - users to include; (see also toDataMap(FastByIDMap, boolean))
timestamps - optionally, provided timestamps of preferences as milliseconds since the epoch. User IDs are mapped to maps of item IDs to Long timestamps.

GenericDataModel

@Deprecated
public GenericDataModel(DataModel dataModel)
                 throws TasteException
Deprecated. without direct replacement. Consider toDataMap(DataModel) with GenericDataModel(FastByIDMap)

Creates a new GenericDataModel containing an immutable copy of the data from another given DataModel.

Parameters:
dataModel - DataModel to copy
Throws:
TasteException - if an error occurs while retrieving the other DataModel's users
Method Detail

toDataMap

public static FastByIDMap<PreferenceArray> toDataMap(FastByIDMap<Collection<Preference>> data,
                                                     boolean byUser)
Swaps, in-place, Lists for arrays in Map values .

Returns:
input value

toDataMap

public static FastByIDMap<PreferenceArray> toDataMap(DataModel dataModel)
                                              throws TasteException
Exports the simple user IDs and preferences in the data model.

Returns:
a FastByIDMap mapping user IDs to PreferenceArrays representing that user's preferences
Throws:
TasteException

getRawUserData

public FastByIDMap<PreferenceArray> getRawUserData()
This is used mostly internally to the framework, and shouldn't be relied upon otherwise.


getRawItemData

public FastByIDMap<PreferenceArray> getRawItemData()
This is used mostly internally to the framework, and shouldn't be relied upon otherwise.


getUserIDs

public LongPrimitiveArrayIterator getUserIDs()
Returns:
all user IDs in the model, in order

getPreferencesFromUser

public PreferenceArray getPreferencesFromUser(long userID)
                                       throws NoSuchUserException
Parameters:
userID - ID of user to get prefs for
Returns:
user's preferences, ordered by item ID
Throws:
NoSuchUserException - if there is no such user

getItemIDsFromUser

public FastIDSet getItemIDsFromUser(long userID)
                             throws TasteException
Parameters:
userID - ID of user to get prefs for
Returns:
IDs of items user expresses a preference for
Throws:
NoSuchUserException - if the user does not exist
TasteException - if an error occurs while accessing the data

getItemIDs

public LongPrimitiveArrayIterator getItemIDs()
Returns:
a LongPrimitiveIterator of all item IDs in the model, in order

getPreferencesForItem

public PreferenceArray getPreferencesForItem(long itemID)
                                      throws NoSuchItemException
Parameters:
itemID - item ID
Returns:
all existing Preferences expressed for that item, ordered by user ID, as an array
Throws:
NoSuchItemException - if the item does not exist

getPreferenceValue

public Float getPreferenceValue(long userID,
                                long itemID)
                         throws TasteException
Description copied from interface: DataModel
Retrieves the preference value for a single user and item.

Parameters:
userID - user ID to get pref value from
itemID - item ID to get pref value for
Returns:
preference value from the given user for the given item or null if none exists
Throws:
NoSuchUserException - if the user does not exist
TasteException - if an error occurs while accessing the data

getPreferenceTime

public Long getPreferenceTime(long userID,
                              long itemID)
                       throws TasteException
Description copied from interface: DataModel
Retrieves the time at which a preference value from a user and item was set, if known. Time is expressed in the usual way, as a number of milliseconds since the epoch.

Parameters:
userID - user ID for preference in question
itemID - item ID for preference in question
Returns:
time at which preference was set or null if no preference exists or its time is not known
Throws:
NoSuchUserException - if the user does not exist
TasteException - if an error occurs while accessing the data

getNumItems

public int getNumItems()
Returns:
total number of items known to the model. This is generally the union of all items preferred by at least one user but could include more.

getNumUsers

public int getNumUsers()
Returns:
total number of users known to the model.

getNumUsersWithPreferenceFor

public int getNumUsersWithPreferenceFor(long itemID)
Parameters:
itemID - item ID to check for
Returns:
the number of users who have expressed a preference for the item

getNumUsersWithPreferenceFor

public int getNumUsersWithPreferenceFor(long itemID1,
                                        long itemID2)
Parameters:
itemID1 - first item ID to check for
itemID2 - second item ID to check for
Returns:
the number of users who have expressed a preference for the items

removePreference

public void removePreference(long userID,
                             long itemID)
Description copied from interface: DataModel

Removes a particular preference for a user.

Parameters:
userID - user from which to remove preference
itemID - item to remove preference for

setPreference

public void setPreference(long userID,
                          long itemID,
                          float value)
Description copied from interface: DataModel

Sets a particular preference (item plus rating) for a user.

Parameters:
userID - user to set preference for
itemID - item to set preference for
value - preference value

refresh

public void refresh(Collection<Refreshable> alreadyRefreshed)
Description copied from interface: Refreshable

Triggers "refresh" -- whatever that means -- of the implementation. The general contract is that any Refreshable should always leave itself in a consistent, operational state, and that the refresh atomically updates internal state from old to new.

Parameters:
alreadyRefreshed - Refreshables that are known to have already been refreshed as a result of an initial call to a Refreshable.refresh(Collection) method on some object. This ensure that objects in a refresh dependency graph aren't refreshed twice needlessly.

hasPreferenceValues

public boolean hasPreferenceValues()
Returns:
true if this implementation actually stores and returns distinct preference values; that is, if it is not a 'boolean' DataModel

toString

public String toString()
Overrides:
toString in class Object


Copyright © 2008–2014 The Apache Software Foundation. All rights reserved.