我用Java写了一个协调过滤算法案例

协调过滤算法(Collaborative Filtering)是一种基于用户行为数据的推荐算法。这里给出一个简单的Java实现案例,使用余弦相似度计算物品之间的相似度,并根据相似度为用户推荐物品。

import java.util.*;

public class CollaborativeFiltering {
    public static void main(String[] args) {
        // 用户评分数据
        Map<String, Map<String, Integer>> userRatings = new HashMap<>();
        userRatings.put("A", new HashMap<String, Integer>() {{
            put("item1", 5);
            put("item2", 3);
            put("item3", 4);
        }});
        userRatings.put("B", new HashMap<String, Integer>() {{
            put("item1", 4);
            put("item2", 0);
            put("item3", 4);
        }});
        userRatings.put("C", new HashMap<String, Integer>() {{
            put("item1", 2);
            put("item2", 5);
            put("item3", 1);
        }});

        // 计算物品之间的相似度
        Map<String, Map<String, Double>> itemSimilarities = calculateItemSimilarities(userRatings);

        // 为用户A推荐物品
        List<String> recommendations = recommendItemsForUser(userRatings, itemSimilarities, "A");
        System.out.println("为用户A推荐的物品: " + recommendations);
    }

    public static Map<String, Map<String, Double>> calculateItemSimilarities(Map<String, Map<String, Integer>> userRatings) {
        Map<String, Map<String, Double>> itemSimilarities = new HashMap<>();

        for (String item1 : userRatings.values().iterator().next().keySet()) {
            itemSimilarities.put(item1, new HashMap<String, Double>());
            for (String item2 : userRatings.values().iterator().next().keySet()) {
                if (!item1.equals(item2)) {
                    double similarity = cosineSimilarity(userRatings, item1, item2);
                    itemSimilarities.get(item1).put(item2, similarity);
                }
            }
        }

        return itemSimilarities;
    }

    public static double cosineSimilarity(Map<String, Map<String, Integer>> userRatings, String item1, String item2) {
        double dotProduct = 0.0;
        double norm1 = 0.0;
        double norm2 = 0.0;

        for (String user : userRatings.keySet()) {
            int rating1 = userRatings.get(user).getOrDefault(item1, 0);
            int rating2 = userRatings.get(user).getOrDefault(item2, 0);
            dotProduct += rating1 * rating2;
            norm1 += rating1 * rating1;
            norm2 += rating2 * rating2;
        }

        return dotProduct / (Math.sqrt(norm1) * Math.sqrt(norm2));
    }

    public static List<String> recommendItemsForUser(Map<String, Map<String, Integer>> userRatings, Map<String, Map<String, Double>> itemSimilarities, String user) {
        List<String> recommendations = new ArrayList<>();
        Map<String, Integer> userRating = userRatings.get(user);

        for (String item : userRatings.values().iterator().next().keySet()) {
            if (!userRating.containsKey(item)) {
                double totalSimilarity = 0.0;
                double weightedSum = 0.0;

                for (String ratedItem : userRating.keySet()) {
                    double similarity = itemSimilarities.get(item).getOrDefault(ratedItem, 0.0);
                    totalSimilarity += Math.abs(similarity);
                    weightedSum += similarity * userRating.get(ratedItem);
                }

                if (totalSimilarity > 0) {
                    recommendations.add(item);
                }
            }
        }

        recommendations.sort((item1, item2) -> Double.compare(itemSimilarities.get(item1).getOrDefault(user, 0.0), itemSimilarities.get(item2).getOrDefault(user, 0.0)));
        Collections.reverse(recommendations);

        return recommendations;
    }
}

这个案例中,我们首先定义了一个用户评分数据userRatings,然后计算物品之间的相似度itemSimilarities,最后根据相似度为用户A推荐物品。

你可能感兴趣的:(java服务端,java,算法,开发语言)