作者:计算机源码社
个人简介:本人七年开发经验,擅长Java、Python、PHP、.NET、微信小程序、爬虫、大数据等,大家有这一块的问题可以一起交流!
学习资料、程序开发、技术解答、文档报告
如需要源码,可以扫取文章下方二维码联系咨询
JavaWeb项目
微信小程序项目
Python项目
Android项目
基于协同过滤算法的绿色食品推荐系统 功能演示视频
如需要源码,可以扫取文章下方二维码联系咨询
绿色食品推荐系统的设计背景源于当今社会对健康饮食和可持续发展的日益关注。随着人们对食品质量和环境问题的关切不断增加,绿色食品——即符合环保标准、不含有害物质、有机栽培或采购的食品——已成为消费者的首选。因此,开发一种基于协同过滤算法的绿色食品推荐系统变得尤为重要。
绿色食品有助于保持人们的健康,因为它们通常不含农药和化学添加剂,提供更多的营养价值。此外,这些食品的生产和采购通常更环保,有助于减少生态系统的破坏。现代社会中,有关绿色食品的信息和选择非常丰富,这对消费者来说可能是一种挑战。绿色食品推荐系统可以帮助消费者在众多选项中做出明智的选择。不同人对绿色食品的需求和口味各不相同。通过个性化推荐,推荐系统可以根据用户的需求和偏好提供定制的建议,从而提高用户满意度。
协同过滤算法是一种强大的推荐系统技术,它基于用户行为和偏好的相似性,为用户提供个性化的建议。在绿色食品推荐系统中,协同过滤可以分为两种类型:
1.用户协同过滤:根据用户的历史购买记录、评价和喜好,向他们推荐与相似用户购买的绿色食品。
2.物品协同过滤:根据绿色食品的属性、品牌和特性,向用户推荐其他类似的绿色食品。
基于协同过滤算法的绿色食品推荐系统功能图如下:
(2)下图是用户实体和其具备的属性。
(3)下图是绿色食品实体和其具备的属性。
(4)下图是公告实体和其具备的属性。
(6)下图是绿色食品留言实体和其具备的属性。
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class GreenFoodRecommendationSystem {
// 用户评分矩阵(示例数据)
private static double[][] ratings = {
{5, 4, 0, 0, 0},
{0, 0, 4, 0, 5},
{0, 0, 0, 0, 4},
{4, 0, 5, 0, 0},
{0, 0, 4, 5, 0}
};
// 计算用户相似度矩阵
private static double[][] computeUserSimilarity(double[][] ratings) {
int numUsers = ratings.length;
double[][] userSimilarity = new double[numUsers][numUsers];
for (int i = 0; i < numUsers; i++) {
for (int j = 0; j < numUsers; j++) {
if (i != j) {
double dotProduct = 0;
double normA = 0;
double normB = 0;
for (int k = 0; k < ratings[i].length; k++) {
if (ratings[i][k] != 0 && ratings[j][k] != 0) {
dotProduct += ratings[i][k] * ratings[j][k];
normA += Math.pow(ratings[i][k], 2);
normB += Math.pow(ratings[j][k], 2);
}
}
if (normA != 0 && normB != 0) {
userSimilarity[i][j] = dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));
}
}
}
}
return userSimilarity;
}
// 基于用户相似度进行推荐
private static List<Integer> recommendItems(int userId, double[][] userSimilarity) {
int numUsers = userSimilarity.length;
int numItems = ratings[0].length;
List<Integer> recommendedItems = new ArrayList<>();
double[] userRatings = ratings[userId];
Map<Integer, Double> itemScores = new HashMap<>();
for (int item = 0; item < numItems; item++) {
if (userRatings[item] == 0) {
double weightedSum = 0;
double sumOfSimilarities = 0;
for (int otherUser = 0; otherUser < numUsers; otherUser++) {
if (otherUser != userId && ratings[otherUser][item] != 0) {
weightedSum += userSimilarity[userId][otherUser] * ratings[otherUser][item];
sumOfSimilarities += Math.abs(userSimilarity[userId][otherUser]);
}
}
if (sumOfSimilarities > 0) {
itemScores.put(item, weightedSum / sumOfSimilarities);
}
}
}
// 获取前N个推荐物品
itemScores.entrySet().stream()
.sorted((e1, e2) -> Double.compare(e2.getValue(), e1.getValue()))
.limit(3)
.forEach(entry -> recommendedItems.add(entry.getKey()));
return recommendedItems;
}
public static void main(String[] args) {
int userId = 0; // 示例用户ID
double[][] userSimilarity = computeUserSimilarity(ratings);
List<Integer> recommendedItems = recommendItems(userId, userSimilarity);
System.out.println("为用户 " + userId + " 推荐的物品索引为: " + recommendedItems);
}
}
import java.util.*;
public class CollaborativeFilteringRecommendationSystem {
private int[][] userRatings; // 用户评分矩阵
private int numUsers;
private int numItems;
public CollaborativeFilteringRecommendationSystem(int[][] userRatings) {
this.userRatings = userRatings;
this.numUsers = userRatings.length;
this.numItems = userRatings[0].length;
}
// 计算用户相似度矩阵
public double[][] computeUserSimilarity() {
double[][] similarityMatrix = new double[numUsers][numUsers];
for (int user1 = 0; user1 < numUsers; user1++) {
for (int user2 = 0; user2 < numUsers; user2++) {
if (user1 != user2) {
double dotProduct = 0;
double normUser1 = 0;
double normUser2 = 0;
for (int item = 0; item < numItems; item++) {
int rating1 = userRatings[user1][item];
int rating2 = userRatings[user2][item];
if (rating1 != 0 && rating2 != 0) {
dotProduct += rating1 * rating2;
normUser1 += rating1 * rating1;
normUser2 += rating2 * rating2;
}
}
if (normUser1 != 0 && normUser2 != 0) {
similarityMatrix[user1][user2] = dotProduct / (Math.sqrt(normUser1) * Math.sqrt(normUser2));
}
}
}
}
return similarityMatrix;
}
// 获取用户的推荐物品
public List<Integer> getRecommendations(int userId, double[][] userSimilarity, int numRecommendations) {
List<Integer> recommendations = new ArrayList<>();
int[] userRatings = this.userRatings[userId];
Map<Integer, Double> itemScores = new HashMap<>();
for (int item = 0; item < numItems; item++) {
if (userRatings[item] == 0) {
double weightedSum = 0;
double sumOfSimilarities = 0;
for (int otherUser = 0; otherUser < numUsers; otherUser++) {
if (otherUser != userId && this.userRatings[otherUser][item] != 0) {
weightedSum += userSimilarity[userId][otherUser] * this.userRatings[otherUser][item];
sumOfSimilarities += Math.abs(userSimilarity[userId][otherUser]);
}
}
if (sumOfSimilarities > 0) {
itemScores.put(item, weightedSum / sumOfSimilarities);
}
}
}
itemScores.entrySet().stream()
.sorted((e1, e2) -> Double.compare(e2.getValue(), e1.getValue()))
.limit(numRecommendations)
.forEach(entry -> recommendations.add(entry.getKey()));
return recommendations;
}
public static void main(String[] args) {
int[][] userRatings = {
{5, 4, 0, 0, 0},
{0, 0, 4, 0, 5},
{0, 0, 0, 0, 4},
{4, 0, 5, 0, 0},
{0, 0, 4, 5, 0}
};
CollaborativeFilteringRecommendationSystem recommendationSystem = new CollaborativeFilteringRecommendationSystem(userRatings);
double[][] userSimilarity = recommendationSystem.computeUserSimilarity();
int userId = 0;
int numRecommendations = 3;
List<Integer> recommendations = recommendationSystem.getRecommendations(userId, userSimilarity, numRecommendations);
System.out.println("为用户 " + userId + " 推荐的物品索引为: " + recommendations);
}
}