Removing attributes after performing attribute selection on Matlab

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Removing attributes after performing attribute selection on Matlab

Guilherme_Freire
I need to manually remove attributes using Remove after performing Attribute
Selection. I use the ReliefF method to create a ranking and then remove the
last attributes.

This is the code I am using for Attribute Selection:

/function attSel = wekaAttributeSelection( data, evaluator,
evaluator_options, search, search_options )

%%%some input checking here

evalObj = javaObject(evaluator);
if ~isempty(evaluator_options)
    evalObj.setOptions(evaluator_options);
end

searchObj = javaObject(search);
if ~isempty(search_options)
    searchObj.setOptions(search_options);
end

attSel = weka.attributeSelection.AttributeSelection();
attSel.setEvaluator(evalObj);
attSel.setSearch(searchObj);

attSel.SelectAttributes(data);
end/

And this is the code where I use Remove, and then try to classify the output
using RotationForest:

/function [AUC1,AUC2] = rocWeka(removidos,folds)
% ROC Curve
javaaddpath('C:\Program Files\Weka-3-6\weka.jar');
filename1='C:\Users\guilh_000\MyFile.arff';
if(~wekaPathCheck); return,end

% Some imports here

%% Loading arff file and creation of Instances object
filename = java.lang.String(filename1); % matlab string -> java string
loader   = ArffLoader();                   % loader constructor
loader.setFile(File(filename));            % loading arff
wekaOBJ  = loader.getDataSet();            % Instances object from loader
wekaOBJ.setClassIndex(wekaOBJ.numAttributes - 1); % class is last attribute

