先回忆一下基于 M-distance 的推荐的过程:
以左图为例,图中的u0,u1,u2,u3,u4代表用户,m0,m1,m2,m3,m4,m5代表五种不同的电影,图中的数字代表着用户对相应电影的评分,0则代表该用户没有看过此电影,num代表对应的电影的被评分次数,sum代表被评分的总分,最后一行代表该电影评分的平均值。
如果要求(u0,m2)的值,也就是u0用户对m2的评分,首先观察各个电影评分的平均值,m2电影评分的平均值为3.5,那么在电影平均分为[3.2 , 3.8]的范围内,有电影m0,m1,m3,m5,但在其中u0用户并没有看过m0和m5,所以剩下m1,m3,u0用户对m1的评分为2,对m3的评分为4,则预测的平均分为(2 + 4) / 2 = 3.
评分表 (用户, 项目, 评分) 的压缩方式给出,数据为“0,2,4” 表示用户 0 对项目 2 的评分为 4.
while ((tempString = tempBufReader.readLine()) != null) {
tempStrArray = tempString.split(",");
compressedRatingMatrix[tempIndex][0] = Integer.parseInt(tempStrArray[0]);
compressedRatingMatrix[tempIndex][1] = Integer.parseInt(tempStrArray[1]);
compressedRatingMatrix[tempIndex][2] = Integer.parseInt(tempStrArray[2]);
userDegrees[compressedRatingMatrix[tempIndex][0]]++;
itemDegrees[compressedRatingMatrix[tempIndex][1]]++;
if (tempIndex > 0) {
if (compressedRatingMatrix[tempIndex][0] != compressedRatingMatrix[tempIndex - 1][0]) {
userStartingIndices[compressedRatingMatrix[tempIndex][0]] = tempIndex;
} // Of if
} // Of if
tempIndex++;
} // Of while
这段代码思考了很久,由于我们数据是以
0,2,4
0,3,5
1,1,3
...
这种形式存放的,这段代码做的工作是先获得数据,并在压缩矩阵中存储
举个例子:比如压缩矩阵中的compressedRatingMatrix[0][0] = 0,(用户0)compressedRatingMatrix[0][1] = 2,(电影2)compressedRatingMatrix[0][2] = 4,(代表用户0对电影2的评分情况)
再使用 userDegrees[compressedRatingMatrix[tempIndex][0]]++表示:userDegrees[compressedRatingMatrix[0][0]]++,即用户0对电影的评分个数加一;
ItemDegrees[compressedRatingMatrix[tempIndex][1]]++表示:ItemDegrees[compressedRatingMatrix[0][1]]++表示电影2被评分的次数加一。以此计算每个用户的评分次数,和每个电影的被评分次数。
由于我们的数据是按照用户排序的(先是用户0的所有数据,再是用户1...)if语句是用来判断是否换了一个新的用户读取,即上一个用户的数据已经完全读完,用userStartingIndices[]记录每个用户的数据是在哪行开始的。如:userStartingIndices[0] = 0,userStartingIndices[1] = 2.
完整代码:
package knn5;
import java.io.*;
public class MBR {
public static final double DEFAULT_RATING = 3.0;
private int numUsers;
private int numItems;
private int numRatings;
private double[] predictions;
private int[][] compressedRatingMatrix;
private int[] userDegrees;
private double[] userAverageRatings;
private int[] itemDegrees;
private double[] itemAverageRatings;
private int[] userStartingIndices;
private int numNonNeighbors;
private double radius;
public MBR(String paraFilename, int paraNumUsers, int paraNumItems, int paraNumRatings) throws Exception {
numItems = paraNumItems;
numUsers = paraNumUsers;
numRatings = paraNumRatings;
userDegrees = new int[numUsers];
userStartingIndices = new int[numUsers + 1];
userAverageRatings = new double[numUsers];
itemDegrees = new int[numItems];
compressedRatingMatrix = new int[numRatings][3];
itemAverageRatings = new double[numItems];
predictions = new double[numRatings];
System.out.println("Reading " + paraFilename);
File tempFile = new File(paraFilename);
if (!tempFile.exists()) {
System.out.println("File " + paraFilename + " does not exists.");
System.exit(0);
} // Of if
BufferedReader tempBufReader = new BufferedReader(new FileReader(tempFile));
String tempString;
String[] tempStrArray;
int tempIndex = 0;
userStartingIndices[0] = 0;
userStartingIndices[numUsers] = numRatings;
while ((tempString = tempBufReader.readLine()) != null) {
tempStrArray = tempString.split(",");
compressedRatingMatrix[tempIndex][0] = Integer.parseInt(tempStrArray[0]);
compressedRatingMatrix[tempIndex][1] = Integer.parseInt(tempStrArray[1]);
compressedRatingMatrix[tempIndex][2] = Integer.parseInt(tempStrArray[2]);
userDegrees[compressedRatingMatrix[tempIndex][0]]++;
itemDegrees[compressedRatingMatrix[tempIndex][1]]++;
if (tempIndex > 0) {
if (compressedRatingMatrix[tempIndex][0] != compressedRatingMatrix[tempIndex - 1][0]) {
userStartingIndices[compressedRatingMatrix[tempIndex][0]] = tempIndex;
} // Of if
} // Of if
tempIndex++;
} // Of while
tempBufReader.close();
double[] tempUserTotalScore = new double[numUsers];
double[] tempItemTotalScore = new double[numItems];
for (int i = 0; i < numRatings; i++) {
tempUserTotalScore[compressedRatingMatrix[i][0]] += compressedRatingMatrix[i][2];
tempItemTotalScore[compressedRatingMatrix[i][1]] += compressedRatingMatrix[i][2];
} // Of for i
for (int i = 0; i < numUsers; i++) {
userAverageRatings[i] = tempUserTotalScore[i] / userDegrees[i];
} // Of for i
for (int i = 0; i < numItems; i++) {
itemAverageRatings[i] = tempItemTotalScore[i] / itemDegrees[i];
} // Of for i
}// Of the first constructor
public void setRadius(double paraRadius) {
if (paraRadius > 0) {
radius = paraRadius;
} else {
radius = 0.1;
} // Of if
}// Of setRadius
public void leaveOneOutPrediction() {
double tempItemAverageRating;
int tempUser, tempItem, tempRating;
System.out.println("rnLeaveOneOutPrediction for radius " + radius);
numNonNeighbors = 0;
for (int i = 0; i < numRatings; i++) {
tempUser = compressedRatingMatrix[i][0];
tempItem = compressedRatingMatrix[i][1];
tempRating = compressedRatingMatrix[i][2];
tempItemAverageRating = (itemAverageRatings[tempItem] * itemDegrees[tempItem] - tempRating)
/ (itemDegrees[tempItem] - 1);
int tempNeighbors = 0;
double tempTotal = 0;
int tempComparedItem;
for (int j = userStartingIndices[tempUser]; j < userStartingIndices[tempUser + 1]; j++) {
tempComparedItem = compressedRatingMatrix[j][1];
if (tempItem == tempComparedItem) {
continue;
} // Of if
if (Math.abs(tempItemAverageRating - itemAverageRatings[tempComparedItem]) < radius) {
tempTotal += compressedRatingMatrix[j][2];
tempNeighbors++;
} // Of if
} // Of for j
if (tempNeighbors > 0) {
predictions[i] = tempTotal / tempNeighbors;
} else {
predictions[i] = DEFAULT_RATING;
numNonNeighbors++;
} // Of if
} // Of for i
}// Of leaveOneOutPrediction
public double computeMAE() throws Exception {
double tempTotalError = 0;
for (int i = 0; i < predictions.length; i++) {
tempTotalError += Math.abs(predictions[i] - compressedRatingMatrix[i][2]);
} // Of for i
return tempTotalError / predictions.length;
}// Of computeMAE
public double computeRSME() throws Exception {
double tempTotalError = 0;
for (int i = 0; i < predictions.length; i++) {
tempTotalError += (predictions[i] - compressedRatingMatrix[i][2])
* (predictions[i] - compressedRatingMatrix[i][2]);
} // Of for i
double tempAverage = tempTotalError / predictions.length;
return Math.sqrt(tempAverage);
}// Of computeRSME
public static void main(String[] args) {
try {
MBR tempRecommender = new MBR("C:\Users\ASUS\Desktop\11.txt",5, 6, 30);
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
}// Of class MBR
运行截图:



