推荐系统——基于用户的协同过滤算法
1.RelateDTO
package com.example.entity;
/**
* 协同过滤算法
*/
public class RelateDTO {
/** 用户id */
private Integer useId;
/** 商品id */
private Integer goodsId;
/** 指数 */
private Integer index;
public RelateDTO(Integer useId, Integer goodsId, Integer index) {
this.useId = useId;
this.goodsId = goodsId;
this.index = index;
}
public Integer getUseId() {
return useId;
}
public void setUseId(Integer useId) {
this.useId = useId;
}
public Integer getGoodsId() {
return goodsId;
}
public void setGoodsId(Integer goodsId) {
this.goodsId = goodsId;
}
public Integer getIndex() {
return index;
}
public void setIndex(Integer index) {
this.index = index;
}
}
2.CoreMath
package com.example.utils.recommend;
import cn.hutool.core.collection.CollectionUtil;
import com.example.entity.RelateDTO;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.IntStream;
/**
* 基于用户的协同过滤的核心算法
*/
public class CoreMath {
/**
* 计算相关系数并排序
*/
public static Map<Integer, Double> computeNeighbor(Integer key, Map<Integer, List<RelateDTO>> map, int type) {
Map<Integer, Double> distMap = new TreeMap<>();
List<RelateDTO> userItems = map.get(key);
if (CollectionUtil.isNotEmpty(userItems)) {
map.forEach((k, v) -> {
//排除此用户
if (!k.equals(key)) {
//关系系数
double coefficient = relateDist(v, userItems, type);
//关系距离
double distance = Math.abs(coefficient);
distMap.put(k, distance);
}
});
}
return distMap;
}
/**
*
原文地址:https://blog.csdn.net/weixin_59798969/article/details/135711637
免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!