wekaClassifier = weka.classifiers.meta.RotationForest;
options = Utils.splitOptions('-G 3 -H 3 -P 50 -F
"weka.filters.unsupervised.attribute.PrincipalComponents -R 1.0 -A 5 -M -1"
-S 1 -I 10 -W weka.classifiers.trees.J48 -- -C 0.25 -M 2');
wekaClassifier.setOptions(options)


%% Cross validation
evalWeka = Evaluation(wekaOBJ);

% Mandatory parameters to get it working (supposed optional)
ran = Random(1);
sb = StringBuffer(0);
range = Range();
bo = Boolean(false);
ob = [sb; range; bo];

wekaOBJ = wekaAttributeSelection(wekaOBJ,
'weka.attributeSelection.ReliefFAttributeEval', [],  'Ranker', []);

f = javaObject('weka.filters.unsupervised.attribute.Remove');
f.setAttributeIndices(removidos);
*f.setInputFormat(wekaOBJ);*
rD = javaMethod('useFilter', 'weka.filters.Filter', wekaOBJ, f);

evalWeka.crossValidateModel(wekaClassifier, rD, folds, ran, ob);

AUC1 = evalWeka.areaUnderROC(0);
AUC2 = evalWeka.areaUnderROC(1);

end/

The problem happens at this line:
*f.setInputFormat(wekaOBJ);*

I get it that it is a incompatility problem with the object type. But the
code works fine without using Attribute Selection.

Thanks in advance.


Regards,

Guilherme Freire




--
Sent from: http://weka.8497.n7.nabble.com/
_______________________________________________
Wekalist mailing list
Send posts to: [hidden email]
To subscribe, unsubscribe, etc., visit 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
|

Re: Removing attributes after performing attribute selection on Matlab

Guilherme_Freire
I managed to find a solution by using the rankedAttributes() method. This
returns a list of the ranked features:


/A = wekaAttributeSelection(wekaOBJ,
'weka.attributeSelection.ReliefFAttributeEval', [],  'Ranker', []);
f = javaObject('weka.filters.unsupervised.attribute.Remove');
ranking = A.rankedAttributes();
remover = strcat(remover,num2str(ranking(AttributeToRemove,1)+1));
f.setAttributeIndices(remover);
f.setInputFormat(wekaOBJ);
rD = javaMethod('useFilter', 'weka.filters.Filter', wekaOBJ, f);
evalWeka.crossValidateModel(wekaClassifier, rD, folds, ran, ob);/


I will leave this here on the forum in case someone has the same problem.



--
Sent from: http://weka.8497.n7.nabble.com/
_______________________________________________
Wekalist mailing list
Send posts to: [hidden email]
To subscribe, unsubscribe, etc., visit 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
|

Re: Removing attributes after performing attribute selection on Matlab

Eibe Frank-2
Administrator
In reply to this post by Guilherme_Freire
The method setInputFormat() expects an argument of type Instances. Anyway, do *not* perform supervised attribute selection before running a k-fold cross-validation on the data. This will introduce optimistic bias in the performance estimates.

Use WEKA’s AttributeSelectedClassifier to perform attribute selection inside the k-fold cross-validation loop. In this way, attribute selection will be performed separately for each of the training sets that occurs in the k-fold cross-validation.

Cheers,
Eibe

> On 8/01/2019, at 2:47 AM, Guilherme_Freire <[hidden email]> wrote:
>
> I need to manually remove attributes using Remove after performing Attribute
> Selection. I use the ReliefF method to create a ranking and then remove the
> last attributes.
>
> This is the code I am using for Attribute Selection:
>
> /function attSel = wekaAttributeSelection( data, evaluator,
> evaluator_options, search, search_options )
>
> %%%some input checking here
>
> evalObj = javaObject(evaluator);
> if ~isempty(evaluator_options)
>    evalObj.setOptions(evaluator_options);
> end
>
> searchObj = javaObject(search);
> if ~isempty(search_options)
>    searchObj.setOptions(search_options);
> end
>
> attSel = weka.attributeSelection.AttributeSelection();
> attSel.setEvaluator(evalObj);
> attSel.setSearch(searchObj);
>
> attSel.SelectAttributes(data);
> end/
>
> And this is the code where I use Remove, and then try to classify the output
> using RotationForest:
>
> /function [AUC1,AUC2] = rocWeka(removidos,folds)
> % ROC Curve
> javaaddpath('C:\Program Files\Weka-3-6\weka.jar');
> filename1='C:\Users\guilh_000\MyFile.arff';
> if(~wekaPathCheck); return,end
>
> % Some imports here
>
> %% Loading arff file and creation of Instances object
> filename = java.lang.String(filename1); % matlab string -> java string
> loader   = ArffLoader();                   % loader constructor
> loader.setFile(File(filename));            % loading arff
> wekaOBJ  = loader.getDataSet();            % Instances object from loader
> wekaOBJ.setClassIndex(wekaOBJ.numAttributes - 1); % class is last attribute
>
> wekaClassifier = weka.classifiers.meta.RotationForest;
> options = Utils.splitOptions('-G 3 -H 3 -P 50 -F
> "weka.filters.unsupervised.attribute.PrincipalComponents -R 1.0 -A 5 -M -1"
> -S 1 -I 10 -W weka.classifiers.trees.J48 -- -C 0.25 -M 2');
> wekaClassifier.setOptions(options)
>
>
> %% Cross validation
> evalWeka = Evaluation(wekaOBJ);
>
> % Mandatory parameters to get it working (supposed optional)
> ran = Random(1);
> sb = StringBuffer(0);
> range = Range();
> bo = Boolean(false);
> ob = [sb; range; bo];
>
> wekaOBJ = wekaAttributeSelection(wekaOBJ,
> 'weka.attributeSelection.ReliefFAttributeEval', [],  'Ranker', []);
>
> f = javaObject('weka.filters.unsupervised.attribute.Remove');
> f.setAttributeIndices(removidos);
> *f.setInputFormat(wekaOBJ);*
> rD = javaMethod('useFilter', 'weka.filters.Filter', wekaOBJ, f);
>
> evalWeka.crossValidateModel(wekaClassifier, rD, folds, ran, ob);
>
> AUC1 = evalWeka.areaUnderROC(0);
> AUC2 = evalWeka.areaUnderROC(1);
>
> end/
>
> The problem happens at this line:
> *f.setInputFormat(wekaOBJ);*
>
> I get it that it is a incompatility problem with the object type. But the
> code works fine without using Attribute Selection.
>
> Thanks in advance.
>
>
> Regards,
>
> Guilherme Freire
>
>
>
>
> --
> Sent from: http://weka.8497.n7.nabble.com/
> _______________________________________________
> Wekalist mailing list
> Send posts to: [hidden email]
> To subscribe, unsubscribe, etc., visit 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]
To subscribe, unsubscribe, etc., visit https://list.waikato.ac.nz/mailman/listinfo/wekalist
List etiquette: http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html