用java语言写一个协同过滤算法
时间: 2023-05-16 14:05:26 浏览: 142
好的,以下是一个简单的基于Java的协同过滤算法实现:
```
public class CollaborativeFiltering {
private int[][] ratings;
private int numUsers;
private int numItems;
public CollaborativeFiltering(int[][] ratings) {
this.ratings = ratings;
this.numUsers = ratings.length;
this.numItems = ratings[0].length;
}
public double predictRating(int userId, int itemId) {
double sum = 0;
int count = 0;
for (int i = 0; i < numUsers; i++) {
if (i != userId && ratings[i][itemId] > 0) {
sum += ratings[i][itemId];
count++;
}
}
return count == 0 ? 0 : sum / count;
}
}
```
这个算法基于用户对物品的评分来预测用户对未评分物品的评分。它计算了与目标用户具有相似评分历史的其他用户的平均评分,并将其作为预测评分返回。
阅读全文