用Java创建weka需要的Instance对象和arff文件
来源:互联网 发布:越贵妃知乎 编辑:程序博客网 时间:2024/05/29 15:40
在用Java使用weka时,我们需要weka支持的数据格式,weka官网上给出的大多是从arff文件中导入数据。从数据库中生成对系统环境要求也较高。因此本文介绍了如何通过普通的Java对象生成weka支持的Instance对象,并保存到arff文件中。
总体思路
首先创建一个Instances对象,为Instances对象设置属性(@Attribute),然后从对象创建Instance加入到Instances中,如果需要保存,可以直接将Instances对象保存为arff格式的文件。
Attribute
在介绍attribute之前首先简述一下arff的文件格式。官网上示例如下:
% 1. Title: Iris Plants Database % % 2. Sources: % (a) Creator: R.A. Fisher % (b) Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov) % (c) Date: July, 1988 % @RELATION iris @ATTRIBUTE sepallength NUMERIC @ATTRIBUTE sepalwidth NUMERIC @ATTRIBUTE petallength NUMERIC @ATTRIBUTE petalwidth NUMERIC @ATTRIBUTE class {Iris-setosa,Iris-versicolor,Iris-virginica} @DATA 5.1,3.5,1.4,0.2,Iris-setosa 4.9,3.0,1.4,0.2,Iris-setosa 4.7,3.2,1.3,0.2,Iris-setosa 4.6,3.1,1.5,0.2,Iris-setosa 5.0,3.6,1.4,0.2,Iris-setosa 5.4,3.9,1.7,0.4,Iris-setosa 4.6,3.4,1.4,0.3,Iris-setosa 5.0,3.4,1.5,0.2,Iris-setosa 4.4,2.9,1.4,0.2,Iris-setosa 4.9,3.1,1.5,0.1,Iris-setosa
因为arff文件和Instances是对应的,在创建Instances对象之前我们需要指定Instances的属性,对应到Java中,@ATTRIBUTE对应的是weka.core.Attribute对象。
Instance
Instance是一条数据,类似于数据库中的一行。在新版的weka(>3.5)中,Instance只是一个接口,在本文中以DenseInstance为例创建Instance,其他Instance可以在weka官方api中查到。
创建Instances
private Instances generatePopularInstance(List<SecRepoEntity> entities) { //set attributes ArrayList<Attribute> attributes = new ArrayList<>(); attributes.add(new Attribute("fork")); attributes.add(new Attribute("size")); attributes.add(new Attribute("sum")); attributes.add(new Attribute("avg")); attributes.add(new Attribute("weight")); //set instances Instances instances = new Instances("repo_popular",attributes,0); instances.setClassIndex(instances.numAttributes() - 1); //add instance for (SecRepoEntity secRepoEntity: entities) { Instance instance = new DenseInstance(attributes.size()); instance.setValue(0,secRepoEntity.getForkCount()); instance.setValue(1,secRepoEntity.getSize()); instance.setValue(2,secRepoEntity.getSumFollower()); instance.setValue(3,secRepoEntity.getAvgFollower()); instance.setValue(4,secRepoEntity.getWeightFollower()); instances.add(instance); } return instances; }
保存Instances对象为arff文件
/** * generate weka dataSource file * @param instances weka Instances */ public void generateArffFile(Instances instances, String path) { ArffSaver saver = new ArffSaver(); saver.setInstances(instances); try { saver.setFile(new File(path)); saver.writeBatch(); } catch (IOException e) { e.printStackTrace(); } }
从arff文件导入Instances
weka官网有详细的说明,这里摘录新版的方法。(使用DataSource)
import weka.core.converters.ConverterUtils.DataSource; ... DataSource source = new DataSource("/some/where/data.arff"); Instances data = source.getDataSet(); // setting class attribute if the data format does not provide this information // For example, the XRFF format saves the class attribute information as well if (data.classIndex() == -1) data.setClassIndex(data.numAttributes() - 1);
5 0
- 用Java创建weka需要的Instance对象和arff文件
- Java调用Weka API创建Arff文件
- 在Java中使用weka:创建一个ARFF文件
- weka关联的ARFF文件
- 在Eclipse(JAVA)中加载WEKA的ARFF文件
- Weka学习之用java构建ARFF文件
- weka中的arff文件
- Weka中的ARFF文件
- 在java中调用weka聚类方法并显示相应的类标签+weka中创建arff数据
- java调用weka,读取csv及arff文件,将csv转化为arff文件
- Weka学习之ARFF文件
- weka支持的arff格式
- java代码实现将时间序列数据集(UCR)转化为weka能识别的.arff文件
- Weka项目中的ARFF文件结构解析
- Weka项目中的ARFF文件结构解析
- Weka项目中的ARFF文件结构解析
- weka中将Instances另存为一个arff文件
- 在Java中使用weka:将实例转化为ARFF文件
- Java-NowCoder-求int型正整数在内存中存储时1的个数
- MySql Server5.7社区版的安装详情-配图【陈科肇】
- Write a function that takes a string as input and returns the string reversed.
- 【opencv练习35 - 轮廓旋转边界框 + 椭圆】
- ASoC Platform驱动代码框架图
- 用Java创建weka需要的Instance对象和arff文件
- VS2013 错误对照表(持续更新)
- Discriminative Action States Discovery for Online Action Recognition (动作识别 3)
- FFmpeg的解复用器
- libsvm的使用方法及调参
- Hive2.1.0安装配置
- gerrit安装指南(ubuntu环境)
- 程序化广告交易中的点击率预估 CRT
- libsvm参数的意义