写一段java代码实现推荐算法的核心代码
时间: 2023-07-10 11:18:18 浏览: 90
推荐算法有很多种,这里给出一个简单的基于协同过滤的推荐算法的Java代码示例:
```java
import java.util.*;
public class RecommenderSystem {
private Map<String, Map<String, Double>> user_item_rating; // 用户对物品的评分
private Map<String, Map<String, Double>> item_user_rating; // 物品被哪些用户评分过
private Map<String, Double> item_avg_rating; // 物品的平均评分
public RecommenderSystem(Map<String, Map<String, Double>> user_item_rating) {
this.user_item_rating = user_item_rating;
this.item_user_rating = new HashMap<>();
this.item_avg_rating = new HashMap<>();
for (String user : user_item_rating.keySet()) {
Map<String, Double> item_rating = user_item_rating.get(user);
for (String item : item_rating.keySet()) {
if (!item_user_rating.containsKey(item)) {
item_user_rating.put(item, new HashMap<>());
}
item_user_rating.get(item).put(user, item_rating.get(item));
item_avg_rating.put(item, item_avg_rating.getOrDefault(item, 0.0) + item_rating.get(item));
}
}
for (String item : item_avg_rating.keySet()) {
item_avg_rating.put(item, item_avg_rating.get(item) / item_user_rating.get(item).size());
}
}
// 基于用户相似度的推荐
public List<String> recommendByUser(String user, int k, int n) {
Map<String, Double> scores = new HashMap<>();
Map<String, Double> user_rating = user_item_rating.get(user);
for (String item : user_rating.keySet()) {
for (String other_user : item_user_rating.get(item).keySet()) {
if (!other_user.equals(user)) {
double similarity = cosineSimilarity(user_item_rating.get(other_user), user_rating);
double score = item_user_rating.get(item).get(other_user) * similarity;
scores.put(item, scores.getOrDefault(item, 0.0) + score);
}
}
}
List<String> recommendations = new ArrayList<>(scores.keySet());
recommendations.sort((a, b) -> scores.get(b).compareTo(scores.get(a)));
if (k > 0) {
recommendations = recommendations.subList(0, Math.min(k, recommendations.size()));
}
if (n > 0) {
List<String> popular_items = new ArrayList<>(item_avg_rating.keySet());
popular_items.sort((a, b) -> item_avg_rating.get(b).compareTo(item_avg_rating.get(a)));
recommendations.addAll(popular_items.subList(0, Math.min(n, popular_items.size())));
}
return recommendations;
}
// 基于物品相似度的推荐
public List<String> recommendByItem(String user, int k, int n) {
Map<String, Double> scores = new HashMap<>();
Map<String, Double> user_rating = user_item_rating.get(user);
for (String item : user_rating.keySet()) {
double weight_sum = 0.0;
for (String other_item : item_user_rating.keySet()) {
if (!item.equals(other_item)) {
double similarity = cosineSimilarity(item_user_rating.get(item), item_user_rating.get(other_item));
double weight = item_user_rating.get(other_item).containsKey(user) ? item_user_rating.get(other_item).get(user) : item_avg_rating.get(other_item);
double score = weight * similarity;
scores.put(other_item, scores.getOrDefault(other_item, 0.0) + score);
weight_sum += similarity;
}
}
scores.put(item, scores.get(item) / weight_sum);
}
List<String> recommendations = new ArrayList<>(scores.keySet());
recommendations.sort((a, b) -> scores.get(b).compareTo(scores.get(a)));
if (k > 0) {
recommendations = recommendations.subList(0, Math.min(k, recommendations.size()));
}
if (n > 0) {
List<String> popular_items = new ArrayList<>(item_avg_rating.keySet());
popular_items.sort((a, b) -> item_avg_rating.get(b).compareTo(item_avg_rating.get(a)));
recommendations.addAll(popular_items.subList(0, Math.min(n, popular_items.size())));
}
return recommendations;
}
// 计算余弦相似度
private double cosineSimilarity(Map<String, Double> a, Map<String, Double> b) {
double dot_product = 0.0, norm_a = 0.0, norm_b = 0.0;
for (String key : a.keySet()) {
if (b.containsKey(key)) {
dot_product += a.get(key) * b.get(key);
}
norm_a += a.get(key) * a.get(key);
}
for (String key : b.keySet()) {
norm_b += b.get(key) * b.get(key);
}
return dot_product / Math.sqrt(norm_a * norm_b);
}
}
```
这个代码实现了一个简单的基于协同过滤的推荐算法。其中,`user_item_rating` 是一个 `Map`,存储每个用户对每个物品的评分;`item_user_rating` 是一个 `Map`,存储每个物品被哪些用户评分过;`item_avg_rating` 是一个 `Map`,存储每个物品的平均评分。`recommendByUser` 方法基于用户相似度进行推荐,`recommendByItem` 方法基于物品相似度进行推荐,两个方法的参数含义相同。`cosineSimilarity` 方法计算两个向量的余弦相似度。
阅读全文