阿里巴巴大数据竞赛-天池

本文探讨了天池数据的特点与挑战,包括同用户对同物品的多操作、购买欲与价格关注的区分及重复购买率问题。通过代码实现了一个推荐系统,采用点击、加购、购买和收藏四种行为的分析,设计了基于用户行为的预测评分模型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本人菜鸟,对天池数据写点小理解。天池数据不同于一般的评分数据,有几点比较主要:天池数据存在同用户对同物品的不同操作,这和SVD相背,所以数据处理这块非常重要。又根据啊里的背景来看,会发现数据存在这样一个内部关系。购买的物品必然受到点击到购物车,那么这个时候购物车的分析意义有多大?值得商榷。同时那么多的点击次数是因为对找个商品有购买欲望?还是因为购买过来看看价格波动?同时淘宝的数据存在这样一个问题,例如我买的小零食可以出现多次购买,但是购买例如电脑、冰箱这种大物件基本存在二次购买的几率比较小。而数据经过加密处理,这种情况怎么考虑?等等。其这篇文章主要还是一贯作风,贴代码,留个纪念。效果不是很好,F1只有6.4.仅当纪念。

#include <iostream>
#include <cstring>
#include <fstream>
#include <map>
#include <vector>

using namespace std;

void load(string);
void train(vector<int>,vector<int>,vector<int>,int);
void ComM(vector<int>,vector<int>,vector<int>);
void ComR(vector<int>,vector<int>,vector<int>);
void TopN(vector<map<vector<int>,double> >);
double W1 = 1;
double W2 = 1;

map<vector<int>,double> predictScore(int,vector<int>,vector<int>,int);
vector<int> U,I,R;
vector<int> MinRc,MaxRc,MaxRv,MinRv,MaxRp,MinRp,MaxRg,MinRg;
map<vector<int>,int> RCui,RVui,RPui,RGui;
vector<map<vector<int>,double> > result;


int step = 1;

int main()
{
    string trains_file = "/home/ja/CADATA/ALI/data/New/trains_tmp.dat";
    load(trains_file);
    ComM(U,I,R);


    string train_file = "/home/ja/CADATA/ALI/data/New/trains.dat";

    load(train_file);
    ComR(U,I,R);

    for(size_t i=0;i<step;i++){
        train(U,I,R,W1);
    }

    U.clear();
    I.clear();
    R.clear();
    RCui.clear();
    RVui.clear();
    RPui.clear();
    RGui.clear();

    string train_file_1 = "/home/ja/CADATA/ALI/data/New/test_s.dat";

    load(train_file_1);
    ComR(U,I,R);

    for(size_t i=0;i<step;i++){
        train(U,I,R,W2);
    }


    TopN(result);

    return 0;
}

void TopN(vector<map<vector<int>,double> > r){
    ofstream out("/home/ja/CADATA/ALI/data/New/result/result.dat");
    for(size_t i=0;i<r.size();i++){
        for(map<vector<int>,double>::iterator it=r[i].begin();it!=r[i].end();++it){
            out << it->first[0] << "\t" << it->first[1] << "\t" << it->second << endl;
        }
    }
}

