Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rated items filter #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,18 @@

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm

*.iml

## Directory-based project format:
.idea/

# IntelliJ
/out/
*.iml
/build/

# mpeltonen/sbt-idea plugin
.idea_modules/
19 changes: 0 additions & 19 deletions CARSKit.iml

This file was deleted.

33 changes: 33 additions & 0 deletions build.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<project name="CARSKit" default="jar">

<property name="version" value="v0.2.1-alpha"/>
<property name="src.dir" value="src"/>
<property name="build.dir" value="build"/>
<property name="jar.dir" value="jar"/>
<property name="lib.dir" value="lib" />
<path id="lib.path.id">
<fileset dir="${lib.dir}" />
</path>

<target name="clean">
<delete dir="${build.dir}"/>
</target>

<target name="compile">
<mkdir dir="${build.dir}"/>
<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="lib.path.id">
<compilerarg value="-XDignore.symbol.file"/>
</javac>
</target>

<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${ant.project.name}-${version}.jar" basedir="${build.dir}">
<zipgroupfileset dir="${lib.dir}" includes="*.jar"/>
<manifest>
<attribute name="Main-Class" value="carskit.main.CARSKit"/>
</manifest>
</jar>
</target>

</project>
Binary file added jar/CARSKit-v0.2.1-alpha.jar
Binary file not shown.
3 changes: 0 additions & 3 deletions src/META-INF/MANIFEST.MF

This file was deleted.

50 changes: 43 additions & 7 deletions src/carskit/generic/Recommender.java
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ public abstract class Recommender implements Runnable{
protected String foldInfo;
// is output recommendation results
protected boolean isResultsOut = true;
// candidateItems filter
protected String ratedItemsFilter;
private static final Set<String> CANDIDATE_ITEMS_FILTERS = new HashSet<String>(Arrays.asList(new String[] {"none", "user", "user-context"}));

// number of users, items, ratings
protected int numUsers, numItems, numRates;
Expand Down Expand Up @@ -259,6 +262,12 @@ public Recommender(SparseMatrix trainMatrix, SparseMatrix testMatrix, int fold)
algoName = this.getClass().getSimpleName();
// get parameters of an algorithm
algoOptions = getModelParams(algoName);
// get the filter parameter
ratedItemsFilter = cf.getString("items.filter", "none");
if (!CANDIDATE_ITEMS_FILTERS.contains(ratedItemsFilter)) {
Logs.error("Filter {} defined in items.filter is invalid", ratedItemsFilter);
System.exit(-1);
}

// compute item-item correlations
if (isRankingPred && isDiverseUsed)
Expand Down Expand Up @@ -621,6 +630,14 @@ protected double perplexity(int u, int j, double r) throws Exception {
protected Map<Measure, Double> evalRankings() throws Exception {

HashMap<Integer, HashMultimap<Integer, Integer>> uciList=rateDao.getUserCtxList(testMatrix);

// Create a ratedItems set to enable filtering. Remains empty if no filter enabled (items.filter=none)
Set<Integer> ratedItems = new HashSet<Integer>();
HashMap<Integer, HashMultimap<Integer, Integer>> uciList_train = null;
if (!ratedItemsFilter.equals("none")) {
// If filtering is enabled, get all the ratings in the training set
uciList_train = rateDao.getUserCtxList(trainMatrix);
}
int capacity = uciList.keySet().size();

// initialization capacity to speed up
Expand Down Expand Up @@ -690,6 +707,16 @@ protected Map<Measure, Double> evalRankings() throws Exception {
List<Double> c_aucs = new ArrayList<>(c_capacity);
List<Double> c_ndcgs = new ArrayList<>(c_capacity);

HashMultimap<Integer, Integer> cList_train = null;
if (!ratedItemsFilter.equals("none")) {
// Get the ratings in the training set by the current user
cList_train = (uciList_train.containsKey(u)) ? uciList_train.get(u) : HashMultimap.<Integer, Integer>create();
if (ratedItemsFilter.equals("user")) {
// If the filter is on user, then this is the ratedItems set
ratedItems = Sets.newHashSet(cList_train.values());
}
}

// for each ctx
for (int c : cis.keySet()) {

Expand All @@ -712,19 +739,28 @@ protected Map<Measure, Double> evalRankings() throws Exception {
if (correctItems.size() == 0)
continue; // no testing data for user u

// remove rated items from candidate items
// in CARS, we do not exclude rateditems, since user may rate a same item for multiple times within different contexts
//Set<Integer> ratedItems = trainMatrix.getRatedItemsList(u, idUIs, userRatingList);
if (ratedItemsFilter.equals("user-context")) {
// If filter is on user-context, then get the ratings in this context if present in cList_train
if (cList_train.containsKey(c)) {
ratedItems = cList_train.get(c);
}
}

// Log ratedItems
/* String ratedItemsString = "";
for (Integer i : ratedItems) {
ratedItemsString += rateDao.getItemId(i).toString() + " ";
}
Logs.debug("ratedItems user {} (filter {}): {}", rateDao.getUserId(u).toString(), ratedItemsFilter, ratedItemsString); */

// predict the ranking scores (unordered) of all candidate items
List<Map.Entry<Integer, Double>> itemScores = new ArrayList<>(Lists.initSize(candItems));
for (final Integer j : candItems) {
// item j is not rated; but in CARS, we do not exclude rateditems, since user may rate a same item for multiple times within different contexts
//if (!ratedItems.contains(j)) {
final double rank = ranking(u, j, c);
if (ratedItemsFilter.equals("none") || !ratedItems.contains(j)) {
final double rank = ranking(u, j, c);
if (!Double.isNaN(rank)) {
itemScores.add(new SimpleImmutableEntry<Integer, Double>(j, rank));
//}
}
} else {
numCands--;
}
Expand Down