"Get and set values using Groovy Script"
Dear All,
I want to learn how to use groovy script.
How to get example values for a particular attribute?
and how to set example values for a particular attribute?
The example by Ingo, pasted below only shows how to iterate over all attributes and all examples.
This is nice, but what if you only want to iterate over all examples of attribute 1,
calculate the sum, and place the result in attribute 2?
How to do this?
Like you start with:
att1 att2
0.1 NaN
0.2 NaN
0.3 NaN
0.4 NaN
And the result will be:
att1 att2
0.1 0.1
0.2 0.3
0.3 0.6
0.4 1.0
Best regards,
Wesesl
ExampleSet exampleSet = operator.getInput(ExampleSet.class);
exampleSet.recalculateAllAttributeStatistics();
for (Attribute attribute : exampleSet.getAttributes()) {
double mean = exampleSet.getStatistics(attribute, Statistics.AVERAGE);
String name = attribute.getName();
for (Example example : exampleSet) {
example[name] = example[name] - mean;
}
}
return exampleSet;
I want to learn how to use groovy script.
How to get example values for a particular attribute?
and how to set example values for a particular attribute?
The example by Ingo, pasted below only shows how to iterate over all attributes and all examples.
This is nice, but what if you only want to iterate over all examples of attribute 1,
calculate the sum, and place the result in attribute 2?
How to do this?
Like you start with:
att1 att2
0.1 NaN
0.2 NaN
0.3 NaN
0.4 NaN
And the result will be:
att1 att2
0.1 0.1
0.2 0.3
0.3 0.6
0.4 1.0
Best regards,
Wesesl
ExampleSet exampleSet = operator.getInput(ExampleSet.class);
exampleSet.recalculateAllAttributeStatistics();
for (Attribute attribute : exampleSet.getAttributes()) {
double mean = exampleSet.getStatistics(attribute, Statistics.AVERAGE);
String name = attribute.getName();
for (Example example : exampleSet) {
example[name] = example[name] - mean;
}
}
return exampleSet;
Tagged:
0
Answers
I guess you don't have the tutorial for the extension development at hand. This starts with an example for using the script operator. Since the other existing documentation is very rare, I built a little process for your example (just containing other values). I added a third attribute to demonstrate the creation of new attributes. The whole process is appended to the end of the post, here comes just the content of the script operator: I hope this makes things a bit clearer.
Best regards
Matthias
Thank you for the help above, very useful.
我有一个简单的脚本(商店wn below) to add 300 to each numerical example. However when I run the script the script outputs correctly but also changes all the previous blocks in my process. Is it possible to make the script operator work in one direction only? i.e. not effect previous results in the process?
Many Thanks,
大卫
ExampleSet exampleSet2 = input[0];
Attributes attributes = exampleSet2.getAttributes();
Attribute att2 = attributes.get("Midterm Exam");
String name = att2.getName();
for (Example example : exampleSet2) {
example[name] = example[name] + 300;
}
return exampleSet2;
I think that this RapidMiner description which I found:
"The process logic which RapidMiner uses is not "linear", but recursive. We dont apply operators linearly, one after another."
explains my query a bit. Could anybody expand on this description please?
Thanks a lot,
大卫
You can however create a deep copy of an example set prior to your script with the Materialize operator. That way the changes won't get progagated backwards.
Best, Marius
That explanation makes sense. I will be more careful with the operators. By any chance, is there a way to turn off the default setting (meta-data only passed) for a process?
Could you please let me know if it is possible to select a specific instance attribute?
For example, the first example in the attribute "Midterm Exam".
Attributes attributes = exampleSet.getAttributes();
Attribute exam = attributes.get("Mideterm Exam");
float ff = exam.getElementAt[0];
Many Thanks.