转:.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 point
    for j = 1 : cols - 1
        if data(i,j) ~= -1 % check if it is a missing value
            fprintf(farff, '%d,', data(i,j));
        else
            fprintf(farff, '?,');
        end
    end
    % print the label for the data point
    fprintf(farff, '%d\n', data(i,end));
end

% close the file
fclose(farff);

r = 0;
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

该方法的不足之处就是要单独提供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);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57

最后给出关于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}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12

因此,完整的一个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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

更多细节可查看 
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,end

import weka.core.converters.ArffLoader;

import java.io.File;
  • 1
  • 2
  • 3
  • 4
  • 5

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

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

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

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值