Weka 二次开发使用心得

一、weka数据挖掘流程


使用weka图形界面,初步尝试了下数据的预处理、分类、关联等操作,因为weka本身就是一个开源的机器学习库,于是想自己尝试下利用weka的api进行相关的学习。

在Eclipse中新建一个工程,导入weka.jar,就可以开始编写代码了,具体的配置很简单,不清楚的话网上有很多的参考教程,这里只是记录一些学习中大致的过程。

weka作为开源的数据挖掘平台,封装了很多优秀的机器学习算法,它进行数据挖掘的过程一般如下:

  1. 读入训练、测试样本
  2. 初始化分类器
  3. 使用训练样本训练分类器
  4. 使用测试样本测试分类器的学习效果
  5. 打印分类结果

下面是示例代码,其中引入的jar包没有给出,届时可以在Eclipse中使用快捷键ctrl+shift+o 来自动导入所需要的包。

public class WekaTest {
public static void main(String[] args) throws Exception {
//读取训练数据
Instances ins=null;
Classifier cfs=null;
File file=new File("data/iris.arff");
ArffLoader loader=new ArffLoader();
loader.setFile(file);
ins=loader.getDataSet();
ins.setClassIndex(ins.numAttributes()-1);
//初始化分类器
cfs = (Classifier)Class.forName("weka.classifiers.bayes.NaiveBayes").newInstance();
//使用训练样本进行分类
cfs.buildClassifier(ins);
//使用测试样本测试分类器的学习效果 ,这里使用的还是原来的数据集,只是为了方便
//具体操作过程中需要导入新的测试数据
Instance testInst;
Evaluation testingEvaluation = new Evaluation(ins);
int length = ins.numInstances();
for(int i = 0; i < length ; i++){
testInst = ins.instance(i);
testingEvaluation.evaluateModelOnceAndRecordPrediction(cfs, testInst);
}
//打印分类结果
System.out.println("right classifier=="+(1-testingEvaluation.errorRate()));
}
}

大体的学习和评测过程就是这样,然后可能在不同的应用中会选择不同的算法或者其他参数等。这个还在进一步摸索之中。

备注:

使用weka进行模型的训练过程中,如果没有测试集,可以采用k-fold交叉验证的方式。

        //why not like this?
testingEvaluation.evaluateModel(cfs, ins);
System.out.println(1-testingEvaluation.errorRate()); // k-fold cross evaluation
Evaluation tencrosseva=new Evaluation(ins);
tencrosseva.crossValidateModel(cfs, ins, 14, new Random(1));
System.out.println(1-tencrosseva.errorRate()); //save the model
SerializationHelper.write("data/knn.model", cfs); //load the model
Classifier clf_name = (Classifier) SerializationHelper.read("data/knn.model");

常用分类器介绍,有些名字笔记晦涩。

  • bayes下的Naïve Bayes(朴素贝叶斯)和BayesNet(贝叶斯信念网络)。
  • functions下的LibLinear、LibSVM(这两个需要安装扩展包)、Logistic Regression、Linear Regression
  • lazy下的IB1(1-NN)和IBK(KNN)。
  • meta下的很多boosting和bagging分类器,比如AdaBoostM1。
  • trees下的J48(weka版的C4.5)、RandomForest。

二、weka 属性选择


在数据挖掘的研究中,通常要通过距离来计算样本之间的距离,而样本距离是通过属性值来计算的。我们知道对于不同的属性,它们在样本空间的权重是不一样的,即它们与类别的关联度是不同的,因此有必要筛选一些属性或者对各个属性赋一定的权重。这样属性选择的方法就应运而生了。 ——weka属性选择

这里我使用的是kdd99 进行网络入侵检测的10%数据集合(大概4w多条记录),每条记录包含41个特征属性以及一个类标签。使用weka训练这么点数据的时候显得还是有点吃力,因为有些属性是相关而且相对冗余,有必要对其进行属性的选择,可以理解成主成分分析PCA不?有些概念还是比较模糊,一定要理解清楚。

导入kdd99数据集

默认安装的堆内存只有1024m ,在运行大的数据集的时候可能会出现堆溢出的错误。

有两种方法可以改变堆内存的大小

  • 在控制台运行java -Xmx1500m -jar weka.jar启动weka。
  • 或者修改安装目录下的runweka.ini配置文件。
# placeholders ("#bla#" in command gets replaced with content of key "bla")
# Note: "#wekajar#" gets replaced by the launcher class, since that jar gets
# provided as parameter
maxheap=1024M
# The MDI GUI
#mainclass=weka.gui.Main

原先的逗号分隔的文本文件(csv),导入weka中然后可以另存为arff文件,可以很清晰明了的看到哪些是连续型变量、哪些是离散变量。

kdd99 数据概览

@relation attr

@attribute duration numeric
@attribute protocol_type {tcp,udp,icmp}
@attribute service {http,smtp,finger,domain_u,auth,telnet,ftp,eco_i,ntp_u,ecr_i,other,private,pop_3,ftp_data,rje,time,mtp,link,remote_job,gopher,ssh,name,whois,domain,login,imap4,daytime,ctf,nntp,shell,IRC,nnsp,http_443,exec,printer,efs,courier,uucp,klogin,kshell,echo,discard,systat,supdup,iso_tsap,hostnames,csnet_ns,pop_2,sunrpc,uucp_path,netbios_ns,netbios_ssn,netbios_dgm,sql_net,vmnet,bgp,Z39_50,ldap,netstat,urh_i,X11,urp_i,pm_dump,tftp_u,tim_i,red_i}
@attribute flag {SF,S1,REJ,S2,S0,S3,RSTO,RSTR,RSTOS0,OTH,SH}
@attribute src_bytes numeric
@attribute dst_bytes numeric
@attribute land numeric
@attribute wrong_fragment numeric
@attribute urgent numeric
...
...
@attribute dst_host_srv_serror_rate numeric
@attribute dst_host_rerror_rate numeric
@attribute dst_host_srv_rerror_rate numeric
@attribute lable {normal.,buffer_overflow.,loadmodule.,perl.,neptune.,smurf.,guess_passwd.,pod.,teardrop.,portsweep.,ipsweep.,land.,ftp_write.,back.,imap.,satan.,phf.,nmap.,multihop.,warezmaster.,warezclient.,spy.,rootkit.} @data
0,tcp,http,SF,181,5450,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,8,8,0,0,0,0,1,0,0,9,9,1,0,0.11,0,0,0,0,0,normal.
0,tcp,http,SF,239,486,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,8,8,0,0,0,0,1,0,0,19,19,1,0,0.05,0,0,0,0,0,normal.
...
...

进行预处理

有些方法的使用对数据集的类型有要求,比如关联方法的话就要求是离散型的,如果有数值型的数据的话,那么就要对这些个属性进行离散化操作,同样的道理,有时候需要对数据进行规范化、正则化等操作,目的为了就是能够使用特定的算法,或者说是提高精度与训练速度等。。。

属性选择操作

对这个数据集使用信息增益算法进行属性选择的时候内存溢出,故重新抽样选择了原数据的一半进行选择,采用10-fold交叉验证的选择方式进行。

在右侧的列表中可以看到属性排名,信息量越大的越能很好的区分分类类别,故用来做分类属性的话更具有价值。

示例代码

public class WekaASE {

	public static void main(String[] args) throws Exception {
// 1. 读取训练数据
Instances ins = null;
Classifier cfs = null;
File file = new File("data/kdd99.arff");
ArffLoader loader = new ArffLoader();
loader.setFile(file);
ins = loader.getDataSet();
ins.setClassIndex(ins.numAttributes() - 1);
//初始化搜索算法(search method)及属性评测算法(attribute evaluator)
Ranker rank = new Ranker();
InfoGainAttributeEval eval = new InfoGainAttributeEval();
// 3.根据评测算法评测各个属性
eval.buildEvaluator(ins);
// 4.按照特定搜索算法对属性进行筛选
//在这里使用的Ranker算法仅仅是属性按照InfoGain的大小进行排序
int[] attrIndex = rank.search(eval, ins);
//5.打印结果信息 在这里我们了属性的排序结果同时将每个属性的InfoGain信息打印出来
StringBuffer attrIndexInfo = new StringBuffer();
StringBuffer attrInfoGainInfo = new StringBuffer();
attrIndexInfo.append("Selected attributes:");
attrInfoGainInfo.append("Ranked attributes:\n");
for (int i = 0; i < attrIndex.length; i++) {
attrIndexInfo.append(attrIndex[i]);
attrIndexInfo.append(",");
attrInfoGainInfo.append(eval.evaluateAttribute(attrIndex[i]));
attrInfoGainInfo.append("\t");
attrInfoGainInfo.append((ins.attribute(attrIndex[i]).name()));
attrInfoGainInfo.append("\n");
}
System.out.println(attrIndexInfo.toString());
System.out.println(attrInfoGainInfo.toString()); }
}

三、关联分析

四、分类探究

五、聚类分析

六、验证&评估

七、特征工程


华丽的分割线~~~

参考资料

