weka特征选择(IG、chi-square)
一、说明
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)
*
*/
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特征筛选
*
*/
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数据挖掘拾遗(二)---- 特征选择(IG、chi-square)
weka特征选择(IG、chi-square)的更多相关文章
- Chi Square Distance
The chi squared distance d(x,y) is, as you already know, a distance between two histograms x=[x_1,.. ...
- 特征选择之Chi卡方检验
特征选择之Chi卡方检验 卡方值越大,说明对原假设的偏离越大,选择的过程也变成了为每个词计算它与类别Ci的卡方值,从大到小排个序(此时开方值越大越相关),取前k个就可以. 针对英文纯文本的实验结果表明 ...
- 【Machine Learning】wekaの特征选择简介
看过这篇博客的都应该明白,特征选择代码实现应该包括3个部分: 搜索算法: 评估函数: 数据: 因此,代码的一般形式为: AttributeSelection attsel = new Attribut ...
- BendFord's law's Chi square test
http://www.siam.org/students/siuro/vol1issue1/S01009.pdf bendford'law e=log10(1+l/n) o=freq of first ...
- 文本挖掘之特征选择(python 实现)
机器学习算法的空间.时间复杂度依赖于输入数据的规模,维度规约(Dimensionality reduction)则是一种被用于降低输入数据维数的方法.维度规约可以分为两类: 特征选择(feature ...
- 使用Python的文本挖掘的特征选择/提取
在文本挖掘与文本分类的有关问题中,文本最初始的数据是将文档表示成向量空间模型的一个矩阵,而这个矩阵所拥有的就是不同的词,常采用特征选择方法.原因是文本的特征一般都是单词(term),具有语义信息,使用 ...
- scikit-learn:在实际项目中用到过的知识点(总结)
零.全部项目通用的: http://blog.csdn.net/mmc2015/article/details/46851245(数据集格式和预測器) http://blog.csdn.net/mmc ...
- NLP-特征选择
文本分类之特征选择 1 研究背景 对于高纬度的分类问题,我们在分类之前一般会进行特征降维,特征降维的技术一般会有特征提取和特征选择.而对于文本分类问题,我们一般使用特征选择方法. 特征提取:PCA.线 ...
- 用R进行市场调查和消费者感知分析
// // 问题到数据 理解问题 理解客户的问题:谁是客户(某航空公司)?交流,交流,交流! 问题要具体 某航空公司: 乘客体验如何?哪方面需要提高? 类别:比较.描述.聚类,判别还是回归 需要什么样 ...
随机推荐
- 我的Ubuntu系统配置所作的备份记录如下
Ubuntu无法关机解决办法 说明:如果不成功请参考一下文章最后的内容,也许会有帮助. 其实不止在ubuntu里面,fedora里面我也遇到了这个问题,就是电脑可以重启,但是不能直接关机,否则就一直停 ...
- 使用PHP编写发红包程序
使用PHP编写发红包程序 http://www.jb51.net/article/69815.htm 投稿:hebedich 字体:[增加 减小] 类型:转载 时间:2015-07-22 微信发红 ...
- jQuery 中 on 方法-----给未来元素添加事件
<li class='clear dir-li'> <div class='left title'> 添加到目录:</div> <div class='lef ...
- Centos下使用Heartbeat实现集群[转]
Linux 包括 CentOS 下高可用性(HA:High Availability)集群方案很多,而 Heartbeat 是比较常见和性价比比较高的一种。一、硬件及网络连接 群集一般需要2台以上服务 ...
- Ext TabPanel items高度宽度自适应
写Ext的时候经常会遇到一些莫名其妙,令人感到非常神奇的问题,甚至都没办法用语言去描述它,搞的人想请教一下百度或Google都不知道该去怎么问,简直能够令人发疯.先来看张截图吧. 有没有注意到里面的G ...
- vector 初始化所有方法
简介:vector可用于代替C中的数组,或者MFC中的CArray,从许多说明文档或者网上评论,一般一致认为应该多用vector,因为它的效率更高,而且具备很好的异常安全性.而且vector是STL推 ...
- 64 位win 7或windows 8下的visual studio不能连接Oracle数据库调试网站的问题
在64 位win 7或windows 8系统下,visual studio直接F5运行网站调试,你会发现不能连接Oracle数据库,会报一个“ORA-06413: Connection not ope ...
- 图算法(一)——基本图算法(BFS,DFS及其应用)(2)
2)DFS 深度优先搜索总是对最近发现的节点v的出发边进行搜索,直到该节点的所有出发边都被发现 一旦节点v的所有出发边都被发现,搜索回溯到v的前驱结点进行 实现细节:时间戳 每一个结点有一个发现时间和 ...
- FW:: ehcache memcache redis 三大缓存男高音
最近项目组有用到这三个缓存,去各自的官方看了下,觉得还真的各有千秋!今天特意归纳下各个缓存的优缺点,仅供参考! Ehcache 在java项目广泛的使用.它是一个开源的.设计于提高在数据从RDBMS ...
- C++ 字符串操作常见函数
//字符串拷贝,排除指定字符 char *strcpy_exclude_char(char *dst, const int dst_len, const char *src, const char * ...