是否有任何包加载.arff格式文件到matlab?.arff格式在Weka中用于运行机器学习算法.
由于Weka是一个Java库,您可以直接使用它公开的API来读取ARFF文件:
%## paths WEKA_HOME = 'C:\Program Files\Weka-3-7'; javaaddpath([WEKA_HOME '\weka.jar']); fName = [WEKA_HOME '\data\iris.arff']; %## read file loader = weka.core.converters.ArffLoader(); loader.setFile( java.io.File(fName) ); D = loader.getDataSet(); D.setClassIndex( D.numAttributes()-1 ); %## dataset relationName = char(D.relationName); numAttr = D.numAttributes; numInst = D.numInstances; %## attributes %# attribute names attributeNames = arrayfun(@(k) char(D.attribute(k).name), 0:numAttr-1, 'Uni',false); %# attribute types types = {'numeric' 'nominal' 'string' 'date' 'relational'}; attributeTypes = arrayfun(@(k) D.attribute(k-1).type, 1:numAttr); attributeTypes = types(attributeTypes+1); %# nominal attribute values nominalValues = cell(numAttr,1); for i=1:numAttr if strcmpi(attributeTypes{i},'nominal') nominalValues{i} = arrayfun(@(k) char(D.attribute(i-1).value(k-1)), 1:D.attribute(i-1).numValues, 'Uni',false); end end %## instances data = zeros(numInst,numAttr); for i=1:numAttr data(:,i) = D.attributeToDoubleArray(i-1); end %## visualize data parallelcoords(data(:,1:end-1), ... 'Group',nominalValues{end}(data(:,end)+1), ... 'Labels',attributeNames(1:end-1)) title(relationName)
您甚至可以直接使用MATLAB的功能.一个例子:
%## classification classifier = weka.classifiers.trees.J48(); classifier.buildClassifier( D ); fprintf('Classifier: %s %s\n%s', ... char(classifier.getClass().getName()), ... char(weka.core.Utils.joinOptions(classifier.getOptions())), ... char(classifier.toString()) )
输出C4.5决策树:
Classifier: weka.classifiers.trees.J48 -C 0.25 -M 2 J48 pruned tree ------------------ petalwidth <= 0.6: Iris-setosa (50.0) petalwidth > 0.6 | petalwidth <= 1.7 | | petallength <= 4.9: Iris-versicolor (48.0/1.0) | | petallength > 4.9 | | | petalwidth <= 1.5: Iris-virginica (3.0) | | | petalwidth > 1.5: Iris-versicolor (3.0/1.0) | petalwidth > 1.7: Iris-virginica (46.0/1.0) Number of Leaves : 5 Size of the tree : 9