How to change the attribute value of a multi-instance?

View: New views
2 Messages — Rating Filter:   Alert me  

How to change the attribute value of a multi-instance?

by Dr. Xingquan Zhu :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Some parts of this message have been removed. Learn more about Nabble's security policy.

Hi, Peter,

 

Do you have any suggestion on how to change the attribute value of an MI instance (multi-instance)?

 

More specifically, after we transfer the instances from propositional to MultiInstances format. Each MI instance only contains three attributes. The second attribute is a relational attribute which contains all the attribute values.

 

What we intend to do is to change the second attribute’s value of an MI instance (e.g., replace one MI instance’s value with another MI instance value). We have tried something like

 

                                                Instance MISingle=MITrainSet.instance(0);

                                                Attribute MIAtt= MISingle.attribute(1);

                                                String MIAttVal= MISingle.stringValue(1);

                                                MISingle.setValue(MIAtt, MIAttVal);

 

But we got error messages as follows, when calling the function MISingle.setValue()

 

Exception in thread "main" java.lang.IllegalArgumentException: Attribute neither nominal nor string!

                at weka.core.Instance.setValue(Unknown Source)

 

Any suggestions?

 

Many thanks,

 

Hill


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

Re: How to change the attribute value of a multi-instance?

by Peter Reutemann-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> Do you have any suggestion on how to change the attribute value of an MI

> instance (multi-instance)?
>
> More specifically, after we transfer the instances from propositional to
> MultiInstances format. Each MI instance only contains three attributes. The
> second attribute is a relational attribute which contains all the attribute
> values.
>
> What we intend to do is to change the second attribute’s value of an MI
> instance (e.g., replace one MI instance’s value with another MI instance
> value). We have tried something like
>
>                                                 Instance
> MISingle=MITrainSet.instance(0);
>                                                 Attribute MIAtt=
> MISingle.attribute(1);
>                                                 String MIAttVal=
> MISingle.stringValue(1);
>                                                 MISingle.setValue(MIAtt,
> MIAttVal);
>
> But we got error messages as follows, when calling the function
> MISingle.setValue()
>
> Exception in thread "main" java.lang.IllegalArgumentException: Attribute
> neither nominal nor string!
>                 at weka.core.Instance.setValue(Unknown Source)
>
> Any suggestions?
Yes, you're retrieving the relational value wrong. You have to use the
"relationalValue(Attribute/int)" method of the Instance class (or in
the current developer version, Interface) to obtain the stored
weka.core.Instances object. And when setting, you have to set the
internal index via the "setValue(Attribute/int,double)" method that
you obtain via the Attribute's "addRelation(Instances)" method.

See attached example class, which just swaps the relational value of
two instances in a multi-instance dataset (works also when adding
completely new Instances objects).

Cheers, Peter
--
Peter Reutemann, Dept. of Computer Science, University of Waikato, NZ
http://www.cs.waikato.ac.nz/~fracpete/           Ph. +64 (7) 858-5174

[MISwap.java]

import weka.core.*;
import weka.core.converters.ConverterUtils.*;

/**
 * Example class that loads a multi-instance dataset and swaps the values of
 * the MI-bag of two instances.
 *
 * @author FracPete (fracpete at waikato dot ac dot nz)
 */
public class MISwap {

  /**
   * Expects a filename pointing to an MI dataset as first parameter.
   * The last attribute is used as class attribute.
   *
   * @param args        the commandline arguments
   * @throws Exception  if something goes wrong
   */
  public static void main(String[] args) throws Exception {
    // load data
    Instances data = DataSource.read(args[0]);
    data.setClassIndex(data.numAttributes() -1);

    // swap multi-instance value of instance 0 and 1 at attribute position 1
    int attPos = 1;
    Instances value0 = data.instance(0).relationalValue(attPos);
    Instances value1 = data.instance(1).relationalValue(attPos);
    data.instance(0).setValue(attPos, data.attribute(attPos).addRelation(value1));
    data.instance(1).setValue(attPos, data.attribute(attPos).addRelation(value0));

    // dump the Instances object in ARFF format to stdout
    System.out.println(data);
  }
}


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