I'm trying to retrieve classes from WEKA using MATLAB and WEKA API. All looks fine but classes are always 0. Any idea ??
My data set has 241 atributes, applying WEKA to this dataset I'm obtaining correct results.
1st train and test objects are created than classifier is build and classifyInstance performed. But this give wrong result
train = [xtrain ytrain];
test = [xtest];
save ('train.txt','train','-ASCII');
save ('test.txt','test','-ASCII');
%## paths
WEKA_HOME = 'C:\Program Files\Weka-3-7';
javaaddpath([WEKA_HOME '\weka.jar']);
fName = 'train.txt';
%## read file
loader = weka.core.converters.MatlabLoader();
loader.setFile( java.io.File(fName) );
train = loader.getDataSet();
train.setClassIndex( train.numAttributes()-1 );
% setting class as nominal
v(1) = java.lang.String('-R');
v(2) = java.lang.String('242');
options = cat(1,v(1:end));
filter = weka.filters.unsupervised.attribute.NumericToNominal();
filter.setOptions(options);
filter.setInputFormat(train);
train = filter.useFilter(train, filter);
fName = 'test.txt';
%## read file
loader = weka.core.converters.MatlabLoader();
loader.setFile( java.io.File(fName) );
test = loader.getDataSet();
%## dataset
relationName = char(test.relationName);
numAttr = test.numAttributes;
numInst = test.numInstances;
%## classification
classifier = weka.classifiers.trees.J48();
classifier.buildClassifier( train );
fprintf('Classifier: %s %s\n%s', ...
char(classifier.getClass().getName()), ...
char(weka.core.Utils.joinOptions(classifier.getOptions())), ...
char(classifier.toString()) )
classes =[];
for i=1:numInst
classes(i) = classifier.classifyInstance(test.instance(i-1));
end
Here is a new code but still not working - classes = 0. Output from Weka for the same algo and data set is OK
=== Detailed Accuracy By Class ===
TP Rate FP Rate Precision Recall F-Measure ROC Area Class 0.99 0.015 0.985 0.99 0.988 0.991 0 0.985 0.01 0.99 0.985 0.988 0.991 1 Weighted Avg. 0.988 0.012 0.988 0.988 0.988 0.991
=== Confusion Matrix ===
a b <-- classified as 1012 10 | a = 0 15 1003 | b = 1
ytest1 = ones(size(xtest,1),1);
train = [xtrain ytrain];
test = [xtest ytest1];
save ('train.txt','train','-ASCII');
save ('test.txt','test','-ASCII');
%## paths
WEKA_HOME = 'C:\Program Files\Weka-3-7';
javaaddpath([WEKA_HOME '\weka.jar']);
fName = 'train.txt';
%## read file
loader = weka.core.converters.MatlabLoader();
loader.setFile( java.io.File(fName) );
train = loader.getDataSet();
train.setClassIndex( train.numAttributes()-1 );
v(1) = java.lang.String('-R');
v(2) = java.lang.String('242');
options = cat(1,v(1:end));
filter = weka.filters.unsupervised.attribute.NumericToNominal();
filter.setOptions(options);
filter.setInputFormat(train);
train = filter.useFilter(train, filter);
fName = 'test.txt';
%## read file
loader = weka.core.converters.MatlabLoader();
loader.setFile( java.io.File(fName) );
test = loader.getDataSet();
filter = weka.filters.unsupervised.attribute.NumericToNominal();
filter.setOptions( weka.core.Utils.splitOptions('-R last') );
filter.setInputFormat(test);
test = filter.useFilter(test, filter);
%## dataset
relationName = char(test.relationName);
numAttr = test.numAttributes;
numInst = test.numInstances;
%## classification
classifier = weka.classifiers.trees.J48();
classifier.buildClassifier( train );
fprintf('Classifier: %s %s\n%s', ...
char(classifier.getClass().getName()), ...
char(weka.core.Utils.joinOptions(classifier.getOptions())), ...
char(classifier.toString()) )
classes = zeros(numInst,1);
for i=1:numInst
classes(i) = classifier.classifyInstance(test.instance(i-1));
end
here is a code snippet for class distribution in Java
// output predictions
System.out.println("# - actual - predicted - error - distribution");
for (int i = 0; i < test.numInstances(); i++) {
double pred = cls.classifyInstance(test.instance(i));
double[] dist = cls.distributionForInstance(test.instance(i));
System.out.print((i+1));
System.out.print(" - ");
System.out.print(test.instance(i).toString(test.classIndex()));
System.out.print(" - ");
System.out.print(test.classAttribute().value((int) pred));
System.out.print(" - ");
if (pred != test.instance(i).classValue())
System.out.print("yes");
else
System.out.print("no");
System.out.print(" - ");
System.out.print(Utils.arrayToString(dist));
System.out.println();
I converted it to MATLAB code like this
classes = zeros(numInst,1);
for i=1:numInst
pred = classifier.classifyInstance(test.instance(i-1));
classes(i) = str2num(char(test.classAttribute().value(( pred))));
end
but classes are output incorrectly.
In your answer you dont show that pred contains classes and predProb probabilities. Just print it !!!
Training and testing data must have the same number of attributes. So in your case, even if you don't know the actual class of the test data, just use dummy values:
Don't forget to convert it to a nominal attribute when you load the test dataset (like you did for the training dataset):
Finally, you can call the trained J48 classifier to predict the class values for the test instances:
EDIT
It is difficult to tell without knowing the data you are working with..
So let me illustrate with a complete example. I am going to be creating the datasets in MATLAB out of the Fisher Iris data (4 attributes, 150 instances, 3 classes).
I should mention here that it is important to make sure that the class values are fully represented in each of the two datasets before using the
NumericToNominal
filter. Otherwise, the train and test sets could be incompatible. What I mean is that you must have at least one instance from every class value in each. Thus if you are using dummy values, maybe we can do this:Next, lets read the newly created files using Weka API. We convert the last attribute from numeric to nominal (to enable classification):
Now we train a J48 classifier and use it to predict the class of the test instances:
Finally, we evaluate the trained model performance over the test data (this should look similar to what you see in Weka Explorer). Obviously this only makes sense if the test instances have the true class value (not dummy values):
The output in MATLAB for the example above: