weka.clusterers.ClassSpecificClusterer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of WekaClustererPT Show documentation
Show all versions of WekaClustererPT Show documentation
Weka implementation of the Clustering systems for Weka
The newest version!
/**
*
*/
package weka.clusterers;
import java.util.Arrays;
import weka.core.Capabilities;
import weka.core.Capabilities.Capability;
import weka.core.Instance;
import weka.core.Instances;
import weka.core.Utils;
import weka.filters.Filter;
import weka.filters.unsupervised.attribute.Remove;
import weka.tools.GlobalInfoHandler;
import weka.tools.data.InstancesOperator;
/**
* A class for clusterers that make separate clustering for each available class
* @author pawel trajdos
* @since 0.0.1
* @version 0.0.1
*
*
*/
public class ClassSpecificClusterer extends SingleClustererEnhancer implements GlobalInfoHandler {
/**
*
*/
private static final long serialVersionUID = -7492315438320617468L;
protected Remove removeFilter;
protected boolean noClass=false;
protected boolean noInstances=false;
protected boolean classesOnly = false;
protected Clusterer[] clusterers;
protected int numberOfClusters;
protected int numberOfClasses;
/**
*
*/
public ClassSpecificClusterer() {
super();
}
@Override
public void buildClusterer(Instances data) throws Exception {
this.noClass=false;
this.noInstances = false;
int classIndex = data.classIndex();
int numInstances = data.numInstances();
if(classIndex <0) {
this.noClass = true;
this.numberOfClasses = 0;
if(!this.m_DoNotCheckCapabilities)
this.m_Clusterer.getCapabilities().testWithFail(data);
this.m_Clusterer.buildClusterer(data);
this.numberOfClusters = this.m_Clusterer.numberOfClusters();
return;
}
if(numInstances == 0) {
this.noInstances=true;
this.numberOfClasses = data.numClasses();
this.numberOfClusters= this.numberOfClasses;
return;
}
int nAttributes = data.numAttributes();
if (classIndex>=0 & nAttributes==1){
this.classesOnly = true;
this.numberOfClasses = data.numClasses();
this.numberOfClusters = this.numberOfClasses;
return;
}
Instances[] classSplitData = InstancesOperator.classSpecSplit(data);
this.numberOfClasses = classSplitData.length;
int numClasses = classSplitData.length;
this.clusterers = AbstractClusterer.makeCopies(this.m_Clusterer, numClasses);
Remove remFilter = new Remove();
remFilter.setAttributeIndicesArray(new int[] {classIndex});
remFilter.setInputFormat(data);
remFilter.setInvertSelection(false);
this.removeFilter = remFilter;
Instances tmpInstances = null;
for(int i=0;i