Adding a new Attribute to Instances

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

Adding a new Attribute to Instances

Irma Ravkic
Dear,

I encountered the following problem (I searched everywhere and couldn't find someone reporting the same issue. I might be wrong in how I am handling this):

I need to delete a numeric class attribute and add a new class attribute that represents the deleted one but with nominal (string) values. I am aware of filters (NumericToNominal) but why I am doing it this way is to bypass another issue which iI will explain at the end of the e-mail. 

The code producing the problem is the following:
String classAttribute = "someAttribute";
Instances dataSet = loader.getDataSet();
Attribute classAttr = dataSet.attribute(classAttribute); 
Instances newData = new Instances(dataSet);
Attribute newAttr = new Attribute("newAttr", Arrays.asList(new String[] {"A", "B"}));
newData.insertAttributeAt(newAttr, newData.numAttributes());
 System.out.println(newAttr.index()); //prints -1


This means that I cannot access the index of the new Attribute through index().
If I do:
System.out.println(newData.attribute(44)); //44 represents the position of the new attribute
it does return: @attribute newAttr {A,B} so no problem there.

(I went around it without using .index() and doing "hard coding", but just FYI)

Best and thanks in advance for your help and support,
Irma

_______________________________________________
Wekalist mailing list -- [hidden email]
Send posts to: To unsubscribe send an email to [hidden email]
To subscribe, unsubscribe, etc., visit
https://list.waikato.ac.nz/postorius/lists/wekalist.list.waikato.ac.nz
List etiquette: http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html
Reply | Threaded
Open this post in threaded view
|

Re: Adding a new Attribute to Instances

Eibe Frank-2
Administrator
This is a tricky one. The Javadoc for insertAttributeAt() has the key: "Shallow copies the attribute before it is inserted.”

This means the attribute object newAttr is (shallow) copied, and the index in your newAttr object is left at -1.

Try

  System.out.println(newData.attribute(44).index());

rather than

  System.out.println(newAttr.index());

Cheers,
Eibe

> On 13/11/2019, at 7:41 PM, Irma Ravkic <[hidden email]> wrote:
>
> Dear,
>
> I encountered the following problem (I searched everywhere and couldn't find someone reporting the same issue. I might be wrong in how I am handling this):
>
> I need to delete a numeric class attribute and add a new class attribute that represents the deleted one but with nominal (string) values. I am aware of filters (NumericToNominal) but why I am doing it this way is to bypass another issue which iI will explain at the end of the e-mail.
>
> The code producing the problem is the following:
> String classAttribute = "someAttribute";
> Instances dataSet = loader.getDataSet();
> Attribute classAttr = dataSet.attribute(classAttribute);
> Instances newData = new Instances(dataSet);
> Attribute newAttr = new Attribute("newAttr", Arrays.asList(new String[] {"A", "B"}));
> newData.insertAttributeAt(newAttr, newData.numAttributes());
>  System.out.println(newAttr.index()); //prints -1
>
> This means that I cannot access the index of the new Attribute through index().
> If I do:
> System.out.println(newData.attribute(44)); //44 represents the position of the new attribute
> it does return: @attribute newAttr {A,B} so no problem there.
>
> (I went around it without using .index() and doing "hard coding", but just FYI)
>
> Best and thanks in advance for your help and support,
> Irma
> _______________________________________________
> Wekalist mailing list -- [hidden email]
> Send posts to: To unsubscribe send an email to [hidden email]
> To subscribe, unsubscribe, etc., visit
> https://list.waikato.ac.nz/postorius/lists/wekalist.list.waikato.ac.nz
> List etiquette: http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html
_______________________________________________
Wekalist mailing list -- [hidden email]
Send posts to: To unsubscribe send an email to [hidden email]
To subscribe, unsubscribe, etc., visit
https://list.waikato.ac.nz/postorius/lists/wekalist.list.waikato.ac.nz
List etiquette: http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html