简介
协同过滤推荐算法是诞生最早,并且较为著名的推荐算法。主要的功能是预测和推荐。算法通过对用户历史行为数据的挖掘发现用户的偏好,基于不同的偏好对用户进行群组划分并推荐品味相似的商品。协同过滤推荐算法分为两类,分别是基于用户的协同过滤算法(user-based collaboratIve filtering),和基于物品的协同过滤算法(item-based collaborative filtering)。简单的说就是:人以类聚,物以群分。
1、基于用户的协同过滤 user-based collaboratIve filtering
基于用户的协同过滤算法是通过用户的历史行为数据发现用户对商品或内容的喜欢(如商品购买,收藏,内容评论或分享),并对这些喜好进行度量和打分。根据不同用户对相同商品或内容的态度和偏好程度计算用户之间的关系。在有相同喜好的用户间进行商品推荐。
2、基于物品的协同过滤算法与基于用户的协同过滤算法很像,将商品和用户互换。通过计算不同用户对不同物品的评分获得物品间的关系。基于物品间的关系对用户进行相似物品的推荐。这里的评分代表用户对商品的态度和偏好。简单来说就是如果用户A同时购买了商品1和商品2,那么说明商品1和商品2的相关度较高。当用户B也购买了商品1时,可以推断他也有购买商品2的需求。
开发环境
开发语言:Java 框架:springboot JDK版本:JDK1.8 服务器:tomcat7
数据库:mysql5.7(一定要5.7版本) 数据库工具:Navicat11 开发软件:eclipse/myeclipse/idea
Maven包:Maven3.3.9 浏览器:谷歌浏览器后台路径地址:localhost:8080/项目名称/admin/dist/index.html
前台路径地址:localhost:8080/项目名称/front/dist/index.html (无前台不需要输入)管理员账号:admin 管理员密码:admin
开发技术简介
Java简介
Java是一种面向对象的静态式编程语言。Java编程语言具有多线程和对象定向的特点。其特点是根据方案的属性将方案分为几个不同的模块,这些模块是封闭的和多样化的,在申请过程中具有很强的独立性。Java语言在计算机软件开发过程中的运用可以达到交互操作的目的,通过各种形式的交换,可以有效地处理所需的数据,从而确保计算机软件开发的可控性和可见性。开发java语言时,保留了网络接口,Java保留的缺省网络接口可以与web应用程序编程所依赖的类别库相匹配。为了使Java开发的应用程序更加稳定和强健,Java会自动收集程序中的垃圾,并处理程序中存在的异常。Java语言是日常开发过程中广泛使用的通用基本语言。其中Java语言课程库、句子、语法规则和关键字经常用于计算机软件的开发和编程。
面向对象编程是Java语言最显着的特点。它具有原始接口和补充接口以及继承,不仅可以实现相同类型的单个继承,而且还支持接口之间的多个继承,从而实现类、接口和接口之间以及类和接口之间的有效通信。Java的面向对象特性主要包括三个方面:继承、多态性和封装。封装是Java的核心,可以封装所有数据操作。多态性是指由面向对象行为派生的相关行为。继承作为特殊编程模式有两种类型:父类和子类,这两种类型的属性具有相同的功能和特性。对于父类的属性特性,子类可以实现继承和优化。
Spring Boot框架介绍
SpringBoot是近几年最为流行的后台开发框架,它的诞生一改过去Spring框架开发中繁琐的配置,极大地简化了Spring应用的搭建和开发。SpringBoot框架不仅保有了Spring框架中的所有优秀特性,还通过使用特定的配置方式,在底层帮助开发人员在工程创建是就预先做了很多配置,这样在开发时就不再需要开发人员过多进行繁琐的配置了。另外在SpringBoot中集成了大量框架,这就使得开发人员不再需要到处寻找在导入开发中需要依赖的jar包,同时也解决了依赖包版本冲突问题,从而提高了依赖包引用的稳定性,从而实现了对Spring应用搭建和开发过程的简化。
MySQL数据库
MySQL是一种关系型的数据库管理系统,属于Oracle旗下的产品。MySQL的语言是非结构化的,使用的用户可以在数据上进行工作。这个数据库管理系统一经问世就受到了社会的广泛关注。在各个方面,与同等的数据库相比,MySQL的优点极为突出,它的运行速度快,适用的范围广泛,而且数据库的安全性这一方面独树一帜。在语言结构方面,MySQL的语言简单,其他数据库需要一大段代码来实现的操作,MySQL仅需要一小部分代码甚至几行。综上所述,MySQL这种关系型数据库管理系统,已经成为了开发者进行项目的数据开发、存储的不二之选。MySQL的功能也多种多样,如数据操纵和数据库的建立维护等。而且该数据库的数据共享性高、冗余度低而且容易扩充。MySQL在安全性这一方面也具有自身的特点,它应用了用户的标识和鉴别技术,对试图和数据进行加密,确保资料信息的可靠性。介于数据库系统的功能与强大等性质之间,本数据库系统的设计中主要使用了MySQL实现对数据的处理。基于Web的付费自习室管理系统运用MySQL数据库,在Web应用这一块,MySQL是最好的选择。对于该系统整个的开发、搭建、运行和维护具有极其重要的作用。
详细视频演示
请联系我获取更详细的演示视频
系统功能部分效果
package com.module.controller;
import com.module.mapper.ArticleMapper;
import com.module.mapper.CommonMapper;
import com.module.mapper.ScoreinfoMapper;
import com.module.pojo.Article;
import com.module.pojo.Scoreinfo;
import com.module.pojo.Userinfo;
import com.module.util.MyFilleUtil;
import com.module.util.ResultUtil;
import org.apache.mahout.cf.taste.impl.common.LongPrimitiveIterator;
import org.apache.mahout.cf.taste.impl.model.file.FileDataModel;
import org.apache.mahout.cf.taste.impl.neighborhood.NearestNUserNeighborhood;
import org.apache.mahout.cf.taste.impl.recommender.GenericItemBasedRecommender;
import org.apache.mahout.cf.taste.impl.recommender.GenericUserBasedRecommender;
import org.apache.mahout.cf.taste.impl.similarity.EuclideanDistanceSimilarity;
import org.apache.mahout.cf.taste.model.DataModel;
import org.apache.mahout.cf.taste.recommender.RecommendedItem;
import org.apache.mahout.cf.taste.recommender.Recommender;
import org.apache.mahout.cf.taste.similarity.ItemSimilarity;
import org.apache.mahout.cf.taste.similarity.UserSimilarity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Controller
public class AlgorithmController {
public static int ALGORITHM_TYPE = 1;//临近的用户个数
final static int NEIGHBORHOOD_NUM = 2;//临近的用户个数
final static int RECOMMENDER_NUM = 9;//推荐物品的最大个数
@Autowired //注入对象
public ScoreinfoMapper scoreinfoMapper;
@Autowired
ArticleMapper articleMapper;
@Autowired //注入对象
public CommonMapper commonMapper;
//公共查询方法
@ResponseBody
@RequestMapping("/pc/selectAction")
public ResultUtil selectAction(String sql) {
List<Map> mapList = commonMapper.selectAction(sql);
return ResultUtil.ok(mapList);
}
//公共修改方法
@ResponseBody
@RequestMapping("/pc/updateAction")
public ResultUtil updateAction(String sql) {
commonMapper.updateAction(sql);
return ResultUtil.ok("操作成功");
}
@RequestMapping("/")
public String echartsB22() {
return "redirect:pc/index";
}
@RequestMapping("/pc/algorithmList")
public String algorithmList(HttpSession session, HttpServletRequest request, Model model) throws Exception {
String filePath = request.getSession().getServletContext().getRealPath("/static") + File.separatorChar + "data.csv";
System.out.println("filePath = " + filePath);
Userinfo user = (Userinfo) session.getAttribute("loginUserinfo");
int myid = user.getId();
List<Scoreinfo> scoreinfoList = scoreinfoMapper.selectByMap(null);
String content = "";
for (Scoreinfo scoreinfo : scoreinfoList) {
content = content + scoreinfo.getUid() + "," + scoreinfo.getTid() + "," + scoreinfo.getScore() + "\n";
}
MyFilleUtil.WriteFile(filePath, content);
//List<Integer> arr = userCF(filePath,myid);// 基于用户的
List<Integer> arr = new ArrayList<Integer>();
if (ALGORITHM_TYPE == 1) {
arr = itemCF(filePath, myid);// 基于物品的
} else {
arr = userCF(filePath, myid);// 基于用户的
}
List<Article> datainfoList = new ArrayList<Article>();
for (Integer tid : arr) {
Article datainfo = articleMapper.selectArticleById(tid);
if (null != datainfo) {
datainfoList.add(datainfo);
}
}
model.addAttribute("datainfoList", datainfoList);
return "pc/algorithm";
}
@ResponseBody
@RequestMapping("/pc/algorithmSave")
public ResultUtil algorithmSave(HttpSession session, Scoreinfo scoreinfo, Model model) throws Exception {
scoreinfoMapper.insert(scoreinfo);
return ResultUtil.ok("操作成功");
}
//基于用户的 CF(User CF) 推荐算法
public static List<Integer> userCF(String filePath, int myid) throws Exception {
List<Integer> arrList = new ArrayList<Integer>();
DataModel model = new FileDataModel(new File(filePath));//数据模型
UserSimilarity user = new EuclideanDistanceSimilarity(model);//用户相识度算法
NearestNUserNeighborhood neighbor = new NearestNUserNeighborhood(NEIGHBORHOOD_NUM, user, model);
//用户近邻算法
Recommender r = new GenericUserBasedRecommender(model, neighbor, user);//用户推荐算法
LongPrimitiveIterator iter = model.getUserIDs();///得到用户ID
while (iter.hasNext()) {
long uid = iter.nextLong();
List<RecommendedItem> list = r.recommend(uid, RECOMMENDER_NUM);
System.out.printf("uid:%s", uid);
for (RecommendedItem ritem : list) {
System.out.printf("(%s,%f)", ritem.getItemID(), ritem.getValue());
if (myid == uid) {
arrList.add((int) ritem.getItemID());
}
}
System.out.println();
}
return arrList;
}
//基于物品的 CF 的基本原理 推荐算法
public static List<Integer> itemCF(String filePath, int myid) throws Exception {
List<Integer> arrList = new ArrayList<Integer>();
DataModel model = new FileDataModel(new File(filePath));//数据模型
ItemSimilarity item = new EuclideanDistanceSimilarity(model);//用户相识度算法
Recommender r = new GenericItemBasedRecommender(model, item);//物品推荐算法
LongPrimitiveIterator iter = model.getUserIDs();
while (iter.hasNext()) {
long uid = iter.nextLong();
List<RecommendedItem> list = r.recommend(uid, RECOMMENDER_NUM);
System.out.printf("uid:%s", uid);
for (RecommendedItem ritem : list) {
System.out.printf("(%s,%f)", ritem.getItemID(), ritem.getValue());
if (myid == uid) {
arrList.add((int) ritem.getItemID());
}
}
System.out.println();
}
return arrList;
}
}