/*
* RapidMiner
*
* Copyright (C) 2001-2008 by Rapid-I and the contributors
*
* Complete list of developers available at our web site:
*
* http://rapid-i.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.rapidminer.operator.learner.clustering.clusterer;
import java.util.Iterator;
import com.rapidminer.example.Attribute;
import com.rapidminer.example.Attributes;
import com.rapidminer.example.Example;
import com.rapidminer.example.ExampleSet;
import com.rapidminer.example.Tools;
import com.rapidminer.operator.AbstractModel;
import com.rapidminer.operator.OperatorException;
import com.rapidminer.operator.UserError;
import com.rapidminer.tools.Ontology;
import com.rapidminer.tools.WekaInstancesAdaptor;
import com.rapidminer.tools.WekaTools;
import weka.clusterers.Clusterer;
import weka.core.Instance;
import weka.core.Instances;
/**
* A Weka clusterer which can be used to cluster examples. It is generated by a WekaClusterer.
*
* @author Ingo Mierswa
* @version $Id: WekaCluster.java,v 1.7 2008/09/12 10:31:40 tobiasmalbrecht Exp $
*/
public class WekaCluster extends AbstractModel {
private static final long serialVersionUID = -8901173604075912094L;
/** The used Weka clusterer. */
private final Clusterer clusterer;
public WekaCluster(ExampleSet exampleSet, Clusterer clusterer) {
super(exampleSet);
this.clusterer = clusterer;
}
public ExampleSet apply(ExampleSet exampleSet) throws OperatorException {
log("Converting to Weka instances.");
Instances instances = WekaTools.toWekaInstances(exampleSet, "ClusterInstances", WekaInstancesAdaptor.CLUSTERING);
log("Applying Weka clusterer.");
int i = 0;
Attribute clusterAtt = exampleSet.getAttributes().getCluster();
if (clusterAtt == null)
clusterAtt = Tools.createSpecialAttribute(exampleSet, Attributes.CLUSTER_NAME, Ontology.NOMINAL);
Iterator<Example> r = exampleSet.iterator();
while (r.hasNext()) {
Example e = r.next();
Instance instance = instances.instance(i++);
applyModelForInstance(instance, e, clusterAtt);
}
return exampleSet;
}
/**
* Clusters ervery weka instance and sets the result as cluster index of the current example.
*/
public void applyModelForInstance(Instance instance, Example e, Attribute clusterAtt) throws OperatorException {
int cluster = -1;
try {
cluster = clusterer.clusterInstance(instance);
} catch (Exception exc) {
throw new UserError(null, exc, 905, new Object[] {
clusterer, exc.toString()
});
}
e.setValue(clusterAtt, "cluster" + cluster);
}
public String toString() {
return clusterer.toString();
}
}