.mat,.txt,.csv 数据转换为weka中的arff格式及matlab和Weka之间相互转换格式

在RUSBoost和SMOTEBoost中提供了csv转换为arff格式的方法,详见CSVtoARFF.m
http://www.mathworks.com/matlabcentral/fileexchange/37315-rusboost
http://cn.mathworks.com/matlabcentral/fileexchange/37311-smoteboost

function r = CSVtoARFF (data, relation, type)
% csv to arff file converter% load the csv data
[rows cols] = size(data);% open the arff file for writing
farff = fopen(strcat(type,'.arff'), 'w');% print the relation part of the header
fprintf(farff, '@relation %s', relation);% Reading from the ARFF header
fid = fopen('ARFFheader.txt','r');
tline = fgets(fid);
while ischar(tline)tline = fgets(fid);fprintf(farff,'%s',tline);
end
fclose(fid);% Converting the data
for i = 1 : rows% print the attribute values for the data pointfor j = 1 : cols - 1if data(i,j) ~= -1 % check if it is a missing valuefprintf(farff, '%d,', data(i,j));elsefprintf(farff, '?,');endend% print the label for the data pointfprintf(farff, '%d\n', data(i,end));
end% close the file
fclose(farff);r = 0;

该方法的不足之处就是要单独提供ARFFheader.txt ,很多情况下,该表头需要人工添加(属性少时),但当属性大时,相对较麻烦,还是可以通过程序循环添加。

下面给出一个可以直接将.mat,.txt和.csv格式转换为weka中的arff格式
http://www.aiseminar.com/bbs/forum.php?mod=viewthread&tid=1058

function Mat2Arff('input_filename','arff_filename')
%
% This function is used to convert the input data to '.arff'
% file format,which is compatible to weka file format ...
%
% Parameters:
% input_filename -- Input file name,only can conversion '.mat','.txt'
% or '.csv' file format ...
% arff_filename -- the output '.arff' file ...% NOTEs:
%The input 'M*N' file data must be the following format:
% M: sampel numbers;
% N: sample features and label,"1:N-1" -- features, "N" - sample label ...% 读取文件数据 ...
if strfind(input_filename,'.mat')
matdata = importdata(input_filename);
elseif strfind(input_filename,'.txt')
matdata = textread(input_filename) ;
elseif strfind(input_filename,'.csv')
matdata = csvread(input_filename);
end;[row,col] = size(matdata);
f = fopen(arff_filename,'wt');
if (f < 0)
error(sprintf('Unable to open the file %s',arff_filename));
return;
end;
fprintf(f,'%s\n',['@relation ',arff_filename]);
for i = 1 : col - 1
st = ['@attribute att_',num2str(i),' numeric'];
fprintf(f,'%s\n',st);
end;
% 保存文件头最后一行类别信息
floatformat = '%.16g';
Y = matdata(:,col);
uY = unique(Y); % 得到label类型
st = ['@attribute label {'];
for j = 1 : size(uY) - 1
st = [st sprintf([floatformat ' ,'],uY(j))];
end;
st = [st sprintf([floatformat '}'],uY(length(uY)))];
fprintf(f,'%s\n\n',st);
% 开始保存数据 ...
labelformat = [floatformat ' '];
fprintf(f,'@data\n');
for i = 1 : row
Xi = matdata(i,1:col-1);
s = sprintf(labelformat,Y(i));
s = [sprintf([floatformat ' '],[; Xi]) s];
fprintf(f,'%s\n',s);
end;
fclose(f);

最后给出关于weka数据处理的简明介绍。
数据挖掘简述和weka介绍–数据挖掘学习和weka使用(一)
输入数据与ARFF文件–数据挖掘学习和weka使用(二)
简单总结一下:
weka中的arff格式数据是由两部分组成:头部定义和数据区。
头部定义包含了关系名称(relation name)、一些属性(attributes)和对应的类型,如

   @RELATION iris@ATTRIBUTE sepallength  NUMERIC @ATTRIBUTE sepalwidth   NUMERIC @ATTRIBUTE petallength  NUMERIC @ATTRIBUTE petalwidth   NUMERIC @ATTRIBUTE class        {Iris-setosa,Iris-versicolor,Iris-virginica}

NUMERIC说明其为数字型,属性class的取值是限定的,只能是Iris-setosa,Iris-versicolor,Iris-virginica中的一个。数据类型还可以是string和data数据区有@data开头,如:

@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文件如下:

@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

更多细节可查看
http://weka.wikispaces.com/ARFF+%28stable+version%29#Sparse%20ARFF%20files

weka使用自己的文件格式,叫做ARFF,如果想从*matlab和Weka之间相互转换,这里有现成的package*:

http://www.mathworks.com/matlabcentral/fileexchange/21204-matlab-weka-interface

不要以为下载下来就能用,你会在如下地方报错:

if(~wekaPathCheck),wekaOBJ = []; return,endimport weka.core.converters.ArffLoader;import java.io.File;

Tricky的事情就是得把weka.jar加入到matlab的classpath.txt列表。classpath.txt在哪儿?到matlab的command窗口敲:

which classpath.txt
D:\CMWang\MATLABR2014b\toolbox\local\classpath.txt

然后就是到classpath.txt里加入一行,weka.jar的绝对路径,例如:

C:\Program Files\Weka-3-8 \weka.jar

这样就配置完毕了。
该部分参考 http://blog.sciencenet.cn/blog-248606-433590.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/247059.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

IT人不仅要提升挣钱能力,更要拓展挣钱途径

前几天我上班路上&#xff0c;和小区门口开车的师傅闲聊&#xff0c;发现他们虽然学历不高&#xff0c;但挣钱的途径不少&#xff0c;比如固定接送多位客户&#xff0c;然后能通过朋友圈拓展新客户&#xff0c;而且通过客户口口相传&#xff0c;也能不断拉到生意&#xff0c;算…

Class Imbalance Problem

本文转自&#xff1a;http://www.chioka.in/class-imbalance-problem/#comment-202282 What is the Class Imbalance Problem? It is the problem in machine learning where the total number of a class of data (positive) is far less than the total number of another c…

matlab中的类标转换程序

matlab中的类标转换程序 原始类标为Y&#xff0c;新类标为Y_new %进行排序&#xff0c;针对类标数目orig_labels sort(unique(Y)); Y_new Y;new_labels 1:length(orig_labels);for i1:length(orig_labels)Y_new(find(Yorig_labels(i)))Inf;Y_new(isinf(Y_new))new_labels(…

this关键字+super关键字

一.this关键字1.实例一&#xff1a;&#xff08;1&#xff09;需求&#xff1a;使用Java类描述一个动物&#xff1b;&#xff08;2&#xff09;实例&#xff1a;class Animal{ String name; //成员变量 String color; public Animal(String n,String c){ na…

python中的print

python3 中去除了print语句&#xff0c;加入print()函数实现相同的功能 print() 会在输出窗口中显示一些文本。 >>> print "hello,world!" SyntaxError: Missing parentheses in call to print >>> print("hello,world!") hello,world…

final+static

final final关键字顾名思义代表“最后的”&#xff0c;意味着不能被更改。它的定义&#xff0c;可以概括地分为以下三点&#xff1a; 被final修饰的类不能被继承&#xff1b;被final修饰的方法不能被重写&#xff1b;被final修饰的变量不能被改变。注&#xff1a;引用类型的变量…

程序代码编辑器和浏览器代码编辑器&代码可视化执行过程

tutorialspoint http://www.tutorialspoint.com/codingground.htm 1. Sublime Text &#xff1a;http://blog.l1n3.net/editor/sublime-text-introduce/ 下载 &#xff1a;http://www.sublimetext.com/3 2. Notepad https://notepad-plus-plus.org/zh/ 更多细节请查看 htt…

匿名对象+内部类

匿名对象 普通的类对象在使用时会定义一个类类型的变量&#xff0c;用来保存new出来的类所在的地址。而匿名类取消掉了这个变量&#xff0c;这个地址由编译器来处理&#xff0c;并且在new出来之后&#xff0c;它占用的内存会有JVM自动回收掉。后续无法再使用了。例如 public cl…

听技术播客:一边学Python编程一边学英语

本文转自&#xff1a;http://codingpy.com/article/recommended-python-podcasts/ 学技术的朋友一般都会关注不少技术博客&#xff08;blog&#xff09;&#xff0c;但是关注技术播客&#xff08;podcast&#xff09;的人估计不会太多。这里一方面也是由于相关的播客数量&#…

mysql补充

mysql补充 mysql使用流程 开启服务端&#xff0c;mysqld或者net start mysqlcmd下键入mysql -u root -p&#xff0c;输入设置好的密码&#xff0c;连接mysql客户端show databases&#xff1b;展示所有的mysql仓库创建一个库&#xff1a;create database CRM&#xff1b;然后sho…

编程书单:十本Python编程语言的入门书籍

本文转自&#xff1a;http://codingpy.com/article/10-python-beginner-books/ 本文与大家分享一些Python编程语言的入门书籍&#xff0c;其中不乏经典。我在这里分享的&#xff0c;大部分是这些书的英文版&#xff0c;如果有中文版的我也加上了。有关书籍的介绍&#xff0c;大…

JavaScript异步

JavaScript异步类型 延迟类型&#xff1a;setTimeout、setInterval、setImmediate监听事件&#xff1a;监听new Image加载状态、监听script加载状态、监听iframe加载状态、Message带有异步功能类型&#xff1a; Promise、ajax、Worker、async/awaitJavaScript常用异步编程 Prom…

Sublime配置与各种插件

本文转自&#xff1a;http://www.cnblogs.com/yyhh/p/4232063.html Sublime Text 3 安装Package Control 点击View -> Show Console 在下方命令行内&#xff0c;输入以下命令。 import urllib.request,os;pfPackage Control.sublime-package;ippsublime.installed_packages_…

把Sublime Text 2打造成一个轻量级Python的IDE

本文转自&#xff1a;http://blog.l1n3.net/python/sublime-text-to-python-ide/ 因为这段时间迷上了Python&#xff0c;所以想吧Sublime Text 2弄成一个Python的简易IDE&#xff0c;Python自带的IDLE简直太难用&#xff01;&#xff01;&#xff01;&#xff01; 配置Python环…

数据库表操作、数据类型及完整性约束

数据库表操作、数据类型及完整性约束 库操作补充 数据库命名规则&#xff1a; 可以由字母、数字、下划线、&#xff20;、&#xff03;、&#xff04;区分大小写唯一性不能使用关键字如 create select不能单独使用数字最长128位表操作补充 #语法&#xff1a; create table 表名…

算法第一次作业

1.代码规范&#xff08;由于日后可能会用C和Java&#xff0c;就找了两种&#xff09; Google C代码规范&#xff1a;https://blog.csdn.net/freeking101/article/details/78930381 Ggoogle Jave代码规范&#xff1a;https://www.jianshu.com/p/4e50269037ed 2.《数学之美》读后…

Sublime Text官方文档 中英文版本

英文版本&#xff1a;http://docs.sublimetext.info/en/latest/index.html 中文翻译版本&#xff1a;http://sublime-text.readthedocs.org/en/latest/reference/build_systems.html

第99:真正理解拉格朗日乘子法和 KKT 条件

转载于:https://www.cnblogs.com/invisible2/p/11441485.html

有了CodinGame,玩着游戏就能学编程

本文转自&#xff1a;http://www.codingpy.com/article/learning-to-code-becomes-a-game/ 今天编程派向大家推荐一个有趣的编程练习平台&#xff0c;而它与其他平台的差异&#xff0c;就在于它将编程练习变成了一个个游戏。这个平台的名字叫CodinGame&#xff0c;是一家法国创…

第98:svd原理

SVD分解&#xff1a;任何矩阵都可以分解成第一行的形式&#xff0c;3个相乘。UV都是正交矩阵&#xff0c;中间的是奇异值。 3个相乘的形式可以拆分。即奇异值*第一行*第一列。在相加。 奇异值有时很小&#xff0c;在这种情况下&#xff0c;丢掉&#xff0c;可以减少计算量&…