From 24186dfd893cd6a0e9022ce2ef7e56dc2b6f9141 Mon Sep 17 00:00:00 2001 From: canoalberto Date: Tue, 30 Nov 2021 13:50:31 -0500 Subject: [PATCH 1/2] Fixes multilabel classifiers and multilabel generator --- .../com/yahoo/labs/samoa/instances/Range.java | 2 +- .../multilabel/MEKAClassifier.java | 2 +- .../multilabel/MajorityLabelset.java | 3 +- .../multilabel/MultilabelHoeffdingTree.java | 12 +++--- .../multilabel/trees/ISOUPTree.java | 3 +- .../multilabel/MetaMultilabelGenerator.java | 43 +++++++++++-------- 6 files changed, 39 insertions(+), 26 deletions(-) diff --git a/moa/src/main/java/com/yahoo/labs/samoa/instances/Range.java b/moa/src/main/java/com/yahoo/labs/samoa/instances/Range.java index 8de2b8a6a..2120911bf 100644 --- a/moa/src/main/java/com/yahoo/labs/samoa/instances/Range.java +++ b/moa/src/main/java/com/yahoo/labs/samoa/instances/Range.java @@ -27,7 +27,7 @@ public class Range implements Serializable { public Range(String range) { this.rangeText = range; - //this.setRange(range); //needs upperLimit + this.setRange(range); //needs upperLimit } /** diff --git a/moa/src/main/java/moa/classifiers/multilabel/MEKAClassifier.java b/moa/src/main/java/moa/classifiers/multilabel/MEKAClassifier.java index 7691b603f..935af3270 100644 --- a/moa/src/main/java/moa/classifiers/multilabel/MEKAClassifier.java +++ b/moa/src/main/java/moa/classifiers/multilabel/MEKAClassifier.java @@ -48,7 +48,7 @@ * @author Jesse Read * @version $Revision: 1 $ */ -public class MEKAClassifier extends AbstractMultiLabelLearner implements MultiTargetRegressor, Serializable { +public class MEKAClassifier extends AbstractMultiLabelLearner implements MultiLabelLearner, MultiTargetRegressor, Serializable { private static final long serialVersionUID = 1L; diff --git a/moa/src/main/java/moa/classifiers/multilabel/MajorityLabelset.java b/moa/src/main/java/moa/classifiers/multilabel/MajorityLabelset.java index 8810b36ab..e3e308ae9 100644 --- a/moa/src/main/java/moa/classifiers/multilabel/MajorityLabelset.java +++ b/moa/src/main/java/moa/classifiers/multilabel/MajorityLabelset.java @@ -28,6 +28,7 @@ import com.yahoo.labs.samoa.instances.MultiLabelPrediction; import com.yahoo.labs.samoa.instances.Prediction; import moa.classifiers.AbstractMultiLabelLearner; +import moa.classifiers.MultiLabelLearner; import moa.classifiers.MultiTargetRegressor; import moa.core.StringUtils; @@ -38,7 +39,7 @@ * @author Jesse Read (jesse@tsc.uc3m.es) * @version $Revision: 1 $ */ -public class MajorityLabelset extends AbstractMultiLabelLearner implements MultiTargetRegressor { +public class MajorityLabelset extends AbstractMultiLabelLearner implements MultiLabelLearner { //AbstractClassifier { private static final long serialVersionUID = 1L; diff --git a/moa/src/main/java/moa/classifiers/multilabel/MultilabelHoeffdingTree.java b/moa/src/main/java/moa/classifiers/multilabel/MultilabelHoeffdingTree.java index 544e6f0d5..1a43b5920 100644 --- a/moa/src/main/java/moa/classifiers/multilabel/MultilabelHoeffdingTree.java +++ b/moa/src/main/java/moa/classifiers/multilabel/MultilabelHoeffdingTree.java @@ -98,11 +98,13 @@ public MultilabelLearningNodeClassifier(double[] initialClassObservations, Class super(initialClassObservations); if (cl== null) { - this.classifier = ((Classifier) getPreparedClassOption(ht.learnerOption)).copy(); - this.classifier.resetLearning(); - - InstancesHeader raw_header = ht.getModelContext(); - this.classifier.setModelContext(raw_header); + MEKAClassifier learner = new MEKAClassifier(); + learner.baseLearnerOption.setValueViaCLIString("meka.classifiers.multilabel.incremental.PSUpdateable"); + learner.prepareForUse(); + learner.setModelContext(ht.getModelContext()); + + this.classifier = learner; + this.classifier.resetLearning(); } else{ this.classifier = cl.copy(); diff --git a/moa/src/main/java/moa/classifiers/multilabel/trees/ISOUPTree.java b/moa/src/main/java/moa/classifiers/multilabel/trees/ISOUPTree.java index 8279f76ab..78d03fbf9 100644 --- a/moa/src/main/java/moa/classifiers/multilabel/trees/ISOUPTree.java +++ b/moa/src/main/java/moa/classifiers/multilabel/trees/ISOUPTree.java @@ -40,6 +40,7 @@ import moa.AbstractMOAObject; import moa.classifiers.AbstractMultiLabelLearner; +import moa.classifiers.MultiLabelLearner; import moa.classifiers.MultiTargetRegressor; import moa.classifiers.multilabel.core.splitcriteria.ICVarianceReduction; import moa.classifiers.rules.core.Predicate; @@ -62,7 +63,7 @@ * @author Aljaž Osojnik (aljaz.osojnik@ijs.si) * @version $Revision: 1 $ */ -public class ISOUPTree extends AbstractMultiLabelLearner implements MultiTargetRegressor { +public class ISOUPTree extends AbstractMultiLabelLearner implements MultiLabelLearner, MultiTargetRegressor { private static final long serialVersionUID = 1L; diff --git a/moa/src/main/java/moa/streams/generators/multilabel/MetaMultilabelGenerator.java b/moa/src/main/java/moa/streams/generators/multilabel/MetaMultilabelGenerator.java index 421c11dbd..73317d985 100644 --- a/moa/src/main/java/moa/streams/generators/multilabel/MetaMultilabelGenerator.java +++ b/moa/src/main/java/moa/streams/generators/multilabel/MetaMultilabelGenerator.java @@ -28,11 +28,14 @@ import com.github.javacliparser.FloatOption; import com.github.javacliparser.IntOption; import moa.streams.InstanceStream; +import moa.streams.MultiTargetInstanceStream; import moa.tasks.TaskMonitor; import com.yahoo.labs.samoa.instances.Attribute; +import com.yahoo.labs.samoa.instances.DenseInstance; import com.yahoo.labs.samoa.instances.Instance; import com.yahoo.labs.samoa.instances.Instances; import com.yahoo.labs.samoa.instances.InstancesHeader; +import com.yahoo.labs.samoa.instances.Range; import com.yahoo.labs.samoa.instances.SparseInstance; import moa.core.FastVector; import moa.core.Utils; @@ -43,7 +46,7 @@ * @author Jesse Read ((jesse@tsc.uc3m.es)) * @version $Revision: 7 $ */ -public class MetaMultilabelGenerator extends AbstractOptionHandler implements InstanceStream { +public class MetaMultilabelGenerator extends AbstractOptionHandler implements MultiTargetInstanceStream { private static final long serialVersionUID = 1L; @@ -153,19 +156,25 @@ public void restart() { * @param si single-label Instances */ protected MultilabelInstancesHeader generateMultilabelHeader(Instances si) { - Instances mi = new Instances(si, 0, 0); - mi.setClassIndex(-1); - mi.deleteAttributeAt(mi.numAttributes() - 1); - FastVector bfv = new FastVector(); - bfv.addElement("0"); - bfv.addElement("1"); - for (int i = 0; i < this.m_L; i++) { - mi.insertAttributeAt(new Attribute("class" + i, bfv), i); - } - this.multilabelStreamTemplate = mi; - this.multilabelStreamTemplate.setRelationName("SYN_Z" + this.labelCardinalityOption.getValue() + "L" + this.m_L + "X" + m_A + "S" + metaRandomSeedOption.getValue() + ": -C " + this.m_L); - this.multilabelStreamTemplate.setClassIndex(this.m_L); - return new MultilabelInstancesHeader(multilabelStreamTemplate, m_L); + Instances mi = new Instances(si, 0, 0); + mi.deleteAttributeAt(mi.numAttributes() - 1); + FastVector bfv = new FastVector(); + bfv.addElement("0"); + bfv.addElement("1"); + for (int i = 0; i < this.m_L; i++) { + mi.insertAttributeAt(new Attribute("class" + i, bfv), i); + } + + Range range = new Range(Integer.toString((numLabelsOption.getValue()))); + + this.multilabelStreamTemplate = mi; + this.multilabelStreamTemplate.setRelationName("SYN_Z" + this.labelCardinalityOption.getValue() + "L" + this.m_L + "X" + m_A + "S" + metaRandomSeedOption.getValue() + ": -C " + this.m_L); + this.multilabelStreamTemplate.setClassIndex(Integer.MAX_VALUE); + this.multilabelStreamTemplate.setRangeOutputIndices(range); + + MultilabelInstancesHeader header = new MultilabelInstancesHeader(multilabelStreamTemplate, m_L); + header.setRangeOutputIndices(range); + return header; } /** @@ -267,7 +276,7 @@ private double joint(int k, int y[]) { private Instance generateMLInstance(HashSet Y) { // create a multi-label instance: - Instance x_ml = new SparseInstance(this.multilabelStreamTemplate.numAttributes()); + Instance x_ml = new DenseInstance(this.multilabelStreamTemplate.numAttributes()); x_ml.setDataset(this.multilabelStreamTemplate); // set classes @@ -472,7 +481,7 @@ public int compare(HashSet Y1, HashSet Y2) { } // shuffle - Collections.shuffle(Arrays.asList(map_set)); + Collections.shuffle(Arrays.asList(map_set), m_MetaRandom); // return return map_set; @@ -545,7 +554,7 @@ private ArrayList getShuffledListToLWithoutK(int L, int k) { list.add(j); } } - Collections.shuffle(list); + Collections.shuffle(list, m_MetaRandom); return list; } From 695a181374b06edccb22090c067b5fd9e4c75efc Mon Sep 17 00:00:00 2001 From: canoalberto Date: Tue, 30 Nov 2021 13:58:27 -0500 Subject: [PATCH 2/2] Formatting --- .../multilabel/MultilabelHoeffdingTree.java | 14 ++++---- .../multilabel/MetaMultilabelGenerator.java | 36 +++++++++---------- 2 files changed, 25 insertions(+), 25 deletions(-) diff --git a/moa/src/main/java/moa/classifiers/multilabel/MultilabelHoeffdingTree.java b/moa/src/main/java/moa/classifiers/multilabel/MultilabelHoeffdingTree.java index 1a43b5920..754499ab7 100644 --- a/moa/src/main/java/moa/classifiers/multilabel/MultilabelHoeffdingTree.java +++ b/moa/src/main/java/moa/classifiers/multilabel/MultilabelHoeffdingTree.java @@ -98,13 +98,13 @@ public MultilabelLearningNodeClassifier(double[] initialClassObservations, Class super(initialClassObservations); if (cl== null) { - MEKAClassifier learner = new MEKAClassifier(); - learner.baseLearnerOption.setValueViaCLIString("meka.classifiers.multilabel.incremental.PSUpdateable"); - learner.prepareForUse(); - learner.setModelContext(ht.getModelContext()); - - this.classifier = learner; - this.classifier.resetLearning(); + MEKAClassifier learner = new MEKAClassifier(); + learner.baseLearnerOption.setValueViaCLIString("meka.classifiers.multilabel.incremental.PSUpdateable"); + learner.prepareForUse(); + learner.setModelContext(ht.getModelContext()); + + this.classifier = learner; + this.classifier.resetLearning(); } else{ this.classifier = cl.copy(); diff --git a/moa/src/main/java/moa/streams/generators/multilabel/MetaMultilabelGenerator.java b/moa/src/main/java/moa/streams/generators/multilabel/MetaMultilabelGenerator.java index 73317d985..85bfe781e 100644 --- a/moa/src/main/java/moa/streams/generators/multilabel/MetaMultilabelGenerator.java +++ b/moa/src/main/java/moa/streams/generators/multilabel/MetaMultilabelGenerator.java @@ -157,24 +157,24 @@ public void restart() { */ protected MultilabelInstancesHeader generateMultilabelHeader(Instances si) { Instances mi = new Instances(si, 0, 0); - mi.deleteAttributeAt(mi.numAttributes() - 1); - FastVector bfv = new FastVector(); - bfv.addElement("0"); - bfv.addElement("1"); - for (int i = 0; i < this.m_L; i++) { - mi.insertAttributeAt(new Attribute("class" + i, bfv), i); - } - - Range range = new Range(Integer.toString((numLabelsOption.getValue()))); - - this.multilabelStreamTemplate = mi; - this.multilabelStreamTemplate.setRelationName("SYN_Z" + this.labelCardinalityOption.getValue() + "L" + this.m_L + "X" + m_A + "S" + metaRandomSeedOption.getValue() + ": -C " + this.m_L); - this.multilabelStreamTemplate.setClassIndex(Integer.MAX_VALUE); - this.multilabelStreamTemplate.setRangeOutputIndices(range); - - MultilabelInstancesHeader header = new MultilabelInstancesHeader(multilabelStreamTemplate, m_L); - header.setRangeOutputIndices(range); - return header; + mi.deleteAttributeAt(mi.numAttributes() - 1); + FastVector bfv = new FastVector(); + bfv.addElement("0"); + bfv.addElement("1"); + for (int i = 0; i < this.m_L; i++) { + mi.insertAttributeAt(new Attribute("class" + i, bfv), i); + } + + Range range = new Range(Integer.toString((numLabelsOption.getValue()))); + + this.multilabelStreamTemplate = mi; + this.multilabelStreamTemplate.setRelationName("SYN_Z" + this.labelCardinalityOption.getValue() + "L" + this.m_L + "X" + m_A + "S" + metaRandomSeedOption.getValue() + ": -C " + this.m_L); + this.multilabelStreamTemplate.setClassIndex(Integer.MAX_VALUE); + this.multilabelStreamTemplate.setRangeOutputIndices(range); + + MultilabelInstancesHeader header = new MultilabelInstancesHeader(multilabelStreamTemplate, m_L); + header.setRangeOutputIndices(range); + return header; } /**