学习using weka in your javacode

主要学习两个部分的代码:1、过滤数据集 2 使用J48决策树进行分类。下面的例子没有对数据集进行分割,完全使用训练集作为测试集,所以不符合数据挖掘的常识,但是下面这段代码的作用只是为了学习using weka in java

学习部分来自:http://weka.wikispaces.com/Use+WEKA+in+your+Java+code

part1

Filter

A filter has two different properties:

  • supervised or unsupervised
    either takes the class attribute into account or not
  • attribute- or instance-based
    e.g., removing a certain attribute or removing instances that meet a certain condition

Most filters implement the OptionHandler interface, which means you can set the options via a String array, rather than setting them each manually via set-methods.
For example, if you want to remove the first attribute of a dataset, you need this filter

 weka.filters.unsupervised.attribute.Remove

with this option

 -R 1

If you have an Instances object, called data, you can create and apply the filter like this:

 import weka.core.Instances;
import weka.filters.Filter;
import weka.filters.unsupervised.attribute.Remove;
...
String[] options = new String[2];
options[0] = "-R"; // "range"
options[1] = "1"; // first attribute
Remove remove = new Remove(); // new instance of filter
remove.setOptions(options); // set options
remove.setInputFormat(data); // inform filter about dataset **AFTER** setting options
Instances newData = Filter.useFilter(data, remove); // apply filter

part2

Train/test set

In case you have a dedicated test set, you can train the classifier and then evaluate it on this test set. In the following example, a J48 is instantiated, trained and then evaluated. Some statistics are printed to stdout:

 import weka.core.Instances;
import weka.classifiers.Evaluation;
import weka.classifiers.trees.J48;
...
Instances train = ... // from somewhere
Instances test = ... // from somewhere
// train classifier
Classifier cls = new J48();
cls.buildClassifier(train);
// evaluate classifier and print some statistics
Evaluation eval = new Evaluation(train);
eval.evaluateModel(cls, test);
System.out.println(eval.toSummaryString("\nResults\n======\n", false));

下面是一个使用weka进行分类的小例子,后面附上实现这段过程的java代码。

设计一个简单的,低耗的能够区分红酒和白酒的感知器(sensor)

要求:

设计的感知器必须能够至少正确的区分95%的红酒和白酒的样本数据,样本数据集大小为:6497。

数据集Download from:www.technologyforge.net/Datasets

实验步骤:

1、  数据预处理:移除属性quality。在这个试验中不需要用到酒的质量,只关注对白酒和红酒分类的准确率

选中:quality->点击remove

1、  运行默认设置的J48分类器得到一个使用所有属性值得分类结果。

从下图我们可以看到分类准确率达到99.5998%,准确率相当高

3.为了满足低耗的要求,所以我们要尽量使用最后的属性值也能达到95%的分类结果。这就需要重复试验。可以使用正反两个实验方向的方法试错,过程比较简单。

属性选择过程:可以根据图示观察不同属性对于分类结果的影响,经过比较观察可以看到下面两个属性是最能区分白酒和红酒的代表性属性。

分类性能:

使用java重复以上实验过程。

Javacode 如下

import weka.core.Instances;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.File; import javax.xml.crypto.Data; import weka.classifiers.Classifier;
import weka.classifiers.meta.FilteredClassifier;
import weka.classifiers.trees.J48;
import weka.filters.Filter;
import weka.filters.unsupervised.attribute.Remove;
import weka.core.converters.ArffLoader;
import weka.core.converters.ConverterUtils.DataSource;
import weka.classifiers.Evaluation;
public class RWClassifier { public static Instances getFileInstances(String filename) throws Exception{
FileReader frData =new FileReader(filename);
Instances data = new Instances(frData);
int length= data.numAttributes();
String[] options = new String[2];
options[0]="-R";
options[1]=Integer.toString(length);
Remove remove =new Remove();
remove.setOptions(options);
remove.setInputFormat(data);
Instances newData= Filter.useFilter(data, remove);
return newData;
} public static void main(String[] args) throws Exception {
Instances instances = getFileInstances("D://Weka_tutorial//WineQuality//RedWhiteWine.arff");//存储数据的位置
// System.out.println(instances);
instances.setClassIndex(instances.numAttributes()-1); J48 j48= new J48();
j48.buildClassifier(instances); Evaluation eval = new Evaluation(instances);
eval.evaluateModel(j48, instances);
System.out.println(eval.toSummaryString("\nResults\n====\n", false)); } }

使用完整属性的分类结果(可以对比weka的运行结果,完全一致):

Results

====

Correctly Classified Instances        6471               99.5998 %

Incorrectly Classified Instances        26                0.4002 %

Kappa statistic                          0.9892

Mean absolute error                      0.0076

Root mean squared error                  0.0617

Relative absolute error                  2.0491 %

Root relative squared error             14.3154 %

Total Number of Instances             6497

