Crossvalidate weka from matlab

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Crossvalidate weka from matlab

IAN3125
Hello I have this code that uses weka library from matlab. I have got problems in using the cross validation method used in weka.

javaaddpath('/home/ml/weka-3-8/weka.jar');

Attribute1=javaObject('weka.core.Attribute','M');
Attribute2=javaObject('weka.core.Attribute','F');
Attribute3=javaObject('weka.core.Attribute','w');
ClassAttribute=javaObject('weka.core.Attribute','Y');

fvWekaAttribute=javaObject('java.util.ArrayList');
fvWekaAttribute.add(Attribute1);
fvWekaAttribute.add(Attribute2);
fvWekaAttribute.add(Attribute3);
fvWekaAttribute.add(ClassAttribute);

%create training points
isTrainingSet=javaObject('weka.core.Instances','Rel',fvWekaAttribute,10);
isTrainingSet.setClassIndex(3);

iExample=javaObject('weka.core.DenseInstance', 4);
iExample.setValue(fvWekaAttribute.get(0), 1.0);
iExample.setValue(fvWekaAttribute.get(1), 0.5);
iExample.setValue(fvWekaAttribute.get(2), 1.0);
iExample.setValue(fvWekaAttribute.get(3), 1.0);
isTrainingSet.add(iExample);

iExample2=javaObject('weka.core.DenseInstance', 4);
iExample2.setValue(fvWekaAttribute.get(0), 0);
iExample2.setValue(fvWekaAttribute.get(1), 0.5);
iExample2.setValue(fvWekaAttribute.get(2), 0);
iExample2.setValue(fvWekaAttribute.get(3), 0);
isTrainingSet.add(iExample2);

iExample3=javaObject('weka.core.DenseInstance', 4);
iExample3.setValue(fvWekaAttribute.get(0), 4);
iExample3.setValue(fvWekaAttribute.get(1), 4);
iExample3.setValue(fvWekaAttribute.get(2), 4);
iExample3.setValue(fvWekaAttribute.get(3), 5);
isTrainingSet.add(iExample3);

%create testing points points
isTestingSet=javaObject('weka.core.Instances','Rel',fvWekaAttribute,10);
isTestingSet.setClassIndex(3);

iExample=javaObject('weka.core.DenseInstance', 4);
iExample.setValue(fvWekaAttribute.get(0), 1.0);
iExample.setValue(fvWekaAttribute.get(1), 0.5);
iExample.setValue(fvWekaAttribute.get(2), 1.0);
iExample.setValue(fvWekaAttribute.get(3), 1.0);
isTestingSet.add(iExample);

iExample2=javaObject('weka.core.DenseInstance', 4);
iExample2.setValue(fvWekaAttribute.get(0), 0);
iExample2.setValue(fvWekaAttribute.get(1), 0.5);
iExample2.setValue(fvWekaAttribute.get(2), 0);
iExample2.setValue(fvWekaAttribute.get(3), 0);
isTestingSet.add(iExample2);

iExample3=javaObject('weka.core.DenseInstance', 4);
iExample3.setValue(fvWekaAttribute.get(0), 4);
iExample3.setValue(fvWekaAttribute.get(1), 4);
iExample3.setValue(fvWekaAttribute.get(2), 4);
iExample3.setValue(fvWekaAttribute.get(3), 5);
isTestingSet.add(iExample3);

cModel = javaObject('weka.classifiers.functions.SMOreg');
cModel.buildClassifier(isTrainingSet);
cModel.toString()

e = javaObject("weka.classifiers.evaluation.Evaluation", isTrainingSet);
e.crossValidateModel(cModel, isTestingSett, 10, javaObject('java.util.Random',1), javaArray('java.lang.Object', 1) );
e.toSummaryString()

I always get the error java.lang.NullPointerException at the e.crossValidateModel line. Any help on this?

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Crossvalidate weka from matlab

Hawkins Tom (AA-AS/ESD)
> Hello I have this code that uses weka library from matlab. I have got problems in using the cross validation method used in weka.
> ...
> isTestingSet=javaObject('weka.core.Instances','Rel',fvWekaAttribute,10);
> ...
> e = javaObject("weka.classifiers.evaluation.Evaluation", isTrainingSet);
> e.crossValidateModel(cModel, isTestingSett, 10, javaObject('java.util.Random',1), javaArray('java.lang.Object', 1) );
> ...
> I always get the error java.lang.NullPointerException at the e.crossValidateModel line. Any help on this?

In the code you have posted, there is a typo: 'isTestingSett' instead of isTestingSet. Is that the problem?

_______________________________________________
Wekalist mailing list
Send posts to: [hidden email]
List info and subscription status: https://list.waikato.ac.nz/mailman/listinfo/wekalist
List etiquette: http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Crossvalidate weka from matlab

IAN3125
Yes it's a typo.. but i still encounter the same problem.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Crossvalidate weka from matlab

Eibe Frank-2
Administrator
There are two other problems in the e.crossValidateModel(...) command in your Matlab program. The first one is that there are only three instances in your isTestingSet object, so you can run at most a three-fold cross-validation. The second problem is that the object array needs to have size 0. The following modified version of your line works:

e.crossValidateModel(cModel, isTestingSet, 3, javaObject('java.util.Random',1), javaArray('java.lang.Object', 0));

Cheers,
Eibe

> On 28 Apr 2017, at 23:52, IAN3125 <[hidden email]> wrote:
>
> Yes it's a typo.. but i still encounter the same problem.
>
>
>
> --
> View this message in context: http://weka.8497.n7.nabble.com/Crossvalidate-weka-from-matlab-tp40335p40351.html
> Sent from the WEKA mailing list archive at Nabble.com.
> _______________________________________________
> Wekalist mailing list
> Send posts to: [hidden email]
> List info and subscription status: https://list.waikato.ac.nz/mailman/listinfo/wekalist
> List etiquette: http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html

_______________________________________________
Wekalist mailing list
Send posts to: [hidden email]
List info and subscription status: https://list.waikato.ac.nz/mailman/listinfo/wekalist
List etiquette: http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Crossvalidate weka from matlab

IAN3125
Hello Frank,

I have tried your suggestion of changing into javaArray('java.lang.Object', 0) however I get an error "Error using javaArray All dimension arguments must be greater than zero ". I also change the number of folds into 3.

now if I change it back to javaArray('java.lang.Object', 1) I get an error "Java exception occurred: java.lang.NullPointerException".

I dont know which part I am getting wrong.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Crossvalidate weka from matlab

Eibe Frank-2
Administrator
I don’t have Matlab, only Octave, where the zero-element array works.

Perhaps try the following to generate a zero-element area of base type Object:

  toArray(java.util.Vector)

It’s from

  https://au.mathworks.com/matlabcentral/newsreader/view_thread/269714

Cheers,
Eibe

> On 1/05/2017, at 1:12 AM, IAN3125 <[hidden email]> wrote:
>
> Hello Frank,
>
> I have tried your suggestion of changing into javaArray('java.lang.Object',
> 0) however I get an error "Error using javaArray All dimension arguments
> must be greater than zero ". I also change the number of folds into 3.
>
> now if I change it back to javaArray('java.lang.Object', 1) I get an error
> "Java exception occurred: java.lang.NullPointerException".
>
> I dont know which part I am getting wrong.
>
>
>
> --
> View this message in context: http://weka.8497.n7.nabble.com/Crossvalidate-weka-from-matlab-tp40335p40393.html
> Sent from the WEKA mailing list archive at Nabble.com.
> _______________________________________________
> Wekalist mailing list
> Send posts to: [hidden email]
> List info and subscription status: https://list.waikato.ac.nz/mailman/listinfo/wekalist
> List etiquette: http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html

_______________________________________________
Wekalist mailing list
Send posts to: [hidden email]
List info and subscription status: https://list.waikato.ac.nz/mailman/listinfo/wekalist
List etiquette: http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Crossvalidate weka from matlab

Hawkins Tom (AA-AS/ESD)
In reply to this post by IAN3125
I'm successfully using Weka J48 from MATLAB and Eibe's example of

    e.crossValidateModel(cModel, isTestingSet, 3, javaObject('java.util.Random',1), javaArray('java.lang.Object', 0));

looks like it exactly matches the corresponding line from my code:

    wekaEvalObject.crossValidateModel(wekaTreeObject, arffObject, obj.crossValidationFolds, ...
        javaObject('java.util.Random', 1), javaArray('java.lang.Object',0));

so I don't think the problem is with this line, but with one of the objects going in to it. My dataset comes from an ARFF file (hence 'arffObject') but otherwise I'm setting up the model the same sort of way.

Have you tried creating your dataset in ARFF format and running this classifier in Weka Explorer? Does that work without errors?

Tom


_______________________________________________
Wekalist mailing list
Send posts to: [hidden email]
List info and subscription status: https://list.waikato.ac.nz/mailman/listinfo/wekalist
List etiquette: http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Crossvalidate weka from matlab

IAN3125
Yes it works properly when I am using the weka explorer with data in .csv format. however, I need to use matlab for this problem since i want to use the classifier inside an optimization problem in matlab.

I still have not figure this out. I think the problem is on the javaObject('java.util.Random',1) cause when i use

e.evaluateModel(cModel, isTestingSet, javaArray('java.lang.Object', 1));

instead of the crossvalidate function. it works properly. however i want to use the crossvalidate function.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Crossvalidate weka from matlab

Eibe Frank-2
Administrator
No, Random is not the problem. The last, vararg argument of crossValidateModel() is.

The latest version of Matlab should support 0-length Java arrays according to the documentation:

  https://au.mathworks.com/help/matlab/ref/javaarray.html

You are probably using an old version of Matlab? I replicated your problem using Matlab R2013b. It does not seem to support 0-length Java arrays.

One way to make it work is to actually create a one-element array for the vararg parameter. I have confirmed using Matlab R2013b that the following works:

-----------

javaaddpath('/home/ml/weka-3-8/weka.jar');

Attribute1=javaObject('weka.core.Attribute','M');
Attribute2=javaObject('weka.core.Attribute','F');
Attribute3=javaObject('weka.core.Attribute','w');
ClassAttribute=javaObject('weka.core.Attribute','Y');

fvWekaAttribute=javaObject('java.util.ArrayList');
fvWekaAttribute.add(Attribute1);
fvWekaAttribute.add(Attribute2);
fvWekaAttribute.add(Attribute3);
fvWekaAttribute.add(ClassAttribute);

%create training points
isTrainingSet=javaObject('weka.core.Instances','Rel',fvWekaAttribute,10);
isTrainingSet.setClassIndex(3);

iExample=javaObject('weka.core.DenseInstance', 4);
iExample.setValue(fvWekaAttribute.get(0), 1.0);
iExample.setValue(fvWekaAttribute.get(1), 0.5);
iExample.setValue(fvWekaAttribute.get(2), 1.0);
iExample.setValue(fvWekaAttribute.get(3), 1.0);
isTrainingSet.add(iExample);

iExample2=javaObject('weka.core.DenseInstance', 4);
iExample2.setValue(fvWekaAttribute.get(0), 0);
iExample2.setValue(fvWekaAttribute.get(1), 0.5);
iExample2.setValue(fvWekaAttribute.get(2), 0);
iExample2.setValue(fvWekaAttribute.get(3), 0);
isTrainingSet.add(iExample2);

iExample3=javaObject('weka.core.DenseInstance', 4);
iExample3.setValue(fvWekaAttribute.get(0), 4);
iExample3.setValue(fvWekaAttribute.get(1), 4);
iExample3.setValue(fvWekaAttribute.get(2), 4);
iExample3.setValue(fvWekaAttribute.get(3), 5);
isTrainingSet.add(iExample3);

%create testing points points
isTestingSet=javaObject('weka.core.Instances','Rel',fvWekaAttribute,10);
isTestingSet.setClassIndex(3);

iExample=javaObject('weka.core.DenseInstance', 4);
iExample.setValue(fvWekaAttribute.get(0), 1.0);
iExample.setValue(fvWekaAttribute.get(1), 0.5);
iExample.setValue(fvWekaAttribute.get(2), 1.0);
iExample.setValue(fvWekaAttribute.get(3), 1.0);
isTestingSet.add(iExample);

iExample2=javaObject('weka.core.DenseInstance', 4);
iExample2.setValue(fvWekaAttribute.get(0), 0);
iExample2.setValue(fvWekaAttribute.get(1), 0.5);
iExample2.setValue(fvWekaAttribute.get(2), 0);
iExample2.setValue(fvWekaAttribute.get(3), 0);
isTestingSet.add(iExample2);

iExample3=javaObject('weka.core.DenseInstance', 4);
iExample3.setValue(fvWekaAttribute.get(0), 4);
iExample3.setValue(fvWekaAttribute.get(1), 4);
iExample3.setValue(fvWekaAttribute.get(2), 4);
iExample3.setValue(fvWekaAttribute.get(3), 5);
isTestingSet.add(iExample3);

cModel = javaObject('weka.classifiers.functions.SMOreg');
cModel.buildClassifier(isTrainingSet);
cModel.toString()

e = javaObject('weka.classifiers.evaluation.Evaluation', isTrainingSet);
predictionOutput = javaArray('weka.classifiers.evaluation.output.prediction.AbstractOutput',1);
plainText = javaObject('weka.classifiers.evaluation.output.prediction.PlainText');
stringBuffer = javaObject('java.lang.StringBuffer');
plainText.setBuffer(stringBuffer);
predictionOutput(1) = plainText;
e.crossValidateModel(cModel, isTestingSet, 3, javaObject('java.util.Random',1), predictionOutput);
e.toSummaryString()
stringBuffer

-----------

In this case, the predictions will be printed into stringBuffer. You obviously don’t have to output stringBuffer though.

Cheers,
Eibe

> On 4/05/2017, at 11:07 PM, IAN3125 <[hidden email]> wrote:
>
> Yes it works properly when I am using the weka explorer with data in .csv
> format. however, I need to use matlab for this problem since i want to use
> the classifier inside an optimization problem in matlab.
>
> I still have not figure this out. I think the problem is on the
> javaObject('java.util.Random',1) cause when i use
>
> e.evaluateModel(cModel, isTestingSet, javaArray('java.lang.Object', 1));
>
> instead of the crossvalidate function. it works properly. however i want to
> use the crossvalidate function.
>
>
>
> --
> View this message in context: http://weka.8497.n7.nabble.com/Crossvalidate-weka-from-matlab-tp40335p40479.html
> Sent from the WEKA mailing list archive at Nabble.com.
> _______________________________________________
> Wekalist mailing list
> Send posts to: [hidden email]
> List info and subscription status: https://list.waikato.ac.nz/mailman/listinfo/wekalist
> List etiquette: http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html

_______________________________________________
Wekalist mailing list
Send posts to: [hidden email]
List info and subscription status: https://list.waikato.ac.nz/mailman/listinfo/wekalist
List etiquette: http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html
Loading...