协同过滤算法实现步骤

  • 1.表示用户行为矩阵,即统计用户购买某种商品类型的数量
 public double[] getNumByCustomer(Customer customer){List<OrderItem> list =orderItemDao.findByCustomerAndAliveAndState(customer.getId(),1,2);double [] vectore =new double[totalNum];int index=0;for(ProductType type:productTypes){for(OrderItem orderItem:list){if(orderItem.getProduct().getProductType().id==type.id){vectore[index]=vectore[index]+orderItem.getNum();}}return vectore;}
  • 2.用余弦距离计算每个用户与其它用户的行为相似度
    下面代码是两个用户之间的相似度,进行遍历就可以获取全部相似度
 public double countSimilarity(double [] a,double [] b){double total=0;double alength=0;double blength=0;for(int i=0;i<a.length;i++){total=total+a[i]*b[i];alength=alength+a[i]*a[i];blength=blength+b[i]*b[i];}double down=Math.sqrt(alength)*Math.sqrt(blength);double result=0;if(down!=0){result =total/down;}return result;}
  • 3.取相似度最高的前n个用户,组成相似用户集合
    对Map按值进行排序
 public List<Map.Entry<Long,Double>> getMaxSimilarity(Customer customer){Map<Long,Double> result =new HashMap<Long,Double>();double vector[] =(double [])users.get(customer.getId());for(Map.Entry<Long,Object> entry:users.entrySet()){if(entry.getKey()!=customer.getId()){double [] temp =(double[])entry.getValue();double similarity =countSimilarity(temp,vector);              result.put(entry.getKey(),similarity);}}List<Map.Entry<Long,Double>> list = new LinkedList<Map.Entry<Long,Double>>( result.entrySet() );Collections.sort( list, new Comparator<Map.Entry<Long,Double>>(){public int compare( Map.Entry<Long,Double> o1, Map.Entry<Long,Double> o2 ){return (o2.getValue()).compareTo( o1.getValue() );}} );return list;}
  • 4.获得相似用户集合购买的商品,并统计相似用户购买的商品的数量,进行排序
  public Map<Long,ProductNumModel> getProducts(List<Map.Entry<Long,Double>> list){List<Customer> simCustomers =new ArrayList<Customer>();System.out.println("相似度高的3个用户  ");for(int i=0;i<list.size()&&i<3;i++){Long id =list.get(i).getKey();Customer customer =customerDao.findByIdAndAlive(id,1);simCustomers.add(customer);}Map<Long,ProductNumModel> map =new HashMap<Long,ProductNumModel>();for(Customer customer:simCustomers){Map<Long,ProductNumModel> hashSet =getCustomerProduct(customer);for(Map.Entry<Long,ProductNumModel> entry:hashSet.entrySet()){ProductNumModel model=null;if(map.containsKey(entry.getKey())){model=map.get(entry.getKey());model.num+=entry.getValue().num;}else{model=new ProductNumModel();model.product=entry.getValue().product;model.num=entry.getValue().num;}map.put(entry.getKey(),model);}}return map;}
  • 总的调用函数,将前面函数连接,并把结果存到文件中。如果文件不存在,则用算法计算,如果文件内容存在,则直接读取文件的内容。开定时任务,每天或者一周将商品推荐文件删除,则会自动更新商品推荐内容
   public Map<String,Object> getAllSimilarity(Customer customer) throws IOException {changeCustomerToVector();for(Map.Entry<Long,Object> entry:users.entrySet()){double [] temp=(double [])entry.getValue();}InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("cxtx.properties");Properties p = new Properties();try {p.load(inputStream);} catch (IOException e1) {e1.printStackTrace();}String folderPath = p.getProperty("recommendFile");File file=new File(folderPath);if(!file.exists()){file.createNewFile();}FileInputStream fileInputStream=new FileInputStream(file);Map<String,Object> map =new HashMap<String,Object>();com.alibaba.fastjson.JSONObject jsonObject = null;try {if(fileInputStream!=null){jsonObject = com.alibaba.fastjson.JSON.parseObject(IOUtils.toString(fileInputStream, "UTF-8"));}} catch (IOException e) {map.put("msg","JSON 格式不正确");map.put("content","");return map;}Object content=null;if(jsonObject==null){ //如果文件中没有,则计算每个用户的推荐产品FileWriter fileWriter=new FileWriter(file,true);BufferedWriter bufferedWriter=new BufferedWriter(fileWriter);Map<Long,Object> temp =new HashMap<Long,Object>();for(Customer c:customers){List<Map.Entry<Long,Double>> list =this.getMaxSimilarity(c);Map<Long,ProductNumModel> result =getProducts(list);List<Product> list1=sortProduct(result);temp.put(c.getId(),list1);}JSONObject object=new JSONObject(temp);bufferedWriter.write(object.toString());bufferedWriter.flush();if(object!=null){content= object.get(customer.getId()+"");}}else{if(null!=jsonObject.get(customer.getId()+"")){content=jsonObject.get(customer.getId()+"");}}map.put("msg","获取成功");map.put("content",content);return map;}
  • 注意的地方:

1.用户相似度计算时,要考虑分母为0的情况;同时要防止数值太大,超过了double能表示的范围,可以做一些处理,例如除以最大的某个商品销售量,来表示某个维度的向量值,或者减去某个值等等

2.余弦值越接近1,表明两个向量越相似,即计算出来的值越大,用户行为越相似

3.最后获得推荐的商品数量可以较多或较少,要根据一定策略进行排序,例如相似用户的购买数量,而不是商品总的销售量,因为不相似用户的数据,容易产生干扰。

JAVA_协同过滤算法商品推荐相关推荐

  1. 基于协同过滤算法的推荐

    2019独角兽企业重金招聘Python工程师标准>>> 基于协同过滤算法的推荐 (本实验选用数据为真实电商脱敏数据,仅用于学习,请勿商用) 数据挖掘的一个经典案例就是尿布与啤酒的例子 ...

  2. SpringBoot+Vue+Element-UI实现协同过滤算法商品推荐系统

    文末获取源码 开发语言:Java 框架:springboot JDK版本:JDK1.8 服务器:tomcat7 数据库:mysql 5.7/8.0 数据库工具:Navicat11 开发软件:eclip ...

  3. 协同过滤算法智能推荐原理与实践

    数据挖掘的一个经典案例就是尿布与啤酒的例子.尿布与啤酒看似毫不相关的两种产品,但是当超市将两种产品放到相邻货架销售的时候,会大大提高两者销量.很多时候看似不相关的两种产品,却会存在这某种神秘的隐含关系 ...

  4. 02 机器学习算法库Mahout - 协同过滤算法实现推荐功能

     (原文地址:http://blog.csdn.net/codemosi/article/category/2777041,转载麻烦带上原文地址.hadoop hive hbase mahout ...

  5. springboot java基于协同过滤算法商品推荐系统vue

    互时动态更新该页面的 Web 应用 springboot是基于spring的快速开发框架, 相比于原生的spring而言, 它通过大量的java config来避免了大量的xml文件, 只需要简单的生 ...

  6. Python基于用户协同过滤算法电影推荐的一个小改进

    之前曾经推送过这个问题的一个实现,详见:Python基于用户协同过滤算法的电影推荐代码demo 在当时的代码中没有考虑一种情况,如果选出来的最相似用户和待测用户完全一样,就没法推荐电影了.所以,在实际 ...

  7. 个性化智能推荐(协同过滤算法)技术研究

    个性化智能推荐(协同过滤算法)技术研究 一.  协同过滤推荐(Collaborative Filtering简称 CF)        协同过滤技术是目前推荐系统中最成功和应用最广泛的技术,在理论研究 ...

  8. 【机器学习项目实战】Python基于协同过滤算法进行电子商务网站用户行为分析及服务智能推荐

    说明:这是一个机器学习实战项目(附带数据+代码+文档+代码讲解),如需数据+代码+文档+代码讲解可以直接到文章最后获取. 1.项目背景 电子商务网站数量迅速上升,将电子商务网站浏览者变为实际消费者,满 ...

  9. 基于协同过滤算法的商品推荐购物电商系统

    一.介绍 商品推荐是针对用户面对海量的商品信息而不知从何下手的一种解决方案,它可以根据用户的喜好,年龄,点击量,购买量以及各种购买行为来为用户推荐合适的商品.在本项目中采用的是基于用户的协同过滤的推荐 ...

最新文章

  1. linux安装eclipse运行web,Linux安装Tomcat,运行Eclipse,web项目
  2. 机器学习领域中的六大误区
  3. Servlet-Access denied for user 'root'@'localhost' (using password: YES
  4. ABAP OO小例子
  5. 超实用!从0到1教你打造一个令人上瘾的聊天机器人
  6. Repeater分页
  7. 这一团糟的代码,真的是我写的?!
  8. MongoDB学习之在Windows下安装MongoDB
  9. python系统信息_Python获得操作系统信息
  10. LINUX下载编译zrtp
  11. 灰度决策--如何解决棘手复杂问题
  12. 三年级计算机课标,三年级信息技术教案上册
  13. 学号20189220 2018-2019-2 《密码与安全新技术专题》第四周作业
  14. Python 爬取妹子图02
  15. 疫情肆虐,延迟返工,今年的“金三银四”还能照常进行吗?
  16. USB总线转串口芯片:沁恒CH340
  17. linux刻录光驱是哪个好,Linux中使用mkisofs或genisoimage刻录光盘
  18. UNIX TIMESTAMP 与 TIME 之间的转换
  19. AD(Altium Designer)PCB布线中的“格式刷”,助力快速布局布线
  20. AD20画PCB的学习之路(一)

热门文章

  1. java 54张扑克牌_Java基础高级综合练习题扑克牌的创建
  2. 基于asp.net+vbscript+wsc编写网站
  3. android时间控制器,android UiAutomator长按实现控制按住控件时间的方法
  4. 高考后的我们要去追逐星辰大海
  5. JavaScript原型链实现继承
  6. 深度学习项目训练时突然卡住或崩盘问题
  7. 计算机平面设计基础 photoshop基本操作
  8. 长篇故事| 世上的感情真的需要门当户对吗?
  9. 苹果系统 虚拟机_大连win10远程双系统重装电脑维修7苹果笔记本安装做虚拟机服务mac8...
  10. 大鹏背景,大鹏为什么能邀请那么多明星?揭秘董成鹏的成名路