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进行市场调查和消费者感知分析
// // 问题到数据 理解问题 理解客户的问题:谁是客户(某航空公司)?交流,交流,交流! 问题要具体 某航空公司: 乘客体验如何?哪方面需要提高? 类别:比较.描述.聚类,判别还是回归 需要什么样 ...
随机推荐
- register instruction pointer
Computer Science An Overview _J. Glenn Brookshear _11th Edition We have already encountered the conc ...
- 不绑架输入--document.getElementById("linkage_"+id_type+"_echo").value="";--联动
<script> function w_linkage(id_type) { var selected = $("#linkage_"+id_type+"_t ...
- prototype linkage can reduce object initialization time and memory consumption
//对象是可变的键控集合, //"numbers, strings, booleans (true and false), null, and undefined" 不是对象的解释 ...
- P1010 幂次方
这么难得题,居然普及-?做了好久 #include <bits/stdc++.h> using namespace std; int fact[21]; void solve(int n) ...
- OAuth的机制原理讲解及开发流程
本想前段时间就把自己通过QQ OAuth1.0.OAuth2.0协议进行验证而实现QQ登录的心得及Demo实例分享给大家,可一直很忙,今天抽点时间说下OAuth1.0协议原理,及讲解下QQ对于Oaut ...
- javascript的alert()的消息框不弹出或者弹出信息有误
有时不知道什么,有时javascript的alert()的消息框不弹出或者弹出信息有误,代码是这么写的: //提示信息 public static void alert(TemplateControl ...
- FastReport的交叉表实际使用的一个例子
计算发行-->定义份数月表(打开)出现 PosFraisPaysInput选择时间段后,点击“打印”.这个设计表格,就是交叉表. 交叉表的特点是:数据库是一条一条并列的但是出来的结果却是:横向是 ...
- android动态调试samli代码(转)
转载自看雪http://bbs.pediy.com/showthread.php?t=189610,非常感谢原作者分享! 跟踪apk一般的做法是在反编译的smali代码中插入log输出,然后重新编译运 ...
- Maven实战(三)Eclipse构建Maven项目
1. 安装m2eclipse插件 要用Eclipse构建Maven项目,我们需要先安装meeclipse插件 点击eclipse菜单栏Help->Eclipse Marketplac ...
- C# HttpWebRequest 绝技
http://www.sufeinet.com/thread-6-1-1.html 万能框架.分布式......