-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWeightedAveragePredictor.java
47 lines (39 loc) · 1.58 KB
/
WeightedAveragePredictor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package alg.ib.predictor;
import java.util.Map;
import alg.ib.neighbourhood.Neighbourhood;
import profile.Profile;
import similarity.SimilarityMap;
public class WeightedAveragePredictor implements Predictor {
/**
* constructor - creates a new ThresholdNeighbourhood object
* @param t - the threshold value for the neighbourhood
*/
public WeightedAveragePredictor()
{
}
/**
* @returns the target user's predicted rating for the target item or null if a prediction cannot be computed
* @param userId - the numeric ID of the target user
* @param itemId - the numerid ID of the target item
* @param userProfileMap - a map containing user profiles
* @param itemProfileMap - a map containing item profiles
* @param neighbourhood - a Neighbourhood object
* @param simMap - a SimilarityMap object containing item-item similarities
*/
@Override
public Double getPrediction(Integer userId, Integer itemId,
Map<Integer, Profile> userProfileMap, Map<Integer, Profile> itemProfileMap,
Neighbourhood neighbourhood, SimilarityMap simMap)
{
double numerator = 0, denominator = 0;
for(Integer id: userProfileMap.get(userId).getIds()) //Iterate over target userId's
{
if(neighbourhood.isNeighbour(itemId, id)) //Ensuring current item is in the neighbourhood.
{
numerator += simMap.getSimilarity(userId, id) * userProfileMap.get(userId).getValue(id);
denominator += Math.abs(simMap.getSimilarity(userId, id)) + .000000001; //Testing, ensuring denomoinator != 0
}
}
return (denominator == 0)? null : numerator/denominator; //Return prediction score
}
}