Weka 二次开发使用心得的更多相关文章

  1. Java开发学习心得(二):Mybatis和Url路由

    目录 Java开发学习心得(二):Mybatis和Url路由 1.3 Mybatis 2 URL路由 2.1 @RequestMapping 2.2 @PathVariable 2.3 不同的请求类型 ...

  2. 搭建rtmp直播流服务之4:videojs和ckPlayer开源播放器二次开发(播放rtmp、hls直播流及普通视频)

    前面几章讲解了使用 nginx-rtmp搭建直播流媒体服务器; ffmpeg推流到nginx-rtmp服务器; java通过命令行调用ffmpeg实现推流服务; 从数据源获取,到使用ffmpeg推流, ...

  3. IBOS云办公系统二次开发之功能介绍(PHP技术)

    IBOS自动化办公系统是我见到的功能.架构最好的开源自动化办公系统,功能与企业需求吻合度之高.架构之灵活,让我不得不将之介绍给大家,让跟多需要学习PHP开发的朋友来了解她,拥抱她! 如果您还没有很好的 ...

  4. 0421--"数字口袋精灵app"二次开发(Blackbriar团队开发)

    "数字口袋精灵app"二次开发 目录: 一.项目github总仓库推送 二.开发成员 三.分工与合作 四.各模块成果 五.心得墙 六.团队成员贡献分 内容: 一.项目github总 ...

  5. Navisworks API 简单二次开发 (自定义工具条)

    在Navisworks软件运行的时候界面右侧有个工具条.比较方便.但是在二次开发的时候我不知道在Api那里调用.如果有网友知道请告诉我.谢谢. 我用就自己设置一个工具.界面比较丑!没有美工. 代码: ...

  6. [连载]《C#通讯(串口和网络)框架的设计与实现》- 12.二次开发及应用

    目       录 第十二章     二次开发及应用... 2 12.1        项目配制... 3 12.2        引用相关组件... 4 12.3        构建主程序... 5 ...

  7. OBS-Studio二次开发记录

    OBS-Studio 是一款跨平台的,开源的视频直播客户端软件. 公司需要对他进行二次开发,开发的目的是使用它的录屏功能. 开发的要求是:定制全新的界面,所见即所得,window系统兼容要好. 开发步 ...

  8. 小猪cms微信二次开发之怎样分页

    $db=D('Classify'); $zid=$db->where(array('id'=>$this->_GET('fid'),'token'=>$this->tok ...

  9. 承接 AutoCAD 二次开发 项目

    本人有多年的CAD开发经验,独立完成多个CAD二次开发项目.熟悉.net及Asp.net开发技术,和Lisp开发技术. 现在成立了工作室,独立承接CAD二次开发项目.结项后提供源码及开发文档,有需要的 ...

随机推荐

  1. bat实现监测计算机无线连接,断网自动重启无线

    @echo off :Begin ping www.baidu.com if errorlevel 1 goto Reboot if errorlevel 0 goto Continue :Conti ...

  2. JSONCPP开发环境搭建

    环境设置 项目地址 https://github.com/open-source-parsers/jsoncpp.git 操作系统 64位 Fedora 24 安装jsoncpp $ git clon ...

  3. PHP连接 redis

    <?php //连接本地的 Redis 服务 $redis = new Redis(); //连接redis 地址 端口 连接超时时间 连接成功返回true 失败返回false $redis-& ...

  4. 洛谷P1083 借教室

    P1083 借教室 题目描述 在大学期间,经常需要租借教室.大到院系举办活动,小到学习小组自习讨论,都需要向学校申请借教室.教室的大小功能不同,借教室人的身份不同,借教室的手续也不一样. 面对海量租借 ...

  5. 避免picture图片无法删除,提示正在被其他进程使用

    pictureBox1.Image = Image.FromStream(ByteToStream(SetImageToByteArray(cutImgPath))); #region 将文件转换成流 ...

  6. POJ3274-Gold Balanced Lineup

    题目链接:点击打开链接 Gold Balanced Lineup Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 16978 ...

  7. oracle基本命令

    1.首先,创建(新)用户: create user username identified by password; username:新用户名的用户名 password: 新用户的密码也可以不创建新 ...

  8. Git 通过https向码云推送项目

  9. Python网络爬虫(一)

    Urllib发送请求 基本用法 基本的用法就是调用request库, class urllib.request.Request(url, data=None, headers={}, origin_r ...

  10. linux下find查找与批量替换文件中指定内容

    经常在部署tomcat时需要替换配置文件中的ip,find命令批量替换还是很方便的 查找需要替换的ip,看看哪些文件有配置这个ip,执行下面命令: find ./ -type f -regex &qu ...