日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 人工智能 > 循环神经网络 >内容正文

循环神经网络

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

發(fā)布時間:2023/12/13 循环神经网络 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 .mat,.txt,.csv 数据转换为weka中的arff格式及matlab和Weka之间相互转换格式 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

在RUSBoost和SMOTEBoost中提供了csv轉(zhuǎn)換為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 ,很多情況下,該表頭需要人工添加(屬性少時),但當屬性大時,相對較麻煩,還是可以通過程序循環(huán)添加。

下面給出一個可以直接將.mat,.txt和.csv格式轉(zhuǎn)換為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 ...% 讀取文件數(shù)據(jù) ... 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); % 開始保存數(shù)據(jù) ... 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數(shù)據(jù)處理的簡明介紹。
數(shù)據(jù)挖掘簡述和weka介紹–數(shù)據(jù)挖掘?qū)W習和weka使用(一)
輸入數(shù)據(jù)與ARFF文件–數(shù)據(jù)挖掘?qū)W習和weka使用(二)
簡單總結一下:
weka中的arff格式數(shù)據(jù)是由兩部分組成:頭部定義和數(shù)據(jù)區(qū)。
頭部定義包含了關系名稱(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說明其為數(shù)字型,屬性class的取值是限定的,只能是Iris-setosa,Iris-versicolor,Iris-virginica中的一個。數(shù)據(jù)類型還可以是string和data數(shù)據(jù)區(qū)有@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

更多細節(jié)可查看
http://weka.wikispaces.com/ARFF+%28stable+version%29#Sparse%20ARFF%20files

weka使用自己的文件格式,叫做ARFF,如果想從*matlab和Weka之間相互轉(zhuǎn)換,這里有現(xiàn)成的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

總結

以上是生活随笔為你收集整理的.mat,.txt,.csv 数据转换为weka中的arff格式及matlab和Weka之间相互转换格式的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。