Pages: [1]
  Print  
Author Topic: Attribute weight operator  (Read 2849 times)
Username
Newbie
*
Posts: 39


« on: July 01, 2008, 02:03:10 PM »

Hi,

is there an operator that assigns weights to attributes via the attribute name (maybe with regular expressions)? I looked at the docs but I didn't find anything. There's the InteractiveAttributeWeighting operator but I have too many features (text classification). If there's not such an operator I will write one myself. This should actually be pretty easy.

Thanks
Logged
Ingo Mierswa
Administrator
Hero Member
*****
Posts: 1226



WWW
« Reply #1 on: July 02, 2008, 08:55:49 AM »

Hello,

nice idea but there is currently no operator for this, sorry.

Cheers,
Ingo
Logged

Did you try our new Marketplace? Upload or download new Extensions, add comments, and organize your operators. Have a look at  http://marketplace.rapid-i.com
Username
Newbie
*
Posts: 39


« Reply #2 on: July 02, 2008, 09:17:00 AM »

Hello,

nice idea but there is currently no operator for this, sorry.

Cheers,
Ingo

Thanks for your answer. I will write one myself then.
Logged
Username
Newbie
*
Posts: 39


« Reply #3 on: July 02, 2008, 10:23:18 AM »

For anybody who's interested:

Code:
import java.util.List;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import com.rapidminer.example.Attribute;
import com.rapidminer.example.AttributeWeights;
import com.rapidminer.example.ExampleSet;
import com.rapidminer.operator.OperatorDescription;
import com.rapidminer.operator.OperatorException;
import com.rapidminer.operator.features.weighting.AbstractWeighting;
import com.rapidminer.parameter.ParameterType;
import com.rapidminer.parameter.ParameterTypeDouble;
import com.rapidminer.parameter.ParameterTypeList;

public class AttributeWeighting extends AbstractWeighting {

private static final String WEIGHTING_PARAMETER = "regex";

public AttributeWeighting(OperatorDescription description) {
super(description);
}

@Override
public AttributeWeights calculateWeights(ExampleSet exampleSet) throws OperatorException {
AttributeWeights attributeWeights = new AttributeWeights();
for (Attribute attribute : exampleSet.getAttributes()) {
attributeWeights.setWeight(attribute.getName(), 1);
}
@SuppressWarnings("unchecked")
List<Object[]> list = getParameterList(WEIGHTING_PARAMETER);
for (Object[] entry : list) {
String regex = (String) entry[0];
Double weighting = (Double) entry[1];
if (regex != null && weighting != null) {
try {
Pattern pattern = Pattern.compile(regex);
for (Attribute attribute : exampleSet.getAttributes()) {
String attributeName = attribute.getName();
if (pattern.matcher(attributeName).matches()) {
attributeWeights.setWeight(attributeName, weighting);
}
}
} catch (PatternSyntaxException e) {
logWarning(regex + " is not a valid regular expression!");
}
}
}
return attributeWeights;
}

@Override
public List<ParameterType> getParameterTypes() {
List<ParameterType> parameters = super.getParameterTypes();
ParameterType weighting = new ParameterTypeList(WEIGHTING_PARAMETER, "Regex -> Weighting", new ParameterTypeDouble("weighting", "weighting", 0, Double.MAX_VALUE, false));
weighting.setExpert(false);
parameters.add(weighting);
return parameters;
}

}
« Last Edit: July 02, 2008, 10:37:29 AM by Username » Logged
Ingo Mierswa
Administrator
Hero Member
*****
Posts: 1226



WWW
« Reply #4 on: July 02, 2008, 11:00:48 AM »

Wow, that's fast  Cool

I would really like to add your contribution to the official release if you do not object. If this is ok to you, I would like to ask if you could sign the joint copyright assignment (known from Open Office, basically it only states that both parties, i.e. you and Rapid-I, have the copyright for the contribution) and send it to us. Then we are able to add the new operator to the official release. If you like to contribute, just download and sign the JCA under

http://rapid-i.com/component/option,com_docman/task,doc_download/gid,26/Itemid,62/

Since we have to adapt the code a bit anyway to stick to our coding standards, this is of course not too much of an issue but I would like to get things right first.

Cheers,
Ingo
Logged

Did you try our new Marketplace? Upload or download new Extensions, add comments, and organize your operators. Have a look at  http://marketplace.rapid-i.com
Username
Newbie
*
Posts: 39


« Reply #5 on: July 03, 2008, 10:21:31 AM »

Wow, that's fast  Cool

I would really like to add your contribution to the official release if you do not object. If this is ok to you, I would like to ask if you could sign the joint copyright assignment (known from Open Office, basically it only states that both parties, i.e. you and Rapid-I, have the copyright for the contribution) and send it to us. Then we are able to add the new operator to the official release. If you like to contribute, just download and sign the JCA under

http://rapid-i.com/component/option,com_docman/task,doc_download/gid,26/Itemid,62/

Since we have to adapt the code a bit anyway to stick to our coding standards, this is of course not too much of an issue but I would like to get things right first.

Cheers,
Ingo

Ok, I will sign the JCA and send it via old fashioned mail in the next days.
Logged
Username
Newbie
*
Posts: 39


« Reply #6 on: July 08, 2008, 02:50:03 PM »

Ok, I will sign the JCA and send it via old fashioned mail in the next days.

The JCA should arrive today or tomorrow.
Logged
Ingo Mierswa
Administrator
Hero Member
*****
Posts: 1226



WWW
« Reply #7 on: July 09, 2008, 12:46:26 PM »

Hi,

the JCA arrived and we will add your extension. Thanks again and all the best,
Ingo
Logged

Did you try our new Marketplace? Upload or download new Extensions, add comments, and organize your operators. Have a look at  http://marketplace.rapid-i.com
Pages: [1]
  Print  
 
Jump to: