/**
*************************
* Leave-one-out prediction. The predicted values are stored in predictions.
*************************
*/
public void leaveOneOutPredictionUsers() {
double tempUserAverageRating;
// Make each line of the code shorter.
int tempUser, tempItem, tempRating;
System.out.println("\r\nLeaveOneOutPrediction for radius " + radius);
numNonNeighbors = 0;
for (int i = 0; i < numRatings; i++) {
tempUser = compressedRatingMatrix[i][0];
tempItem = compressedRatingMatrix[i][1];
tempRating = compressedRatingMatrix[i][2];
// Step 1. Recompute average rating of the users.
tempUserAverageRating = (userAverageRatings[tempUser] * userDegrees[tempUser] - tempRating)
/ (userDegrees[tempUser] - 1);
// Step 2. Recompute neighbors, at the same time obtain the ratings
//Neighbors.
int tempNeighbors = 0;
double tempTotal = 0;
int tempComparedUser;
for (int j = userStartingIndices[tempUser]; j < userStartingIndices[tempUser + 1]; j++) {
tempComparedUser = compressedRatingMatrix[j][0];
if (tempUser == tempComparedUser) {
continue;
//Ignore itself.
} //of if
if (Math.abs(tempUserAverageRating - userAverageRatings[tempComparedUser]) < radius) {
tempTotal += compressedRatingMatrix[j][2];
tempNeighbors++;
} //of if
} //of for j
// Step 3. Predict as the average value of neighbors.
if (tempNeighbors > 0) {
predictions[i] = tempTotal / tempNeighbors;
} else {
predictions[i] = DEFAULT_RATING;
numNonNeighbors++;
} //of if
} //of for i
}//of leaveOneOutPrediction
day55
最新推荐文章于 2024-06-28 15:05:58 发布