一、说明

  IG是information gain 的缩写,中文名称是信息增益,是选择特征的一个很有效的方法(特别是在使用svm分类时)。这里不做详细介绍,有兴趣的可以googling一下。

  chi-square 是一个常用特征筛选方法,在种子词扩展那篇文章中,有详细说明,这里不再赘述。

二、weka中的使用方法

  1、特征筛选代码

 package com.lvxinjian.alg.models.feature;

 import java.nio.charset.Charset;
import java.util.ArrayList; import weka.attributeSelection.ASEvaluation;
import weka.attributeSelection.AttributeEvaluator;
import weka.attributeSelection.Ranker;
import weka.core.Instances; import com.iminer.tool.common.util.FileTool;
/**
* @Description : 使用Weka的特征筛选方法(目前支持IG、Chi-square)
* @author Lv Xinjian
*
*/
public class FeatureSelectorByWeka { /**
* @function 使用weka内置的算法筛选特征
* @param eval 特征筛选方法的对象实例
* @param data arff格式的数据
* @param maxNumberOfAttribute 支持的最大的特征个数
* @param outputPath lex输出文件
* @throws Exception
*/
public void EvalueAndRank(ASEvaluation eval , Instances data ,int maxNumberOfAttribute , String outputPath) throws Exception
{
Ranker rank = new Ranker();
eval.buildEvaluator(data);
rank.search(eval, data); // 按照特定搜索算法对属性进行筛选 在这里使用的Ranker算法仅仅是属性按照InfoGain/Chi-square的大小进行排序
int[] attrIndex = rank.search(eval, data); // 打印结果信息 在这里我们了属性的排序结果
ArrayList<String> attributeWords = new ArrayList<String>();
for (int i = 0; i < attrIndex.length; i++) {
//如果权重等于0,则跳出循环
if (((AttributeEvaluator) eval).evaluateAttribute(attrIndex[i]) == 0)
break;
if (i >= maxNumberOfAttribute)
break;
attributeWords.add(i + "\t"
+ data.attribute(attrIndex[i]).name() + "\t" + "1");
}
FileTool.SaveListToFile(attributeWords, outputPath, false,
Charset.forName("utf8"));
} }
 package com.lvxinjian.alg.models.feature;

 import java.io.IOException;

 import weka.attributeSelection.ASEvaluation;