map<vector<int>,double> predictScore(int user,vector<int> item,vector<int> rating,int index,int w){
    map<vector<int>,double> TMPc,TMPv,TMPp,TMPg,Tmpresult;
    for(map<vector<int>,int>::iterator it=RCui.begin();it!=RCui.end();++it){
        if(it->first[0] == index){
            double t = w * it->second / (MaxRc[index] * 1.0);
            //double t = it->second / (MaxRc[index] * 1.0);
            vector<int> tmp;
            tmp.push_back(index);
            tmp.push_back(it->first[1]);
            TMPc[tmp] += t;
            //cout << it->second<< "--" << MaxRc[index]<< "--" << t <<endl;
        }
    }

    for(map<vector<int>,int>::iterator it=RVui.begin();it!=RVui.end();++it){
        if(it->first[0] == index){
            double t = w * it->second / (MaxRv[index]* 1.0);
            vector<int> tmp;
            tmp.push_back(index);
            tmp.push_back(it->first[1]);
            TMPv[tmp] += t;
        }
    }

    for(map<vector<int>,int>::iterator it=RPui.begin();it!=RPui.end();++it){
        if(it->first[0] == index){
            double t = w * it->second / (MaxRp[index] * 1.0);
            //double t = it->second / (MaxRp[index] * 1.0);
            vector<int> tmp;
            tmp.push_back(index);
            tmp.push_back(it->first[1]);
            TMPp[tmp] += t;
        }
    }

    for(map<vector<int>,int>::iterator it=RGui.begin();it!=RGui.end();++it){
        if(it->first[0] == index){
            double t = w * it->second / (MaxRg[index] * 1.0);
            //double t = it->second / (MaxRg[index] * 1.0);
            vector<int> tmp;
            tmp.push_back(index);
            tmp.push_back(it->first[1]);
            TMPg[tmp] += t;
        }

    }

    map<vector<int>,double> TmpVv,TmpPp,TmpGg,TmpPpp,TmpGgg,TmpGggg;

    for(map<vector<int>,double>::iterator it=TMPc.begin();it!=TMPc.end();++it){
        double score = it->second;
        for(map<vector<int>,double>::iterator itt=TMPv.begin();itt!=TMPv.end();++itt){
            if(it->first[0] == itt->first[0] ){
                if(it->first[1] == itt->first[1]){
                    score += itt->second;
                }
                else{
                    vector<int> tmp;
                    tmp.push_back(itt->first[0]);
                    tmp.push_back(itt->first[1]);
                    TmpVv[tmp] = itt->second;
                    score += 0;
                }
            }
        }

        for(map<vector<int>,double>::iterator itt=TMPp.begin();itt!=TMPp.end();++itt){
            if(it->first[0] == itt->first[0] ){
                if(it->first[1] == itt->first[1]){
                    score += itt->second;
                }
                else{
                    vector<int> tmp;
                    tmp.push_back(itt->first[0]);
                    tmp.push_back(itt->first[1]);
                    TmpPp[tmp] = itt->second;
                    score += 0;
                }
            }
        }

        for(map<vector<int>,double>::iterator itt=TMPg.begin();itt!=TMPg.end();++itt){
            if(it->first[0] == itt->first[0] ){
                if(it->first[1] == itt->first[1]){
                    score += itt->second;
                }
                else{
                    vector<int> tmp;
                    tmp.push_back(itt->first[0]);
                    tmp.push_back(itt->first[1]);
                    TmpGg[tmp] = itt->second;
                    score += 0;
                }
            }
        }
        vector<int> tmp;
        tmp.push_back(it->first[0]);
        tmp.push_back(it->first[1]);
        Tmpresult[tmp] += score;
    }

    double size = Tmpresult.size();

    if(TmpVv.size() != 0){
        for(map<vector<int>,double >::iterator it = TmpVv.begin();it!=TmpVv.end();++it){
            double score = it->second;
            for(map<vector<int>,double>::iterator itt=TmpPp.begin();itt!=TmpPp.end();++itt){
                if(it->first[0] == itt->first[0]){
                    if(it->first[1] == itt->first[1]){
                        score += itt->second;
                    }
                    else{
                        vector<int> tmp;
                        tmp.push_back(itt->first[0]);
                        tmp.push_back(itt->first[1]);
                        TmpPpp[tmp] = itt->second;
                        score += 0;
                    }
                }
            }

            for(map<vector<int>,double>::iterator itt=TmpGg.begin();itt!=TmpGg.end();++itt){
                if(it->first[0] == itt->first[0] ){
                    if(it->first[1] == itt->first[1]){
                        score += itt->second;
                    }
                    else{
                        vector<int> tmp;
                        tmp.push_back(itt->first[0]);
                        tmp.push_back(itt->first[1]);
                        TmpGgg[tmp] = itt->second;
                        score += 0;
                    }
                }
            }
            vector<int> tmp;
            tmp.push_back(it->first[0]);
            tmp.push_back(it->first[1]);
            Tmpresult[tmp] += score;
        }
    }




    if(TmpPp.size() != 0){
        for(map<vector<int>,double >::iterator it = TmpPpp.begin();it!=TmpPpp.end();++it){
            double score = it->second;
            for(map<vector<int>,double>::iterator itt=TmpGgg.begin();itt!=TmpGgg.end();++itt){
                if(it->first[0] == itt->first[0] ){
                    if(it->first[1] == itt->first[1]){
                        score += itt->second;
                    }
                    else{
                        vector<int> tmp;
                        tmp.push_back(itt->first[0]);
                        tmp.push_back(itt->first[1]);
                        TmpGggg[tmp] = itt->second;
                        score += 0;
                    }
                }
            }
            vector<int> tmp;
            tmp.push_back(it->first[0]);
            tmp.push_back(it->first[1]);
            Tmpresult[tmp] += score;
        }
    }






    if(TmpGggg.size() != 0){

        for(map<vector<int>,double >::iterator it = TmpGggg.begin();it!=TmpGggg.end();++it){
            double score = it->second;
            vector<int> tmp;
            tmp.push_back(it->first[0]);
            tmp.push_back(it->first[1]);
            Tmpresult[tmp] += score;
        }
    }
/*
    if(size != result.size()){
        cout << (size - result.size()) << endl;
    }
*/
    return Tmpresult;
}

void train(vector<int> User,vector<int> Item,vector<int> Rating,int W){
    map<vector<int>,double> Score;
    //for(size_t i=0;i<User.size();i++){
    for(size_t i=0;i<884;i++){
        Score = predictScore(User[i],Item,Rating,i,W);
        result.push_back(Score);
    }
}

void ComR(vector<int> User,vector<int> Item,vector<int> Rating){
    for(size_t i=0;i<User.size()-1;i++){
        if(Rating[i] == 0){
            if(User[i] == User[i+1] && Item[i] == Item[i+1]){
                vector<int> tmp;
                tmp.push_back(User[i]);
                tmp.push_back(Item[i]);
                RCui[tmp] += 1;
            }
            else{
                vector<int> tmp;
                tmp.push_back(User[i]);
                tmp.push_back(Item[i]);
                RCui[tmp] += 1;
            }
        }
        if(i == User.size()-2){
            vector<int> tmp;
            tmp.push_back(User[i+1]);
            tmp.push_back(Item[i+1]);
            RCui[tmp] += 1;
        }
    }

    for(size_t i=0;i<User.size()-1;i++){
        if(Rating[i] == 1){
            if(User[i] == User[i+1] && Item[i] == Item[i+1]){
                vector<int> tmp;
                tmp.push_back(User[i]);
                tmp.push_back(Item[i]);
                RVui[tmp] += 1;
            }
            else{
                vector<int> tmp;
                tmp.push_back(User[i]);
                tmp.push_back(Item[i]);
                RVui[tmp] += 1;
            }
        }
    }

    for(size_t i=0;i<User.size()-1;i++){
        if(Rating[i] == 2){
            if(User[i] == User[i+1] && Item[i] == Item[i+1]){
                vector<int> tmp;
                tmp.push_back(User[i]);
                tmp.push_back(Item[i]);
                RPui[tmp] += 1;
            }
            else{
                vector<int> tmp;
                tmp.push_back(User[i]);
                tmp.push_back(Item[i]);
                RPui[tmp] += 1;
            }
        }
    }

    for(size_t i=0;i<User.size()-1;i++){
        if(Rating[i] == 3){
            if(User[i] == User[i+1] && Item[i] == Item[i+1]){
                vector<int> tmp;
                tmp.push_back(User[i]);
                tmp.push_back(Item[i]);
                RGui[tmp] += 1;
            }
            else{
                vector<int> tmp;
                tmp.push_back(User[i]);
                tmp.push_back(Item[i]);
                RGui[tmp] += 1;
            }
        }
    }
}

void ComM(vector<int> User,vector<int> Item,vector<int> Rating){
    int max = 0;
    int num = 1;
    int min = 1000;
/*
    for(size_t un=0;un<884;un++){
        for(size_t i=0;i<User.size()-1;i++){
            if(Rating[i] == 0 && User[i] == un){
                if(User[i] == User[i+1] && Rating[i] == Rating[i+1] && Item[i] == Item[i+1]){
                    num += 1;
                    if(num > max){
                        max = num;
                    }
                }
                else{
                    if(num > max){
                        max = num;
                    }
                    num = 1;
                }
            }
        }
        MaxRc.push_back(max);
        num = 1;
        max = 0;
    }
    */
    for(size_t un=0;un<884;un++){
        for(size_t i=0;i<User.size()-1;i++){
            if(Rating[i] == 0 && User[i] == un){
                if(User[i] == User[i+1] && Rating[i] == Rating[i+1] && Item[i] == Item[i+1]){
                    num += 1;
                    if(num > max){
                        max = num;
                    }
                }
                else{
                    if(num > max){
                        max = num;
                    }
                    num = 1;
                }
            }
        }
        MaxRc.push_back(max);
        num = 1;
        max = 0;
    }

    for(size_t un=0;un<884;un++){
        for(size_t i=0;i<User.size()-1;i++){
            if(Rating[i] == 1 && User[i] == un){
                //cout << i << " " << Rating[i] << " " << un << endl;
                if(User[i] == User[i+1] && Rating[i] == Rating[i+1] && Item[i] == Item[i+1]){
                    num += 1;
                    if(num > max){
                        max = num;
                    }
                }
                else{
                    num = 1;
                    if(num > max){
                        max = num;
                    }
                }
            }
        }
        MaxRv.push_back(max);
        num = 1;
        max = 0;
    }

    for(size_t un=0;un<884;un++){
        for(size_t i=0;i<User.size()-1;i++){
            if(Rating[i] == 2 && User[i] == un){
                //cout << i << " " << Rating[i] << " " << un << endl;
                if(User[i] == User[i+1] && Rating[i] == Rating[i+1] && Item[i] == Item[i+1]){
                    num += 1;
                    if(num > max){
                        max = num;
                    }
                }
                else{
                    num = 1;
                    if(num > max){
                        max = num;
                    }
                }
            }
        }
        MaxRp.push_back(max);
        num = 1;
        max = 0;
    }

    for(size_t un=0;un<884;un++){
        for(size_t i=0;i<User.size()-1;i++){
            if(Rating[i] == 3 && User[i] == un){
                //cout << i << " " << Rating[i] << " " << un << endl;
                if(User[i] == User[i+1] && Rating[i] == Rating[i+1] && Item[i] == Item[i+1]){
                    num += 1;
                    if(num > max){
                        max = num;
                    }
                }
                else{
                    num = 1;
                    if(num > max){
                        max = num;
                    }
                }
            }
        }
        MaxRg.push_back(max);
        num = 1;
        max = 0;
    }

}

void load(string file){
    ifstream fin(file.c_str());
    if(!fin){
        cout << "error for fileName" << endl;
    }

    int userId,itemId,rating;
    while(fin >> userId >> itemId >> rating){
        U.push_back(userId);
        I.push_back(itemId);
        R.push_back(rating);
    }
    fin.close();
}

 

转载于:https://www.cnblogs.com/wn19910213/p/3661891.html

AliDMCompetition 阿里巴巴大数据竞赛(http://102.alibaba.com/competition/addDiscovery/index.htm ) 数据说明 提供的原始文件有大约4M左右,涉及1千多天猫用户,几千个天猫品牌,总共10万多条的行为记录。 用户4种行为类型(Type)对应代码分别为: 点击:0 购买:1 收藏:2 购物车:3 提交格式 参赛者将预测的用户存入文本文件中,格式如下: user_id \t brand_id , brand_id , brand_id \n 上传的结果文件名字不限(20字以内),文件必须为txt格式。 预测结果 真实购买记录一共有3526条 TODO 注意调整正负样本比例 在LR的基础上做RawLR。按照天猫内部的思路来。 在LR的基础上做MRLR,样本提取要更加合理。 在UserCF和ItemCF上加上时间因子的影响。 利用UserCF做好的用户聚类、ItemCF做好的品牌聚类来做细化的LR,或者在聚类 上做LFM 在ItemCF的思路上挖掘频繁项集/购买模式,如购买品牌A和商品后往往会购买 品牌B的商品 LFM 数据集特征 某一商品在购买前的一段时间内会出现大量点击次数,购买完成后的一段时间内也会出现大量点击次数 用户在本月有过行为的商品极少出现在下个月的购买列表里 根据观察推断:用户浏览商品的行为可分为两类: 无目的浏览,可能会在浏览过程中对某些中意的商品进行购买,数据表现为有大量点击次数<=2的行为记录,但很少有购买行为 有目的的查找商品,可能是事先有需求的情况,数据表现为一段时间内点击商品数很少, 但点击过的商品大多数都进行了购买 参考论文 See https://www.google.com.hk/search?q=data+mining+time+series&ie=utf-8&oe=utf-8&aq=t for more. Chapter 1 MINING TIME SERIES DATA - ResearchGate 模型列表 LR(model=LinearSVC(C=10, loss='l1'), alpha=0.7, degree=1) | TOTAL VISITED BOUGHT FAVO CART NEW | Pred # 1438 1436 626 71 12 | % 100% 99.861% 43.533% 4.937% 0.834% | Real # 1311 250 89 10 1 | % 100% 19.069% 6.789% 0.763% 0.076% Hit # 76 Precision 5.285118% Recall 5.797101% F1 Score 5.529283% LR(model=LogisticRegression(penalty='l1'), alpha=0.7, degree=1) | TOTAL VISITED BOUGHT FAVO CART NEW | Pred # 1472 1470 615 68 14 | % 100% 99.864% 41.780% 4.620% 0.951% | Real # 1311 250 89 10 1 | % 100% 19.069% 6.789% 0.763% 0.076% Hit # 74 Precision 5.027174% Recall 5.644546% F1 Score 5.318002% 这个模型在数据变成2次后,Precision ~ 16%,同时F1 ~ 3% LR(model=Perceptron(penalty='l1'), alpha=0.7, degree=1) | TOTAL VISITED BOUGHT FAVO CART NEW | Pred # 3145 3140 1023 130 26 | % 100% 99.841% 32.528% 4.134% 0.827% | Real # 1311 250 89 10 1 | % 100% 19.069% 6.789% 0.763% 0.076% Hit # 113 Precis
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值