【转】https://blog.csdn.net/haozi_rou/article/details/104888594
在生成ALS和LR模型以后,接下来就可以用在代码中了。
首先ALS,其实在数据已经存在数据库中了,只要从中取出来,去掉个逗号之类的就好
@Service public class RecommendService { @Autowired private RecommendDOMapper recommendDOMapper; //找回数据,根据userid召回shopidList public List<Integer> recall(Integer userId){ RecommendDO recommendDO = recommendDOMapper.selectByPrimaryKey(userId); if (recommendDO == null){ recommendDO = recommendDOMapper.selectByPrimaryKey(99999); } String[] shopIdArr = recommendDO.getRecommend().split(","); List<Integer> shopIdList = new ArrayList<>(); for (int i = 0 ; i < shopIdArr.length ; i ++){ shopIdList.add(Integer.valueOf(shopIdArr[i])); } return shopIdList; } }
对于LR:
@Service public class RecommendSortService { private SparkSession spark; private LogisticRegressionModel lrModel; @PostConstruct public void init(){ //初始化spark运行环境 spark = SparkSession.builder() .master("local") .appName("DianpingApp") .getOrCreate(); lrModel = LogisticRegressionModel.load("file:///F:/mouseSpace/project/background/lr/lrmodel"); } public List<Integer> sort(List<Integer> shopIdList , Integer userId){ //需要根据lrmodel所需要的11维的x生成特征,然后调用预测方法 List<ShopSortModel> list = new ArrayList<>(); for (Integer shopId : shopIdList){ //造的假数据 Vector v = Vectors.dense(1,0,0,0,0,1,0.6,0,0,1,0); Vector result = lrModel.predictProbability(v); double[] arr = result.toArray(); double score = arr[1]; // lrModel.predict(v); 如果用这个,就是返回1或者0 ShopSortModel shopSortModel = new ShopSortModel(); shopSortModel.setShopId(shopId); shopSortModel.setScore(score); list.add(shopSortModel); } list.sort(new Comparator<ShopSortModel>() { @Override public int compare(ShopSortModel o1, ShopSortModel o2) { if (o1.getScore() < o2.getScore()){ return -1; }else if (o1.getScore() > o2.getScore()){ return 1; }else { return 0; } } }); return list.stream().map(shopSortModel -> shopSortModel.getShopId()).collect(Collectors.toList()); } }
代码中自己造了一个数据,所以结果会有些偏差。
对于GBDT
跟lr算法非常像
public class GBDTRecommendSortService { private SparkSession spark; private GBTClassificationModel gbtClassificationModel; @PostConstruct public void init(){ //初始化spark运行环境 spark = SparkSession.builder() .master("local") .appName("DianpingApp") .getOrCreate(); gbtClassificationModel = GBTClassificationModel.load("file:///F:/mouseSpace/project/background/lr/gbdtmodel"); } public List<Integer> sort(List<Integer> shopIdList , Integer userId){ //需要根据lrmodel所需要的11维的x生成特征,然后调用预测方法 List<ShopSortModel> list = new ArrayList<>(); for (Integer shopId : shopIdList){ //造的假数据 Vector v = Vectors.dense(1,0,0,0,0,1,0.6,0,0,1,0); Vector result = gbtClassificationModel.predictProbability(v); double[] arr = result.toArray(); double score = arr[1]; // lrModel.predict(v); 如果用这个,就是返回1或者0 ShopSortModel shopSortModel = new ShopSortModel(); shopSortModel.setShopId(shopId); shopSortModel.setScore(score); list.add(shopSortModel); } list.sort(new Comparator<ShopSortModel>() { @Override public int compare(ShopSortModel o1, ShopSortModel o2) { if (o1.getScore() < o2.getScore()){ return -1; }else if (o1.getScore() > o2.getScore()){ return 1; }else { return 0; } } }); return list.stream().map(shopSortModel -> shopSortModel.getShopId()).collect(Collectors.toList()); } }
A/B Test
它可以帮助我们决策算法的好坏,提供更多的真实依据的手段。
在真实场景中,假如现有的是LR算法,那么我现在马上在线上换成GBDT,当然是有很大风险的,那么AB TEST就出现了,假如有10条数据,我可以分5条用lr算法,5条用GBDT算法,然后将两个依次穿插,形成一个结果集发给前端,然后通过记录点击率来验证哪种算法更好。