商品标题类目判断的实现

根据已有的商品类目标题库,使用Liblinear生成模型,对未知品类商品的标题分类。本文档是工程文档,分类方法的选择,算法的参数设定,benchmark参考我的另一篇博客商品标题类目预判算法调参

工程结构:

  • 包结构

train:训练入口
category:类目精简及映射
model:训练集和测试集的数据结构
examiner:训练效果检测
predict:单个商品title的类目检测

  • 资源

vector/*:类目title文件,文件名是类目号,内容是该类目分词后的title集合
category_code_collect.txt:有用类目列表
category_map.txt:类目映射表

  • 结果

model:训练生成的模型,具体解释看liblinear文档
test_percent.txt:所有测试title的预测结果/标记类目,总体命中率。
test_data.txt:测试title集

程序结构

训练类目分类器.png-22.1kB

一.类目精简及映射

QQ截图20160112115159.png-5.5kB

二.读取类目title数据到训练集,测试集

FeatureNode是使用Liblinear的基本单元,它有index和value属性,ArrayList<ArrayList<FeatureNode>> x_trainDataMatrix是训练集title,ArrayList<Double> trainLabelList保存训练集cid。比如:
5000.png-5.8kB
是类目500019279的title文件,我们的存储为:
500019279 114344:1 127329:1 146946:1
500019279 4522:1 35322:1 47670:1 94559:1 75745:1 114320:1 120807:1 162660:1

注:FeatureNode的List要按照index的顺序排列

三.设定分类参数

struct parameter
{
        int solver_type; //分类方法选择
        double eps;             // stopping criteria 
        double C;           //cost of constrain
        int nr_weight;
        int *weight_label;
        double* weight;
        double p;
};

选择L2 - regularized L2- loss support vector classification分类方法,C=0.25,eps=0.1, eps=0.01取得了较好的效果。

四.设定训练参数

struct problem
{
    int l; //训练集合长度, x_trainDataMatrix.size()
    int n; //特征数目(词典词个数 + 偏移1):171045 + 1
    int *y; //训练集tag, trainLabelList
    struct feature_node **x; //Feature的二维数组格式,x_trainDataMatrix
    double bias;
};

五.训练并保存模型

aModel = Linear.train(aProblem, aParameter);

把得到的模型保存成文件,供检测使用:
QQ截图20160112144252.png-17.3kB
model格式:

solver_type; //训练参数
nr_class;    //类目数量
int *label;      //类目号
double bias;     //偏移
int nr_feature;  //特征数量
double *w;  //支撑向量value

六.效果检测

1.单条title检测

title:"苹果手机iphone6s 全新"
分词结果:苹果 手机 iphone
查词典:苹果:136631 手机:76330 iphone:11969 全新:39527
组成一个title的向量:aNodeList:
136631:1 76330:1 11969:1 39527:1

double result = Linear.predict(aModel, aNodeList);

2.模型效果检测

DefaultModelExaminer.calcTestData(TrainTestDataInfo dataInfo, Model aModel)

读取测试集,输出命中率test_percent.txt:
123.png-6.9kB
格式:目标cid/预测的cid title向量
命中个数/测试集大小

Comments
Write a Comment
  • Lx518 reply

    您好,您有商品类目分类的数据集吗?