代码参考师姐的代码
新增代码:
/**
********************
* Leave-one-out prediction. The predicted values are stores in predictions.
*
* @see predictions
*********************
*/
public void leaveOneOutPrediction2() {
double tempUserAverageRating;
// Make each line of the code shorter.
int tempUser, tempItem, tempRating;
System.out.println("\r\nLeaveOneOutPrediction2 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 current item.
tempUserAverageRating = (userAverageRatings[tempUser] * userDegrees[tempUser] - tempRating)
/ (userDegrees[tempUser] - 1);
// Step 2. Recompute neighbors,at the same time obtain the ratings of neighbors.
int tempNeighbors = 0;
double tempTotal = 0;
int tempComparedItem;
for (int j = 0; j < numUsers; j++) {
if (j == tempUser)
continue;
for (int k = userStartingIndices[j]; k < userStartingIndices[j + 1]; k++) {
tempComparedItem = compressedRatingMatrix[k][1];
if (tempComparedItem == tempItem
&& Math.abs(tempUserAverageRating - userAverageRatings[j]) < radius) {
tempTotal += compressedRatingMatrix[k][2];
tempNeighbors++;
} // Of if
} // Of for k
} // 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 leaveOneOutPrediction2
/**
********************
* Transform the compressed matrix.
*********************
*/
public void transformMatrix() {
int[][] resultMatrix = new int[numRatings][3];
int[] tempItemCounts = new int[numItems];
int[] tempPointIndex = new int[numItems];
int[] tempItemStartingIndices = new int[numItems + 1];
// Count the number of every item.
for (int i = 0; i < numRatings; i++) {
tempItemCounts[compressedRatingMatrix[i][1]]++;
} // Of for i
// Get every item's starting index and initial the point array.
tempPointIndex[0] = 0;
tempItemStartingIndices[0] = 0;
tempItemStartingIndices[numItems] = numRatings;
for (int i = 1; i < numItems; i++) {
tempPointIndex[i] = tempItemCounts[i - 1] + tempPointIndex[i - 1];
tempItemStartingIndices[i] = tempPointIndex[i];
} // Of for i
// Transform the matrix.
int tempIndex;
for (int i = 0; i < numRatings; i++) {
tempIndex = tempPointIndex[compressedRatingMatrix[i][1]];
resultMatrix[tempIndex][0] = compressedRatingMatrix[i][1];
resultMatrix[tempIndex][1] = compressedRatingMatrix[i][0];
resultMatrix[tempIndex][2] = compressedRatingMatrix[i][2];
tempPointIndex[compressedRatingMatrix[i][1]]++;
} // Of for i
// Swap the value between users and items.
int tempArray[], tempValue;
double tempAvarageArray[];
compressedRatingMatrix = resultMatrix;
userStartingIndices = tempItemStartingIndices;
tempArray = userDegrees;
userDegrees = itemDegrees;
itemDegrees = tempArray;
tempValue = numUsers;
numUsers = numItems;
numItems = tempValue;
tempAvarageArray = userAverageRatings;
userAverageRatings = itemAverageRatings;
itemAverageRatings = tempAvarageArray;
// leaveOneOutPrediction();
}// Of transformMatrix
主函数:
/**
*************************
* The entrance of the program.
*
* @param args
* Not used now.
*************************
*/
public static void main(String[] args) {
try {
MBR tempRecommender = new MBR("C:\\Users\\86183\\IdeaProjects\\deepLearning\\src\\main\\java\\resources\\movielens-943u1682m.txt", 943, 1682, 100000);
for (double tempRadius = 0.2; tempRadius < 0.6; tempRadius += 0.1) {
tempRecommender.setRadius(tempRadius);
tempRecommender.leaveOneOutPrediction();
double tempMAE = tempRecommender.computeMAE();
double tempRSME = tempRecommender.computeRSME();
System.out.println("Radius = " + tempRadius + ", MAE = " + tempMAE + ", RSME = " + tempRSME
+ ", numNonNeighbors = " + tempRecommender.numNonNeighbors);
} // Of for tempRadius
System.out.println("\r\n-------user-based recommendation by transform -------");
tempRecommender.transformMatrix();
for (double tempRadius = 0.2; tempRadius < 0.6; tempRadius += 0.1) {
tempRecommender.setRadius(tempRadius);
tempRecommender.leaveOneOutPrediction();
double tempMAE = tempRecommender.computeMAE();
double tempRSME = tempRecommender.computeRSME();
System.out.println("Radius = " + tempRadius + ", MAE = " + tempMAE + ", RSME=" + tempRSME
+ ", numNonNeighbors = " + tempRecommender.numNonNeighbors);
} // Of for tempRadius
} catch (Exception ee) {
System.out.println(ee);
} // Of try
}// Of main
结果: