Can somebody help me with calling weka algorithms in matlab?

1 view (last 30 days)
Hello,
I'm working on machine learning techniques and instead of using WEKA workbench, I want to use the same algorithms but integrate in Matlab. the first step, creating a classifier and classifying an unknown instances I can do it in matlab but i'm facing with problem in extracting the parameters such as number of correctly classified instances, confusion matrix and so on. here's the way how I do it:
%%%include jar libraries to matlab environment:
javaaddpath('C:\Program Files\MATLAB\R2011a\java\jar\weka.jar');
%%%importing of classes
import java.util.Enumeration
import java.lang.String
import weka.classifiers.Classifier
import weka.classifiers.Evaluation
import weka.classifiers.trees.J48
import java.io.FileReader
import weka.core.Instances
import weka.core.Utils
import weka.core.Attribute
import java.lang.System
%%%Reading and configuring treaining set
reader = javaObject('java.io.FileReader','TrainSetOfInstances.arff');
dataset = javaObject('weka.core.Instances',reader);
dataset.setClassIndex(dataset.numAttributes() - 1);
%%%Reading and Configuring testing set (in case we want to perform train/test evaluation)
reader1 = javaObject('java.io.FileReader','TestSetOfInstances.arff');
testset = javaObject('weka.core.Instances',reader1);
testset.setClassIndex(testset.numAttributes() - 1);
%%%getting the parameters of an arff file
relationName = char(dataset.relationName);
numAttr = dataset.numAttributes;
numInst = dataset.numInstances;
%%%creating an object of class J48 and setup the default options
classifier = javaObject('weka.classifiers.trees.J48');
classifier.setOptions(weka.core.Utils.splitOptions('-c last -C 0.25 -M 2'));
%%%string for setting the options
v1 = String('-t');
v2 = String('TrainSetOfInstances.arff');
v3 = String('-x'); %%%if we want cross-validation
v4 = String('10'); %%%with number of folds
options = cat(1,v1,v2,v3,v4);
%%%Creation of classifier Model
classifier.buildClassifier(dataset);
%%%evaluation of the Classifier with options
weka.classifiers.Evaluation.evaluateModel(classifier,options)
%%%creating an object containing the evaluations
eval = weka.classifiers.Evaluation(dataset);
eval.evaluateModel(classifier, options1);
%%%this part works properly
%%%printing the parameters of the evaluation
fprintf('=== Run information ===\n\n')
fprintf('Scheme: %s %s\n', ...
char(classifier.getClass().getName()), ...
char(weka.core.Utils.joinOptions(classifier.getOptions())) )
fprintf('Relation: %s\n', char(dataset.relationName))
fprintf('Instances: %d\n', dataset.numInstances)
fprintf('Attributes: %d\n\n', dataset.numAttributes)
%%%this part works properly
fprintf('=== Classifier model ===\n\n')
disp( char(classifier.toString()) )
%%%this part works properly
fprintf('=== Summary ===\n')
disp( char(eval.toSummaryString()) )
disp( char(eval.toClassDetailsString()) )
disp( char(eval.toMatrixString()) )
%%%This part does not work properly, all of the time I get zeros and dont know why!
So, my problem is, when I run the evaluation command:
%%%evaluation of the Classifier with options
weka.classifiers.Evaluation.evaluateModel(classifier,options)
in the command line I get the classification tree, evaluation model, confusion matrix and all others statistics that I get in weka workbench but when I try to extract them using following methods: toSummaryString(), toClassDetailsString() or toMatrixString() I get only zeros!
So, can someone tell me what I don't do or what I do wrong?
Thanks
  2 Comments
Asdrubal Lopez Chau
Asdrubal Lopez Chau on 19 Dec 2014
Hi Aleksandar Petrov, maybe this can help you
clc
clear
%Load from disk
fileDataset = 'cm1.arff';
myPath = 'C:\Users\Asdrubal\Google Drive\Respaldo\DoctoradoALCPC\Doctorado ALC PC\AlcMobile\AvTh\MyPapers\Papers2014\UnderOverSampling\data\Skewed\datasetsKeel\';
javaaddpath('C:\Users\Asdrubal\Google Drive\Respaldo\DoctoradoALCPC\Doctorado ALC PC\AlcMobile\JarsForExperiments\weka.jar');
wekaOBJ = loadARFF([myPath fileDataset]);
%Transform from data into Matlab
[data, featureNames, targetNDX, stringVals, relationName] = ...
weka2matlab(wekaOBJ,'[]');
%Create testing and training sets in matlab format (this can be improved)
[tam, dim] = size(data);
idx = randperm(tam);
testIdx = idx(1 : tam*0.3);
trainIdx = idx(tam*0.3 + 1:end);
trainSet = data(trainIdx,:);
testSet = data(testIdx,:);
%Trasnform the training and the testing sets into the Weka format
testingWeka = matlab2weka('testing', featureNames, testSet);
trainingWeka = matlab2weka('training', featureNames, trainSet);
%Now evaluate classifier
import weka.classifiers.*;
import java.util.*
wekaClassifier = javaObject('weka.classifiers.trees.J48');
wekaClassifier.buildClassifier(trainingWeka);
e = javaObject('weka.classifiers.Evaluation',trainingWeka);
myrand = Random(1);
plainText = javaObject('weka.classifiers.evaluation.output.prediction.PlainText');
buffer = javaObject('java.lang.StringBuffer');
plainText.setBuffer(buffer)
bool = javaObject('java.lang.Boolean',true);
range = javaObject('weka.core.Range','1');
array = javaArray('java.lang.Object',3);
array(1) = plainText;
array(2) = range;
array(3) = bool;
e.crossValidateModel(wekaClassifier,testingWeka,10,myrand,array)%U
e.toClassDetailsString
alisha murugesh
alisha murugesh on 4 Mar 2016
%%%creating an object containing the evaluations
eval = weka.classifiers.Evaluation(dataset);
eval.evaluateModel(classifier, options1);
I tried executing the above code, and I have a doubt in the above snippet, is it supposed to be 'options1' ,should'nt it be options, however when I change it to 'options'
This is the error I get
Java exception occurred: java.lang.Exception: Weka exception: No training file and no object input file given.

Sign in to comment.

Answers (1)

Stalin Samuel
Stalin Samuel on 19 Dec 2014

Categories

Find more on Statistics and Machine Learning Toolbox in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!