设计一个简单的,低耗的能够区分红酒和白酒的感知器(sensor)的更多相关文章

  1. 180626-Spring之借助Redis设计一个简单访问计数器

    文章链接:https://liuyueyi.github.io/hexblog/2018/06/26/180626-Spring之借助Redis设计一个简单访问计数器/ Spring之借助Redis设 ...

  2. Tomcat详解系列(1) - 如何设计一个简单的web容器

    Tomcat - 如何设计一个简单的web容器 在学习Tomcat前,很多人先入为主的对它的认知是巨复杂的:所以第一步,在学习它之前,要打破这种观念,我们通过学习如何设计一个最基本的web容器来看它需 ...

  3. 【python免费代码】设计一个简单的学生信息管理系统

    文章目录 前言 一.理解 二.部分截图展示 三.代码 四.总结 前言 设计一个简单的学生信息管理系统,实现以下功能(bug) : 录入学生信息,信息以文件方式存储 以学生学号或者学生姓名为条件查询该学 ...

  4. 5、使用Libgdx设计一个简单的游戏------雨滴

    (原文:http://www.libgdx.cn/topic/49/5-%E4%BD%BF%E7%94%A8libgdx%E8%AE%BE%E8%AE%A1%E4%B8%80%E4%B8%AA%E7% ...

  5. JAVA课程设计——一个简单的教务人事管理系统

    大三上学期期末总结,没错,上学期,写在下学期新学期开始,哈哈哈. 上学期学习了面向对象程序设计,课程设计的题目使用JAVA语言完成一个简单的教务人事管理系统,能够实现访问数据库的登录验证,分别按部门和 ...

  6. 设计一个简单的devops系统

    前言 公司设计的RDMS挺好用的,我也照猫画虎简单的设计一个DevOps系统,与大家分享,不足之处欢迎拍砖,以免误人子弟 前置条件 gitlab gitlab-runner k8s 1. gitlab ...

  7. 如何设计一个简单的C++ ORM

    2016/11/15 "没有好的接口,用C++读写数据库和写图形界面一样痛苦" 阅读这篇文章前,你最好知道什么是 Object Relation Mapping (ORM) 阅读这 ...

  8. 快速设计一个简单的WPF串口上位机

    最近一直在学习UWP,其中有的技术参考了WPF,所以又回头再来学习WPF,感觉学的东西很杂,必须记录一下,不然时间长了还得忘掉,于是申请开始写博客,将学习的心得记录一下,以备后用.这次是因为公司内训, ...

  9. 使用redis设计一个简单的分布式锁

    最近看了有关redis的一些东西,了解了redis的一下命令,就记录一下: redis中的setnx命令: 关于redis的操作命令,我们一般会使用set,get等一系列操作,数据结构也有很多,这里我 ...

随机推荐

  1. centos7 下载eclipse的镜像站点

    这里吐槽一下,由于两天前centos被我农崩溃了(系统更新的锅),所以所有的开发环境又得重来一次. 其实,之前去eclipse的官网下载就很慢,打开官网也很慢,然后你会发现下下来的安装程序(只有40多 ...

  2. spring4+hibernate3

    环境说明:spring4.0+hibernate3 数据库:oracle 连接池:c3p0 项目结构: lib中的jar: 一.配置spring.xml 说明:这里采用的配置模式将hibernateT ...

  3. HTML5 中的块级链接

    英文叫做 “Block-level” links,我以为只有我厂那些鸟毛不知道,没想到不知道的还挺多, 需要普及一下. 最近看了 kejun 的 PPT 前端开发理论热点面对面:从怎么看,到怎么做?, ...

  4. BZOJ 3160 万径人踪灭 解题报告

    这个题感觉很神呀.将 FFT 和 Manacher 有机结合在了一起. 首先我们不管那个 “不能连续” 的条件,那么我们就可以求出有多少对字母关于某一条直线对称,然后记 $T_i$ 为关于直线 $i$ ...

  5. The 5th Zhejiang Provincial Collegiate Programming Contest------ProblemK:Kinds of Fuwas

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1974 题意:问四个角都有同一个福娃的矩形有多少个. #include<b ...

  6. Kafka操作

    http://blog.csdn.net/xiao_jun_0820/article/details/46831203 http://blog.csdn.net/xcockroach/article/ ...

  7. [itint5]环形最大连续子段和

    http://www.itint5.com/oj/#9 一开始有了个n*n的算法,就是把原来的数组*2,由环形的展开成数组.然后调用n次最大子段和的方法.超时. 后来看到个O(n)的算法,就是如果不跨 ...

  8. Android如何在ListView中嵌套ListView

    前几天因为项目的需要,要在一个ListView中放入另一个ListView,也即在一个ListView的每个ListItem中放入另外一个ListView.但刚开始的时候,会发现放入的小ListVie ...

  9. 微信支付开发1 微信支付URL配置

    本文介绍微信支付申请时如何设置授权目录及URL. 一.选择支付类型 目前有两种支付类型 JS API网页支付 Native原生支付 如果没有特殊要求,两种都勾选. 二.支付授权目录 目前可以选择htt ...

  10. [转] Android自动化测试之MonkeyRunner录制和回放脚本(四)

    测试脚本录制: 方案一: 我们先看看以下monkeyrecoder.py脚本: #Usage: monkeyrunner recorder.py #recorder.py  http://mirror ...