📄 .#semisupclusterersplitevaluator.java.1.11
字号:
} } ((MPCKMeans)m_Clusterer).buildClusterer(labeledTrainPairs, unlabeledData, labeledTrain, labeledTrain.numClasses(), labeledTrain.numInstances(),homologHash); // KLUGE: have to generalize later } else { throw new Exception ("Inappropriate clusterer: " + m_Clusterer.getClass().getName()); } // ((SeededKMeans)m_Clusterer).printClusters(); int numClusters = labeledTrain.numClasses(); if (m_Clusterer instanceof SemiSupClusterer) { numClusters = ((SemiSupClusterer)m_Clusterer).getNumClusters(); } SemiSupClustererEvaluation eval = new SemiSupClustererEvaluation(labeledTrainPairs, test, labeledTrain.numClasses(), numClusters); long trainTimeElapsed = System.currentTimeMillis() - trainTimeStart; long testTimeStart = System.currentTimeMillis(); eval.evaluateModel(m_Clusterer, test, unlabeledTest, homologHash); long testTimeElapsed = System.currentTimeMillis() - testTimeStart; m_result = eval.toSummaryString(); // The results stored are all per instance -- can be multiplied by the // number of instances to get absolute numbers int current = 0; // Unsupervised stats: 3 result[current++] = new Double(eval.purity()); result[current++] = new Double(eval.entropy()); result[current++] = new Double(eval.objectiveFunction()); // Supervised stats: 3 result[current++] = new Double(eval.klDivergence()); result[current++] = new Double(eval.mutualInformation()); result[current++] = new Double(eval.supervisedDispersion()); // Training data stats: 2 result[current++] = new Double(eval.numSameClassPairs()); result[current++] = new Double(eval.numDiffClassPairs()); // IR stats: 3 result[current++] = new Double(eval.pairwisePrecision()); result[current++] = new Double(eval.pairwiseRecall()); result[current++] = new Double(eval.pairwiseFMeasure()); // Timing stats: 2 result[current++] = new Double(trainTimeElapsed / 1000.0); result[current++] = new Double(testTimeElapsed / 1000.0); // Clusterer defined extras: 1 if (m_Clusterer instanceof Summarizable) { result[current++] = ((Summarizable)m_Clusterer).toSummaryString(); } else { result[current++] = null; } if (current != overall_length) { throw new Error("Results didn't fit RESULT_SIZE"); } return result; } /** * Gets the results for the supplied train and test datasets. * * @param labeledTrain the labeled training Instances. * @param unlabeledTrain the unlabeled training Instances. * @param test the testing Instances. * @return the results stored in an array. The objects stored in * the array may be Strings, Doubles, or null (for the missing value). * @exception Exception if a problem occurs while getting the results */ public Object [] getResult(Instances labeledTrain, Instances unlabeledTrain, Instances test, int numClasses) throws Exception { try { return getResult(labeledTrain, unlabeledTrain, test, test.numClasses(), -1); // labeled set is null } catch (Exception e) { e.printStackTrace(); } return null; } /** * Gets the results for the supplied train and test datasets. * * @param labeledTrain the labeled training Instances. * @param unlabeledData the unlabeled training (+ test for transductive) Instances. * @param test the testing Instances. * @param startingIndexOfTest from where test data starts in unlabeledData, useful if clustering is transductive * @return the results stored in an array. The objects stored in * the array may be Strings, Doubles, or null (for the missing value). * @exception Exception if a problem occurs while getting the results */ public Object [] getResult(Instances labeledTrain, Instances unlabeledData, Instances totalTrainWithLabels, Instances test, int startingIndexOfTest) throws Exception { if (labeledTrain.classAttribute().type() != Attribute.NOMINAL) { throw new WekaException("Class attribute is not nominal!"); } if (m_Clusterer == null) { throw new WekaException("No clusterer has been specified"); } if (!(m_Clusterer instanceof SemiSupClusterer)) { throw new WekaException("Clusterer should implement SemiSupClusterer interface!!\n"); // KLUGE (we could not make m_Clusterer of type SemiSupClusterer, since SemiSupClusterer is an interface and not an abstract class ... so we have to make the check here) } int overall_length = RESULT_SIZE; Object [] result = new Object[overall_length]; long trainTimeStart = System.currentTimeMillis(); int classIndex = labeledTrain.numAttributes()-1; // assuming that the last attribute is always the class ((SeededKMeans)m_Clusterer).buildClusterer(labeledTrain, unlabeledData, classIndex, totalTrainWithLabels, startingIndexOfTest); int numClusters = totalTrainWithLabels.numClasses(); if (m_Clusterer instanceof SemiSupClusterer) { numClusters = ((SemiSupClusterer)m_Clusterer).getNumClusters(); } SemiSupClustererEvaluation eval = new SemiSupClustererEvaluation(test, totalTrainWithLabels.numClasses(), numClusters); long trainTimeElapsed = System.currentTimeMillis() - trainTimeStart; long testTimeStart = System.currentTimeMillis(); Instances unlabeledTest = new Instances (test); unlabeledTest.deleteClassAttribute(); eval.evaluateModel(m_Clusterer, test, unlabeledTest, null); long testTimeElapsed = System.currentTimeMillis() - testTimeStart; m_result = eval.toSummaryString(); // The results stored are all per instance -- can be multiplied by the // number of instances to get absolute numbers int current = 0; // Unsupervised stats: 3 result[current++] = new Double(eval.purity()); result[current++] = new Double(eval.entropy()); result[current++] = new Double(eval.objectiveFunction()); // Supervised stats: 3 result[current++] = new Double(eval.klDivergence()); result[current++] = new Double(eval.mutualInformation()); result[current++] = new Double(eval.supervisedDispersion()); // Training data stats: 2 - there are no training pairs in this case result[current++] = new Double(0); result[current++] = new Double(0); // IR stats: 3 result[current++] = new Double(eval.pairwisePrecision()); result[current++] = new Double(eval.pairwiseRecall()); result[current++] = new Double(eval.pairwiseFMeasure()); // Timing stats: 2 result[current++] = new Double(trainTimeElapsed / 1000.0); result[current++] = new Double(testTimeElapsed / 1000.0); // Clusterer defined extras: 1 if (m_Clusterer instanceof Summarizable) { result[current++] = ((Summarizable)m_Clusterer).toSummaryString(); } else { result[current++] = null; } if (current != overall_length) { throw new Error("Results didn't fit RESULT_SIZE"); } return result; } /** * Gets the results for the supplied train and test datasets. * * @param labeledTrain the labeled training Instances. * @param unlabeledTrain the unlabeled training Instances. * @param test the testing Instances. * @param startingIndexOfTest from where test data starts in unlabeledData, useful if clustering is transductive * @return the results stored in an array. The objects stored in * the array may be Strings, Doubles, or null (for the missing value). * @exception Exception if a problem occurs while getting the results */ public Object [] getResult(Instances labeledTrain, Instances unlabeledTrain, Instances test, int numClasses, int startingIndexOfTest) throws Exception { if (labeledTrain.classAttribute().type() != Attribute.NOMINAL) { throw new WekaException("Class attribute is not nominal!"); } if (m_Clusterer == null) { throw new WekaException("No clusterer has been specified"); } if (!(m_Clusterer instanceof SemiSupClusterer)) { throw new WekaException("Clusterer should implement SemiSupClusterer interface!!\n"); // KLUGE (we could not make m_Clusterer of type SemiSupClusterer, since SemiSupClusterer is an interface and not an abstract class ... so we have to make the check here) } int overall_length = RESULT_SIZE; Object [] result = new Object[overall_length]; long trainTimeStart = System.currentTimeMillis(); int classIndex = labeledTrain.numAttributes()-1; // assuming that the last attribute is always the class ((SemiSupClusterer)m_Clusterer).buildClusterer(labeledTrain, unlabeledTrain, classIndex, numClasses, startingIndexOfTest); int numClusters = numClasses; if (m_Clusterer instanceof SemiSupClusterer) { numClusters = ((SemiSupClusterer)m_Clusterer).getNumClusters(); } SemiSupClustererEvaluation eval = new SemiSupClustererEvaluation(test, numClasses, numClusters); long trainTimeElapsed = System.currentTimeMillis() - trainTimeStart; long testTimeStart = System.currentTimeMillis(); Instances unlabeledTest = new Instances (test); unlabeledTest.deleteClassAttribute(); eval.evaluateModel(m_Clusterer, test, unlabeledTest, null); long testTimeElapsed = System.currentTimeMillis() - testTimeStart; m_result = eval.toSummaryString(); // The results stored are all per instance -- can be multiplied by the // number of instances to get absolute numbers int current = 0; // Unsupervised stats: 3 result[current++] = new Double(eval.purity()); result[current++] = new Double(eval.entropy()); result[current++] = new Double(eval.objectiveFunction()); // Supervised stats: 3 result[current++] = new Double(eval.klDivergence()); result[current++] = new Double(eval.mutualInformation()); result[current++] = new Double(eval.supervisedDispersion()); // Training data stats: 2 - there are no training pairs in this case result[current++] = new Double(0); result[current++] = new Double(0); // IR stats: 3 result[current++] = new Double(eval.pairwisePrecision()); result[current++] = new Double(eval.pairwiseRecall()); result[current++] = new Double(eval.pairwiseFMeasure()); // Timing stats: 2 result[current++] = new Double(trainTimeElapsed / 1000.0); result[current++] = new Double(testTimeElapsed / 1000.0); // Clusterer defined extras: 1 if (m_Clusterer instanceof Summarizable) { result[current++] = ((Summarizable)m_Clusterer).toSummaryString(); } else { result[current++] = null; } if (current != overall_length) { throw new Error("Results didn't fit RESULT_SIZE"); } return result; } /** * Returns the tip text for this property * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String clustererTipText() { return "The clusterer to use."; } /** * Get the value of Clusterer. * * @return Value of Clusterer. */ public Clusterer getClusterer() { return m_Clusterer; } /** * Sets the clusterer. * * @param newClusterer the new clusterer to use. */ public void setClusterer(Clusterer newClusterer) { m_Clusterer = newClusterer; updateOptions(); System.err.println("SemiSupClustererSplitEvaluator: In set clusterer"); } /** * Get the value of ClassForIRStatistics. * @return Value of ClassForIRStatistics. */ public int getClassForIRStatistics() { return m_IRclass; } /** * Set the value of ClassForIRStatistics. * @param v Value to assign to ClassForIRStatistics. */ public void setClassForIRStatistics(int v) { m_IRclass = v; } /** * Updates the options that the current clusterer is using. */ protected void updateOptions() { if (m_Clusterer instanceof OptionHandler) { m_ClustererOptions = Utils.joinOptions(((OptionHandler)m_Clusterer) .getOptions()); } else { m_ClustererOptions = ""; } if (m_Clusterer instanceof Serializable) { ObjectStreamClass obs = ObjectStreamClass.lookup(m_Clusterer .getClass()); m_ClustererVersion = "" + obs.getSerialVersionUID(); } else { m_ClustererVersion = ""; } } /** * Set the Clusterer to use, given it's class name. A new clusterer will be * instantiated. * * @param newClusterer the Clusterer class name. * @exception Exception if the class name is invalid. */ public void setClustererName(String newClustererName) throws Exception { try { setClusterer((Clusterer)Class.forName(newClustererName) .newInstance()); } catch (Exception ex) { throw new Exception("Can't find Clusterer with class name: " + newClustererName); } } /** * Gets the raw output from the clusterer * @return the raw output from the clusterer */ public String getRawResultOutput() { StringBuffer result = new StringBuffer(); if (m_Clusterer == null) { return "<null> clusterer"; } result.append(toString()); result.append("Clusterer model: \n"+m_Clusterer.toString()+'\n'); // append the performance statistics if (m_result != null) { result.append(m_result); } return result.toString(); } /** * Returns a text description of the split evaluator. * * @return a text description of the split evaluator. */ public String toString() { String result = "SemiSupClustererSplitEvaluator: "; if (m_Clusterer == null) { return result + "<null> clusterer"; } return result + m_Clusterer.getClass().getName() + " " + m_ClustererOptions + "(version " + m_ClustererVersion + ")"; }} // SemiSupClustererSplitEvaluator
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -