爱彩乐十一选五

首頁 > 上網技巧 > 電腦小技巧 > mat格式數據集轉換為arff與txt格式

mat格式數據集轉換為arff與txt格式

時間:2019-10-05 16:28 作者:QQ地帶 我要評論

下面的代碼給出了將mat格式數據集轉換為arff與txt格式的matlab代碼。
 
注意,每個.mat文件中只有一個數據集,其中共有m+1列,最后一列是label。
 
轉為arff: mat2arff.m代碼
MATLAB
 
%
% 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 ...
% 讀取文件數據 ...
clear
clc
input_filename = 'GLIOMA-t.mat';
arff_filename = 'GLIOMA.arff';
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);
 
%
 
% 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 ...
 
 
 
% 讀取文件數據 ...
 
clear
clc
input_filename = 'GLIOMA-t.mat';
arff_filename = 'GLIOMA.arff';
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);
轉為txt: mat2txt.m代碼
當然也可用save直接轉換,但是會出現每一行開頭會空兩格的情況。
 
注意dataName.mat中的數據集名稱是data
 
MATLAB
 
clc
clear
load('dataName.mat')
fid = fopen('dataName.txt', 'wt');
for i = 1 : size(data, 1)
for j = 1 : size(data, 2) - 1
fprintf(fid,'%e   ',data(i, j));
end
fprintf(fid,'%e\n',data(i, size(data, 2)));
end
fclose(fid);
 
clc
clear
load('dataName.mat')
fid = fopen('dataName.txt', 'wt');
for i = 1 : size(data, 1)
    for j = 1 : size(data, 2) - 1
        fprintf(fid,'%e   ',data(i, j));
    end
    fprintf(fid,'%e\n',data(i, size(data, 2)));
end
fclose(fid);

標簽: mat
頂一下
(0)
0%
踩一下
(0)
0%

Google提供的廣告

爱彩乐十一选五 澳门乐百家规则 福建快三跨度一定牛 为什么骰宝人人都输 ag电子游戏怎么下载 双色球基本走势图500 波克捕鱼能作弊吗 体育彩票投注截止时间 360新疆时时走势图 飞艇骗局 在上海如何快速赚钱 澳门赌桌21点规则 云南时时中三 彩票怎么买 做车队老板好赚钱吗 联网二人麻将手机版 三分快三人工计划软件