一、说明

  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学习四(属性选择)

weka特征选择(IG、chi-square)的更多相关文章

  1. Chi Square Distance

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

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

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

  3. 【Machine Learning】wekaの特征选择简介

    看过这篇博客的都应该明白,特征选择代码实现应该包括3个部分: 搜索算法: 评估函数: 数据: 因此,代码的一般形式为: AttributeSelection attsel = new Attribut ...

  4. 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 ...

  5. 文本挖掘之特征选择(python 实现)

    机器学习算法的空间.时间复杂度依赖于输入数据的规模,维度规约(Dimensionality reduction)则是一种被用于降低输入数据维数的方法.维度规约可以分为两类: 特征选择(feature ...

  6. 使用Python的文本挖掘的特征选择/提取

    在文本挖掘与文本分类的有关问题中,文本最初始的数据是将文档表示成向量空间模型的一个矩阵,而这个矩阵所拥有的就是不同的词,常采用特征选择方法.原因是文本的特征一般都是单词(term),具有语义信息,使用 ...

  7. scikit-learn:在实际项目中用到过的知识点(总结)

    零.全部项目通用的: http://blog.csdn.net/mmc2015/article/details/46851245(数据集格式和预測器) http://blog.csdn.net/mmc ...

  8. NLP-特征选择

    文本分类之特征选择 1 研究背景 对于高纬度的分类问题,我们在分类之前一般会进行特征降维,特征降维的技术一般会有特征提取和特征选择.而对于文本分类问题,我们一般使用特征选择方法. 特征提取:PCA.线 ...

  9. 用R进行市场调查和消费者感知分析

    // // 问题到数据 理解问题 理解客户的问题:谁是客户(某航空公司)?交流,交流,交流! 问题要具体 某航空公司: 乘客体验如何?哪方面需要提高? 类别:比较.描述.聚类,判别还是回归 需要什么样 ...

随机推荐

  1. 解决ORA-00054资源正忙的问题

    有时候在drop表或者其他对象的时候,会遇到ORA-00054:资源正忙,要求指定NOWAIT(中文字符集)或者ORA-00054: resource busy and acquire with NO ...

  2. PHP学习(二)----数组

    数组: 首先说一下对PHP中的理解,建立一个好的理解模型还是很关键的: 1.PHP中的数组实际上可以理解为键值对,key=>value;而对于key的取值,可以是string/integer;v ...

  3. mod_php VS mod_fastcgi

    mod_php VS mod_fastcgi 目录 什么是mod_php和mod_fastcgi 1 工作原理 1 mod_php 2 mod_fastcgi 3 mod_factcgi的三种配置方式 ...

  4. 5.PHP内核探索:多进程/线程的SAPI生命周期

    多进程的SAPI生命周期 通常PHP是编译为apache的一个模块来处理PHP请求.Apache一般会采用多进程模式, Apache启动后会fork出多个子进程,每个进程的内存空间独立,每个子进程都会 ...

  5. git rm –cached filename

    为了能重新忽略那些已经被track的文件,例如停止tracking一个文件但是又不从仓库中删除它.可以使用以下命令: 代码如下 git rm –cached filename 上面这个命令用于删除单个 ...

  6. 低功耗蓝牙4.0BLE编程-nrf51822开发(5)-链路层

    链路层定义设备处于状态机中五种状态的一种: (1)旁路状态: 处于此状态下的设备不发送或接收数据,处于其它状态下都可以转到此状态. (2)广告状态: 处于此状态的设备发送广播包或者监听.响应广播包.可 ...

  7. HIVE中的HQL操作

    1.字段查询 select empno,ename from emp; 2.过滤where,limit,distinct select * from emp where sal >2500; s ...

  8. ubuntu 14 安装 JDK

    $ sudo mkdir /usr/lib/java $ sudo tar zxvf jdk-7u21-linux-i586.tar.gz -C /usr/lib/java $ cd /usr/lib ...

  9. DOM、SAX、JDOM、DOM4J四种XML解析方法PK

    基础方法(指不需要导入jar包,java自身提供的解析方式):DOM.SAXDOM:是一种平台无关的官方解析方式   --优点:          (1)形成了树结构,直观好理解,代码更易编写     ...

  10. Windows创建文件链接

    Windows平台创建文件.文件夹链接: 测试平台,windows10. D:\>mklink 创建符号链接. MKLINK [[/D] | [/H] | [/J]] Link Target / ...