Giter Site home page Giter Site logo

itemcf's Introduction

ItemCF

个性化推荐(基于物品的协同过滤算法) 个性化推荐_视频Demo

一、原理

  1. 计算物品之间的相似度
  2. 根据物品的相似度和用户的历史行为给用户生成推荐列表

二、算法实现和应用

1、计算物品之间的相似度[余弦相似度]

设 N(u) 表示喜欢物品u的用户数,| N(u) ⋂ N(u) |表示同时喜欢物品u物品v的用户数,那么物品 u 和 v 的相似度为

以Demo为例,为用户1001推荐商品。

1.1、建立用户-物品倒排表

1.2、计算共现矩阵,N[i]表示除去当前用户喜欢物品i的人数

//初始矩阵
for (int i = 0; i < likeLists.size(); i++) {
    int pid1 = likeLists.get(i).getPid();
    ++N[pid1];                                          //喜欢该id物品的人数加一
    for (int j = i + 1; j < likeLists.size(); j++) {
        int pid2 = likeLists.get(j).getPid();
        ++curMatrix[pid1][pid2];
        ++curMatrix[pid2][pid1];                        //对称加一
    }
}
//累加所有矩阵, 得到共现矩阵
for (int i = 0; i < products.size(); i++) {
    for (int j = 0; j < products.size(); j++) {
        int pid1 = products.get(i).getPid(), pid2 = products.get(j).getPid();
        comMatrix[pid1][pid2] += curMatrix[pid1][pid2];
        comMatrix[pid1][pid2] += curMatrix[pid1][pid2];
    }
}

1.3、计算余弦相似度矩阵W

当前用户1001点赞的物品ID为1,由共现矩阵可得Nij。(跳过当前用户1001喜欢得物品1)

根据余弦相似度公式,得Wij。

这里使用了TreeSet重写compare方法实现添加的商品按点赞数排序。

TreeSet<Product> preList = new TreeSet<Product>(new Comparator<Product>() {
    //重写compare方法  按相似度Wij排序;当相似度Wij相同时,按点赞数排序
    @Override
    public int compare(Product o1, Product o2) {
        if(o1.getW()!=o2.getW()){
            return (int) ((o1.getW()-o2.getW())*100); //返回值为0,表示同一元素
        }
        else{
            //当相似度相同时,比较点赞数
            return o1.getCnt()-o2.getCnt();
        }
    }
}); //预处理的列表
for(Like like: likeLists){
    int Nij = 0;                         //既喜欢i又喜欢j的人数
    double Wij;                          //相似度
    Product tmp;                           //当前的产品

    int i = like.getPid();

    for(Product product: products){
        if(like.getPid() == product.getPid()) continue;
        int j = product.getPid();
        Nij = comMatrix[i][j];
        Wij = (double)Nij/Math.sqrt(N[i]*N[j]);         //计算余弦相似度

        tmp = productdao.findProductById(product.getPid());

        if(Double.isNaN(Wij)) tmp.setW(0);
        else tmp.setW(Wij);

        if(used[tmp.getPid()]) continue;               //已加入推荐列表

        if(!Double.isNaN(Wij) && Wij!=0){
            preList.add(tmp);
            used[tmp.getPid()] = true;
        }
    }
}

2、根据物品的相似度和用户的历史行为给用户生成推荐列表

此时推荐的物品列表为4,3,5。

ArrayList<Product> recomLists = new ArrayList<>();      //生成的推荐结果

for(int i = 0; preList.size()>0 && i<5; i++){
    recomLists.add(preList.pollLast());
}

3、推荐列表不足5个的按点赞数补充。

此时推荐的物品列表为4,3,5,2,0。

//推荐数量不满5个, 补足喜欢数最高的文章,考虑重复
if(recomLists.size()<5){
    recomLists = productdao.findTopNProducts(recomLists);
}

三、测试样例

在当前状态下,用户1001对物品6点赞,刷新后推荐列表更新为0,4,3,5,2。 简单过程(具体之后补)

itemcf's People

Contributors

xuzichang avatar

Stargazers

siliconx avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.