List of usage examples for weka.clusterers AbstractClusterer makeCopy
public static Clusterer makeCopy(Clusterer model) throws Exception
From source file:com.actelion.research.orbit.imageAnalysis.models.ClassifierWrapper.java
License:Open Source License
public static ClassifierWrapper makeCopy(ClassifierWrapper classifierWrapper) throws Exception { if (classifierWrapper == null) return null; if (classifierWrapper.getWrapperType() == WRAPPERTYPE_CLASSIFIER) { //return new ClassifierWrapper(Classifier.makeCopy(classifierWrapper.getClassifier())); ClassifierWrapper cw = new ClassifierWrapper( (AbstractClassifier.makeCopy(classifierWrapper.getClassifier()))); // weka 3.7.1 cw.setBinaryClassification(classifierWrapper.getBinaryClassification()); cw.setBuild(classifierWrapper.isBuild); return cw; } else {//from ww w . j a v a 2 s. c om ClassifierWrapper cw = new ClassifierWrapper( AbstractClusterer.makeCopy(classifierWrapper.getClusterer())); cw.setBinaryClassification(classifierWrapper.getBinaryClassification()); cw.setBuild(classifierWrapper.isBuild); return cw; } }
From source file:com.spread.experiment.tempuntilofficialrelease.ClassificationViaClustering108.java
License:Open Source License
/** * builds the classifier/*w w w. j ava 2s . c o m*/ * * @param data the training instances * @throws Exception if something goes wrong */ @Override public void buildClassifier(Instances data) throws Exception { // can classifier handle the data? getCapabilities().testWithFail(data); // save original header (needed for clusters to classes output) m_OriginalHeader = data.stringFreeStructure(); // remove class attribute for clusterer Instances clusterData = new Instances(data); clusterData.setClassIndex(-1); clusterData.deleteAttributeAt(data.classIndex()); m_ClusteringHeader = clusterData.stringFreeStructure(); if (m_ClusteringHeader.numAttributes() == 0) { System.err.println("Data contains only class attribute, defaulting to ZeroR model."); m_ZeroR = new ZeroR(); m_ZeroR.buildClassifier(data); } else { m_ZeroR = null; // build clusterer m_ActualClusterer = AbstractClusterer.makeCopy(m_Clusterer); m_ActualClusterer.buildClusterer(clusterData); if (!getLabelAllClusters()) { // determine classes-to-clusters mapping ClusterEvaluation eval = new ClusterEvaluation(); eval.setClusterer(m_ActualClusterer); eval.evaluateClusterer(clusterData); double[] clusterAssignments = eval.getClusterAssignments(); int[][] counts = new int[eval.getNumClusters()][m_OriginalHeader.numClasses()]; int[] clusterTotals = new int[eval.getNumClusters()]; double[] best = new double[eval.getNumClusters() + 1]; double[] current = new double[eval.getNumClusters() + 1]; for (int i = 0; i < data.numInstances(); i++) { Instance instance = data.instance(i); if (!instance.classIsMissing()) { counts[(int) clusterAssignments[i]][(int) instance.classValue()]++; clusterTotals[(int) clusterAssignments[i]]++; } } best[eval.getNumClusters()] = Double.MAX_VALUE; ClusterEvaluation.mapClasses(eval.getNumClusters(), 0, counts, clusterTotals, current, best, 0); m_ClustersToClasses = new double[best.length]; System.arraycopy(best, 0, m_ClustersToClasses, 0, best.length); } else { m_ClusterClassProbs = new double[m_ActualClusterer.numberOfClusters()][data.numClasses()]; for (int i = 0; i < data.numInstances(); i++) { Instance clusterInstance = clusterData.instance(i); Instance originalInstance = data.instance(i); if (!originalInstance.classIsMissing()) { double[] probs = m_ActualClusterer.distributionForInstance(clusterInstance); for (int j = 0; j < probs.length; j++) { m_ClusterClassProbs[j][(int) originalInstance.classValue()] += probs[j]; } } } for (int i = 0; i < m_ClusterClassProbs.length; i++) { Utils.normalize(m_ClusterClassProbs[i]); } } } }