美文网首页程序员
文本分类的特征选择——DF和卡方检验

文本分类的特征选择——DF和卡方检验

作者: 凌冰_lonny | 来源:发表于2018-07-31 10:50 被阅读325次

收集好了训练样本集,需要对文本词语进行特征选择。
特征选择的意义有两个:
1.去掉噪音
去噪一个是去掉无意义的词,像只有几个文档出现过的词,或者每一个文档都出现的词。或者在很多类别里面都存在的词,其实都没有太大的意义。因为这些词对分类的结果起不到太大的作用。
2.降低维度
降维的作用主要是减少运算复杂度,加快运算速度。如果是自己的电脑一般也不会配置一个256g内存,从这个方面看,筛选的特征越少越好。

常见的特征选择方法,就如宗成庆的《统计自然语言处理》里面写的,有文档频率DF,互信息MI,信息增益IG,卡方检验CHI等等这几种方法。

DF很好理解的,文档频率。就是计算一个词出现的文章数目,非常简单的统计。

先跑了一遍DF,文本数是25236篇。十个类型,数据量分布不均匀。
跑出来词量是88w+。
贴上计算DF的代码

HashMap<String, Double> DFMap = new HashMap<String, Double>();
//在挑选训练集的基础上,计算文档频率DF
    public void getDF(String path) throws IOException {
        for (int i = 0; i < fileList.size(); i++) {
            HashSet<String> idSet = readid(fileList.get(i));
            for (String id : idSet) {
                Item item = hbase.getItem(id);
                if(item == null) {
                    LOG.info("is null . id "+id);
                    continue;
                    }
                List<Feature> keywords = item.getkeywords();
                for (Feature feature : keywords) {
                    if(DFMap.containsKey(feature.getName().trim())) {
                        DFMap.put(feature.getName().trim(), DFMap.get(feature.getName().trim())+1);
                    }
                    else {
                        DFMap.put(feature.getName().trim(), 1.0);
                    }
                }
            }
        }
        FileWriter fw = new FileWriter(path,true);
        for(Entry<String, Double> entry : DFMap.entrySet()) {
            fw.write(entry.getKey()+"\t"+entry.getValue()+"\n");
        }
        fw.flush();
        fw.close();
    }

然后,观察了一下,去掉了某些无意义的词,然后把DF小于6的都去掉了,一下就清爽了不少。还有把大于7000的都去掉了,这部分没多少。这样剩下9w+的词汇。
接着计算CHI检验。
CHI又叫卡方统计量,或者卡方检验。
先画个表格

特征\类别 Cx ~Cx
ti A B
~ti C D

然后我来说说这几个变量的意思。
表头C表示某个类别C,某个用Cx来说明
~C自然就表示非某个类别C
t表示预料中的词,ti就表示某个词
有一个全局变量N,表示训练集中全部文档数目。
A表示属于Cj类且包含ti的文档数目
B表示不属于Cj包含ti的文档数目
C表示属于Cj类但不包含ti的文档数目
D表示不属于Cj类也不包含ti的文档数目
是不是很简单~~~
然后 这个卡方统计量的计算方法就是

好像这里不好输入公式我还是粘个图吧


CHI的计算公式