import weka.attributeSelection.ChiSquaredAttributeEval;
import weka.attributeSelection.InfoGainAttributeEval;
import weka.core.Instances;
import weka.core.converters.ConverterUtils.DataSource; import com.iminer.alg.models.generatefile.ParameterUtils; /**
* @Description : IG、Chi-square特征筛选
* @author Lv Xinjian
*
*/
public class WekaFeatureSelector extends FeatureSelector{ /**
* 最大的特征个数
*/
private int maxFeatureNum = 10000;
/**
* 特征文件保存路径
*/
private String outputPath = null;
/**
* @Fields rule 对于特征过滤的规则
*/
private String classname = "CLASS";
/**
* 特征筛选方法,默认为IG
*/
private String selectMethod = "IG"; private boolean Initialization(String options){
try {
String [] paramArrayOfString = options.split(" "); //初始化特征最大个数
String maxFeatureNum = ParameterUtils.getOption("maxFeatureNum", paramArrayOfString);
if(maxFeatureNum.length() != 0)
this.maxFeatureNum = Integer.parseInt(maxFeatureNum);
//初始化类别
String classname = ParameterUtils.getOption("class", paramArrayOfString);
if(classname.length() != 0)
this.classname = classname;
else{
System.out.println("use default class name(\"CLASS\")");
}
//初始化特征保存路径
String outputPath = ParameterUtils.getOption("outputPath", paramArrayOfString);
if(outputPath.length() != 0)
this.outputPath = outputPath;
else{
System.out.println("please initialze output path.");
return false;
}
String selectMethod = ParameterUtils.getOption("selectMethod", paramArrayOfString);
if(selectMethod.length() != 0)
this.selectMethod = selectMethod;
else{
System.out.println("use default select method(IG)");
}
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
@Override
public boolean selectFeature(Object obj ,String options) throws IOException {
try {
if(!Initialization(options))
return false;
Instances data = (Instances)obj;
data.setClass(data.attribute(this.classname));
ASEvaluation selector = null;
if(this.selectMethod.equals("IG"))
selector = new InfoGainAttributeEval();
else if(this.selectMethod.equals("CHI"))
selector = new ChiSquaredAttributeEval();
FeatureSelectorByWeka attributeSelector = new FeatureSelectorByWeka();
attributeSelector.EvalueAndRank(selector, data ,this.maxFeatureNum ,this.outputPath);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} return true;
} public static void main(String [] args) throws Exception
{
String root = "C:\\Users\\Administrator\\Desktop\\12_05\\模型训练\\1219\\";
WekaFeatureSelector selector = new WekaFeatureSelector();
Instances data = DataSource.read(root + "train.Bigram.arff");
String options = "-maxFeatureNum 10000 -outputPath lex.txt";
selector.selectFeature(data, options);
}
}

三、小结

  其实weka中还提供了一些其它的内嵌特征选择方法,用起来也比较省事儿,但是这里不在赘述。

weka数据挖掘拾遗(二)---- 特征选择(IG、chi-square)的更多相关文章

  1. weka数据挖掘拾遗(一)---- 生成Arff格式文件

    一.什么是arff格式文件 1.arff是Attribute-Relation File Format缩写,从英文字面也能大概看出什么意思.它是weka数据挖掘开源程序使用的一种文件模式.由于weka ...

  2. weka数据挖掘拾遗(三)----再谈如果何生成arff

    前一阵子写过一个arff的随笔,但是写完后发现有些啰嗦.其实如果使用weka自带的api,生成arff文件将变成一件很简单的事儿. 首先,可以先把特征文件生成csv格式的.csv格式就是每列数据都用逗 ...

  3. 初试weka数据挖掘

    初试weka数据挖掘 Posted on 2013-09-07 13:26 DM张朋飞 阅读(321) 评论(7) 编辑 收藏 偶然间在网上看到了一篇关于weka好的博文,就记录了下来…… weka下 ...

  4. Chi Square Distance

    The chi squared distance d(x,y) is, as you already know, a distance between two histograms x=[x_1,.. ...

  5. 特征选择之Chi卡方检验

    特征选择之Chi卡方检验 卡方值越大,说明对原假设的偏离越大,选择的过程也变成了为每个词计算它与类别Ci的卡方值,从大到小排个序(此时开方值越大越相关),取前k个就可以. 针对英文纯文本的实验结果表明 ...

  6. Weka中数据挖掘与机器学习系列之Weka简介(二)

    不多说,直接上干货! Weka简介 Weka是怀卡托智能分析环境(Waikato Environment for Knowledge Analysis)的英文字首缩写,官方网址为:http://www ...

  7. Redis命令拾遗二(散列类型)

    本文版权归博客园和作者吴双共同所有,欢迎转载,转载和爬虫请注明原文地址 :博客园蜗牛NoSql系列地址  http://www.cnblogs.com/tdws/tag/NoSql/ Redis命令拾 ...

  8. 数据挖掘(二)用python实现数据探索:汇总统计和可视化

    今天我们来讲一讲有关数据探索的问题.其实这个概念还蛮容易理解的,就是我们刚拿到数据之后对数据进行的一个探索的过程,旨在了解数据的属性与分布,发现数据一些明显的规律,这样的话一方面有助于我们进行数据预处 ...

  9. Java基础拾遗(二) — 关于equals(),hashcode()和 ==

    这里分别讲==和equals()的关系,以及equals()和hashcode()的关系 讲解之前,需要先明白对象的内容.对象的引用,基本类型,引用类型这几个概念,此处不做解释 一.==和equals ...

随机推荐

  1. 异构GoldenGate 12c 单向复制配置(支持DDL复制)

    1.开始配置OGG支持DDL复制(在source端操作) 1.1 赋予权限 SQL> conn /as sysdba 已连接. SQL> grant execute on utl_file ...

  2. MyBatis学习之多表查询

    一对多需求:即一张表class中又含有多张表(teacher,student)内容.现根据class_id 来获取对应的班级信息(包括学生和老师信息) 方式一:嵌套结果 使用嵌套结果映射来处理重复的联 ...

  3. [转]redhat7(centos7) not registered to Red Hat Subscription Management

    [root@controller0 ~]# yum install ntp Loaded plugins: fastestmirror, product-id, search-disabled-rep ...

  4. 部署OpenStack问题汇总(三)--Failed to add image

    使用glance add 上传完img文件的时候出现了下面的错误 ------------------------------------------------------------------- ...

  5. 编译源码 JAVA out of memory

  6. C#取得Web程序和非Web程序的根目录的N种取法

    取得控制台应用程序的根目录方法方法1.Environment.CurrentDirectory 取得或设置当前工作目录的完整限定路径方法2.AppDomain.CurrentDomain.BaseDi ...

  7. [通信] C# TCP实现多个客户端与服务端 数据 与 文件的传输

    说明: http://download.csdn.net/detail/chwei_cson/4423874 源码: http://download.csdn.net/download/meicanj ...

  8. svn和git的优缺点

    git官网api: https://git-scm.com/docs 一. 集中式vs分布式 1. Subversion属于集中式的版本控制系统集中式的版本控制系统都有一个单一的集中管理的服务器,保存 ...

  9. ftp命令大全

    FTP命令是Internet用户使用最频繁的命令之一,不论是在DOS还是UNIX操作系统下使用FTP,都会遇到大量的FTP内部命令.熟悉并灵活应用FTP的内部命令,可以大大方便使用者,并收到事半功倍之 ...

  10. logstash实战tcp插件

    vim /etc/logstash/conf.d/tcp.conf input{ tcp{ type => "tcp" port => "6666" ...