Package es.upm.etsisi.cf4j.data
Class DataModel
- java.lang.Object
-
- es.upm.etsisi.cf4j.data.DataModel
-
- All Implemented Interfaces:
Serializable
public class DataModel extends Object implements Serializable
This class manages all the information related with a collaborative filtering based recommender system. A DataModel should be instantiated from a DataSet. This class also provides the possibility to manage serialized files based on instances of this class.- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addRating(int userIndex, int itemIndex, double rating)
Adds a single (training) rating to the DataModel.void
addTestRating(int testUserIndex, int testItemIndex, double rating)
Adds a single test rating to the DataModel.void
exportToCSV(String path, String baseName)
Exports datamodel into a CSV for train ratings and other CSV for test ratings.void
exportToCSV(String path, String baseName, boolean useIndexesAsIds)
Exports datamodel into a CSV for train ratings and other CSV for test ratings.void
exportToCSV(String path, String baseName, String separator)
Exports datamodel into a CSV for train ratings and other CSV for test ratings.void
exportToCSV(String path, String baseName, String separator, boolean useIndexesAsIds)
Exports datamodel into a CSV for train ratings and other CSV for test ratings.int
findItemIndex(String itemId)
Finds the itemIndex of an Item at the Items' array given its unique id.int
findTestItemIndex(String itemId)
Finds the testItemIndex of a TestItem at the TestItem' array given its unique id.int
findTestUserIndex(String userId)
Finds the testUserIndex of a TestUser at the TestUsers' array given his/her unique id.int
findUserIndex(String userId)
Finds the userIndex of a User at the Users' array given his/her unique id.DataBank
getDataBank()
Gets the DataBank instance that stores heterogeneous information related to the DataModel.Item
getItem(int itemIndex)
Gets an Item by its index.Item[]
getItems()
Gets the array of Items.double
getMaxRating()
Gets the maximum (training) rating.double
getMaxTestRating()
Gets the maximum test rating.double
getMinRating()
Gets the minimum (training) rating.double
getMinTestRating()
Gets the minimum test rating.int
getNumberOfItems()
Gets the number of items contained in the DataModel.int
getNumberOfRatings()
Return the number of ratings contained in the DataModel.int
getNumberOfTestItems()
Gets the number of test items contained in the DataModel.int
getNumberOfTestRatings()
Return the number of test ratings contained in the DataModel.int
getNumberOfTestUsers()
Gets the number of test users contained in the DataModel.int
getNumberOfUsers()
Gets the number of users contained in the DataModel.double
getRatingAverage()
Gets the average of (training) ratings.TestItem
getTestItem(int testItemIndex)
Gets a TestItem by its index.TestItem[]
getTestItems()
Gets the array of TestItems.double
getTestRatingAverage()
Gets the average of test ratings.TestUser
getTestUser(int testUserIndex)
Gets a TestUser by his/her test index.TestUser[]
getTestUsers()
Gets the array of TestUsers.User
getUser(int userIndex)
Gets an User by his/her index.User[]
getUsers()
Gets the array of Users.static DataModel
load(String filePath)
Loads a DataModel from a previously serialized file (see save() method).void
save(String filePath)
Saves the content of the DataModel in a serialized file.String
toString()
-
-
-
Constructor Detail
-
DataModel
public DataModel(DataSet dataset)
This constructor initializes the DataModel with the contents of the given DataSet. Data contained in the DataSet is stored in 4 different arrays: user, testUser, item and testItems. Keeping the main behaviour of the collaborative filtering in mind, any TestUser could have voted both Item or TestItem. In the same way, any testItem could have been voted by a User and TestUser. In the other hand, Users and Items will not have a test part.- Parameters:
dataset
- DataSet to be added to the DataModel.
-
-
Method Detail
-
save
public void save(String filePath) throws IOException
Saves the content of the DataModel in a serialized file.- Parameters:
filePath
- Path where the file will be stored, filename and extension should be included in the path.- Throws:
IOException
- When the file is not accessible by the system with write permissions.
-
load
public static DataModel load(String filePath) throws IOException, ClassNotFoundException
Loads a DataModel from a previously serialized file (see save() method).- Parameters:
filePath
- Path where the file will be loaded, filename and extension should be included in the path.- Returns:
- If te file was successfully loaded, this method returns the DataModel.
- Throws:
IOException
- When the file is not accessible by the system with reading permissions.ClassNotFoundException
- When the file exist and is accessible, but it doesn't contains a valid instance.
-
addTestRating
public void addTestRating(int testUserIndex, int testItemIndex, double rating)
Adds a single test rating to the DataModel.- Parameters:
testUserIndex
- Index of the TestUser at the DataModeltestItemIndex
- Index of the TestItem at the DataModelrating
- Rating value
-
addRating
public void addRating(int userIndex, int itemIndex, double rating)
Adds a single (training) rating to the DataModel.- Parameters:
userIndex
- Index of the User at the DataModelitemIndex
- Index of the Item at the DataModelrating
- Rating value
-
getUsers
public User[] getUsers()
Gets the array of Users. If you need a specific User it is recommended to use the getUser() method.- Returns:
- Array of Users
-
getTestUsers
public TestUser[] getTestUsers()
Gets the array of TestUsers. If you need a specific TestUser it is recommended to use the getTestUser() method.- Returns:
- Array of TestUsers
-
getItems
public Item[] getItems()
Gets the array of Items. If you need a specific Item it is recommended to use the getItem() method.- Returns:
- Array of Items
-
getTestItems
public TestItem[] getTestItems()
Gets the array of TestItems. If you need a specific TestItem it is recommended to use the getTestItem() method.- Returns:
- Array of TestItems
-
getDataBank
public DataBank getDataBank()
Gets the DataBank instance that stores heterogeneous information related to the DataModel.- Returns:
- DataBank instance
-
getNumberOfUsers
public int getNumberOfUsers()
Gets the number of users contained in the DataModel.- Returns:
- Number of users
-
getUser
public User getUser(int userIndex)
Gets an User by his/her index.- Parameters:
userIndex
- Index of the User in the Users' array inside the DataModel.- Returns:
- User located at given userIndex
-
findUserIndex
public int findUserIndex(String userId)
Finds the userIndex of a User at the Users' array given his/her unique id.- Parameters:
userId
- User id to be searched.- Returns:
- userIndex if the User exists or -1 if doesn't.
-
getNumberOfTestUsers
public int getNumberOfTestUsers()
Gets the number of test users contained in the DataModel.- Returns:
- Number of test users
-
getTestUser
public TestUser getTestUser(int testUserIndex)
Gets a TestUser by his/her test index.- Parameters:
testUserIndex
- Index of the TestUser in the TestUsers' array inside the DataModel.- Returns:
- TestUser located at given testUserIndex
-
findTestUserIndex
public int findTestUserIndex(String userId)
Finds the testUserIndex of a TestUser at the TestUsers' array given his/her unique id.- Parameters:
userId
- User id to be searched.- Returns:
- testUserIndex if the TestUser exists or -1 if doesn't.
-
getNumberOfItems
public int getNumberOfItems()
Gets the number of items contained in the DataModel.- Returns:
- Number of items
-
getItem
public Item getItem(int itemIndex)
Gets an Item by its index.- Parameters:
itemIndex
- Index of the Item in the Items' array inside the DataModel.- Returns:
- Item located at given itemIndex
-
findItemIndex
public int findItemIndex(String itemId)
Finds the itemIndex of an Item at the Items' array given its unique id.- Parameters:
itemId
- Item id to be searched.- Returns:
- itemIndex if the Item exists or -1 if doesn't.
-
getNumberOfTestItems
public int getNumberOfTestItems()
Gets the number of test items contained in the DataModel.- Returns:
- Number of test items
-
getTestItem
public TestItem getTestItem(int testItemIndex)
Gets a TestItem by its index.- Parameters:
testItemIndex
- Index of the TestItem in the TestItem' array inside the DataModel.- Returns:
- TestItem located at given testItemIndex
-
findTestItemIndex
public int findTestItemIndex(String itemId)
Finds the testItemIndex of a TestItem at the TestItem' array given its unique id.- Parameters:
itemId
- Item id to be searched.- Returns:
- testItemIndex if the TestItem exists or -1 if doesn't.
-
getMinRating
public double getMinRating()
Gets the minimum (training) rating.- Returns:
- Minimum rating
-
getMaxRating
public double getMaxRating()
Gets the maximum (training) rating.- Returns:
- Maximum rating
-
getRatingAverage
public double getRatingAverage()
Gets the average of (training) ratings.- Returns:
- Rating average value
-
getMinTestRating
public double getMinTestRating()
Gets the minimum test rating.- Returns:
- Minimum test rating
-
getMaxTestRating
public double getMaxTestRating()
Gets the maximum test rating.- Returns:
- Maximum test rating
-
getTestRatingAverage
public double getTestRatingAverage()
Gets the average of test ratings.- Returns:
- Test rating average value
-
getNumberOfRatings
public int getNumberOfRatings()
Return the number of ratings contained in the DataModel.- Returns:
- Number of ratings
-
getNumberOfTestRatings
public int getNumberOfTestRatings()
Return the number of test ratings contained in the DataModel.- Returns:
- Number of test ratings
-
exportToCSV
public void exportToCSV(String path, String baseName, boolean useIndexesAsIds) throws IOException
Exports datamodel into a CSV for train ratings and other CSV for test ratings.- Parameters:
path
- Path where store the filesbaseName
- Base name for CSV files. SuffixesuseIndexesAsIds
- Use CF4J indexes as ids- Throws:
IOException
- IOException
-
exportToCSV
public void exportToCSV(String path, String baseName) throws IOException
Exports datamodel into a CSV for train ratings and other CSV for test ratings.- Parameters:
path
- Path where store the filesbaseName
- Base name for CSV files. Suffixes.- Throws:
IOException
- IOException
-
exportToCSV
public void exportToCSV(String path, String baseName, String separator) throws IOException
Exports datamodel into a CSV for train ratings and other CSV for test ratings.- Parameters:
path
- Path where store the filesbaseName
- Base name for CSV files. Suffixes.separator
- CSV separator- Throws:
IOException
- IOException
-
exportToCSV
public void exportToCSV(String path, String baseName, String separator, boolean useIndexesAsIds) throws IOException
Exports datamodel into a CSV for train ratings and other CSV for test ratings.- Parameters:
path
- Path where store the filesbaseName
- Base name for CSV files. Suffixesseparator
- CSV separatoruseIndexesAsIds
- Use CF4J indexes as ids- Throws:
IOException
- IOException
-
-