就是这样的,最后,选取在所有类别C中,卡方最大的值作为ti的最后结果。

    double n = 0;
    //chi检验
    class chiword {
        public chiword() {
            a = 0;
            b = 0;
            c = 0;
            d = 0;
            chi = 0;
        }

        String category;
        double a;
        double b;
        double c;
        double d;
        double chi;

        public double calCHI() {
            double result  = n * Math.sqrt((a * d - c * b)) / ((a + c) * (b + d) * (a + b) * (c + d));
            return result;
        }
    }
    HashSet<String> keywordSet = new HashSet<String>();
    private void readDFWord(String path) throws IOException {
        FileReader fr = new FileReader(path);
        BufferedReader br = new BufferedReader(fr);
        String line = null;
        while ((line = br.readLine()) != null) {
            String keyword = line.split("\t")[0].trim();
            keywordSet.add(keyword);
        }
        br.close();
        fr.close();
    }

    private HashSet<String> readid(String path) throws IOException {
        HashSet<String> idSet = new HashSet<String>();
        FileReader fr = new FileReader(path);
        BufferedReader br = new BufferedReader(fr);
        String line = null;
        while ((line = br.readLine()) != null) {
            idSet.add(line.trim());
        }
        br.close();
        fr.close();
        return idSet;
    }

    List<String> categoryList = new ArrayList<String>();
    List<String> fileList = new ArrayList<String>();

    public void refreshFileList(String localPath) {
        File dir = new File(localPath);
        File[] files = dir.listFiles();

        if (files == null)
            return;
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                refreshFileList(files[i].getAbsolutePath());
            } else {
                fileList.add(files[i].getAbsolutePath());
                categoryList.add(files[i].getName());
            }
        }
        LOG.info("get category num is "+categoryList.size());
    }

    HashMap<String, List<chiword>> termMap = new HashMap<String, List<chiword>>();
    HashMap<String, Double> categoryNum = new HashMap<String, Double>();
    public void process() throws IOException {
        for (int i = 0; i < fileList.size(); i++) {
            String category = categoryList.get(i);
            HashSet<String> idSet = readid(fileList.get(i));
            double cateNum = 0;
            for (String id : idSet) {
                LOG.info("Begin process id "+id);
                travel_item item = hbase.getItem(id);
                if(item == null) {
                    LOG.info("is null . id "+id);
                    continue;
                    }
                cateNum++;
                n++;
                LOG.info("n is "+n);
                List<Feature> keywords = item.getkeywords();
                for (Feature feature : keywords) {
                    if(!keywordSet.contains(feature.getName().trim())) {
                        continue;
                    }
                    if (termMap.containsKey(feature.getName().trim())) {
                        List<chiword> chilist = termMap.get(feature.getName().trim());
                        for (int j = 0; j < chilist.size(); j++) {
                            chiword chiw = chilist.get(j);
                            if (chiw.category.equals(category)) {
                                chiw.a = chiw.a + 1;
                            } else {
                                chiw.b = chiw.b + 1;
                            }
                            chilist.set(j, chiw);
                        }
                        termMap.put(feature.getName().trim(), chilist);
                    } else {
                        List<chiword> chilist = new ArrayList<chiword>();
                        for (int k = 0; k < categoryList.size(); k++) {
                            chiword chiw = new chiword();
                            chiw.category = categoryList.get(k);
                            LOG.info("chiw category is "+ chiw.category);
                            if (chiw.category.equals(category)) {
                                chiw.a = chiw.a + 1;
                            } else {
                                chiw.b = chiw.b + 1;
                            }
                            chilist.add(chiw);
                        }
                        termMap.put(feature.getName().trim(), chilist);
                    }
                }
            }
            categoryNum.put(category, cateNum);
            LOG.info("categoryMap put "+category+"\t"+cateNum);
        }
    }
    HashMap<String, Double> termChiMap = new HashMap<String, Double>();
    
    public void getMax() {
        LOG.info("keyword size is "+termMap.size());
        for(Entry<String, List<chiword>> entry : termMap.entrySet()) {
            String term = entry.getKey();
            LOG.info(" get keyword is "+term);
            double chivalue = 0;
            List<chiword> chilist = entry.getValue();
            for(int i = 0; i< chilist.size(); i++) {
                chiword chiw = chilist.get(i);
                chiw.c = categoryNum.get(chiw.category) - chiw.a;
                chiw.d = n - categoryNum.get(chiw.category) - chiw.b;
                chiw.chi = chiw.calCHI();
                LOG.info("CHI value is "+chiw.category+"\t"+chiw.a+"\t"+chiw.b+"\t"+chiw.c+"\t"+chiw.d+"\t"+chiw.chi);
                if(chiw.chi > chivalue) {
                    chivalue = chiw.chi;
                }
            }
            termChiMap.put(term, chivalue);
        }
    }
    
    public void output(String path) throws IOException {
        FileWriter fw = new FileWriter(path);
        for(Entry<String, Double> entry: termChiMap.entrySet()) {
            fw.write(entry.getKey()+"\t"+entry.getValue()+"\n");
        }
        fw.flush();
        fw.close();
    }
    
    public static void main(String[] args) throws IOException {
        FeatureSelect ob = new FeatureSelect();
        ob.refreshFileList(args[0]);
        //计算CHI
        ob.readDFWord(args[2]);
        ob.process();
        ob.getMax();
        ob.output(args[1]);

    }

通过上述的计算,最后保留了CHI较大的数值,维度为81000个词。
好了 ,结束了。

相关文章

  • 文本分类的特征选择——DF和卡方检验

    收集好了训练样本集,需要对文本词语进行特征选择。特征选择的意义有两个:1.去掉噪音去噪一个是去掉无意义的词,像只有...

  • pyspark卡方检验用于特征选择

    卡方检验特征选择原理 计算特征变量与因变量的卡方独立性检验统计量,如果特征变量与因变量独立,说明其对预测因变量效果...

  • 卡方检验案例

    通常情况下,卡方检验是研究分类数据与分类数据之间关系的分析方法,如研究性别和是否吸烟之间的关系。卡方检验通常会涉及...

  • 特征选择之Chi卡方检验

    卡方检验最基本的思想就是通过观察实际值与理论值的偏差来确定理论的正确与否。具体做的时候常常先假设两个变量确实是独立...

  • 卡方检验

    下面我来介绍一下在R和excel如何使用卡方检验 个人理解,卡方检验和方差分析是对分类变量的分析,方差分析是针对不...

  • 卡方分布知识说明

    卡方分布定义 卡方分布的性质 卡方分布的数字特征 拓展SPSSAU卡方检验 卡方分布定义 设X1,X2,⋯,Xn是...

  • 文本分类

    文本分类的一般流程: 预处理 文本表示及特征选择 构造分类器 分类 文本分类的应用 垃圾邮件的判定:是否为垃圾邮件...

  • 特征选择__过滤型

    过滤型--方差过滤法 过滤方差较低的特征 过滤型--## 卡方检验 考虑当前特征和目标特征的相关性,但是容易删除有...

  • 卡方检验

    ①四格表卡方检验 ②配对卡方检验 ③RxC卡方检验

  • 常用的文本分类的特征选择算法

    常采用特征选择方法。常见的六种特征选择方法: 1)DF(Document Frequency) 文档频率 DF:统...

网友评论

    本文标题:文本分类的特征选择——DF和卡方检验

    本文链接:https://www.haomeiwen.com/subject/qghgmftx.html