📄 .#mpckmeans.java.1.106
字号:
return m_Clusters; } /** * Computes the clusters from the cluster assignments, for external access * * @exception Exception if clusters could not be computed successfully */ public HashSet[] getIndexClusters() throws Exception { m_IndexClusters = new HashSet[m_NumClusters]; for (int i=0; i < m_Instances.numInstances(); i++) { if (m_verbose) { // System.out.println("In getIndexClusters, " + i + " assigned to cluster " + m_ClusterAssignments[i]); } if (m_ClusterAssignments[i]!=-1 && m_ClusterAssignments[i] < m_NumClusters) { if (m_IndexClusters[m_ClusterAssignments[i]] == null) { m_IndexClusters[m_ClusterAssignments[i]] = new HashSet(); } m_IndexClusters[m_ClusterAssignments[i]].add(new Integer(i)); } } return m_IndexClusters; } public Enumeration listOptions () { return null; } public String [] getOptions () { String[] options = new String[150]; int current = 0; if (m_Seedable) { options[current++] = "-S"; } if (m_Trainable != TRAINING_NONE) { options[current++] = "-T"; if (m_Trainable == TRAINING_INTERNAL) { options[current++] = "Int"; } else { options[current++] = "Ext"; } } options[current++] = "-M"; options[current++] = Utils.removeSubstring(m_metric.getClass().getName(), "weka.core.metrics."); if (m_metric instanceof OptionHandler) { String[] metricOptions = ((OptionHandler)m_metric).getOptions(); for (int i = 0; i < metricOptions.length; i++) { options[current++] = metricOptions[i]; } } if (m_Trainable != TRAINING_NONE) { options[current++] = "-L"; options[current++] = Utils.removeSubstring(m_metricLearner.getClass().getName(), "weka.clusterers.metriclearners."); String[] metricLearnerOptions = ((OptionHandler)m_metricLearner).getOptions(); for (int i = 0; i < metricLearnerOptions.length; i++) { options[current++] = metricLearnerOptions[i]; } } if (m_regularize) { options[current++] = "-G"; options[current++] = Utils.removeSubstring(m_metric.getRegularizer().getClass().getName(), "weka.clusterers.regularizers."); if (m_metric.getRegularizer() instanceof OptionHandler) { String[] regularizerOptions = ((OptionHandler)m_metric.getRegularizer()).getOptions(); for (int i = 0; i < regularizerOptions.length; i++) { options[current++] = regularizerOptions[i]; } } } options[current++] = "-A"; options[current++] = Utils.removeSubstring(m_Assigner.getClass().getName(), "weka.clusterers.assigners."); if (m_Assigner instanceof OptionHandler) { String[] assignerOptions = ((OptionHandler)m_Assigner).getOptions(); for (int i = 0; i < assignerOptions.length; i++) { options[current++] = assignerOptions[i]; } } options[current++] = "-I"; options[current++] = Utils.removeSubstring(m_Initializer.getClass().getName(), "weka.clusterers.initializers."); if (m_Initializer instanceof OptionHandler) { String[] initializerOptions = ((OptionHandler)m_Initializer).getOptions(); for (int i = 0; i < initializerOptions.length; i++) { options[current++] = initializerOptions[i]; } } if (m_useMultipleMetrics) { options[current++] = "-U"; } options[current++] = "-N"; options[current++] = "" + getNumClusters(); options[current++] = "-R"; options[current++] = "" + getRandomSeed(); options[current++] = "-l"; options[current++] = "" + m_logTermWeight; options[current++] = "-r"; options[current++] = "" + m_regularizerTermWeight; options[current++] = "-m"; options[current++] = "" + m_MLweight; options[current++] = "-c"; options[current++] = "" + m_CLweight; options[current++] = "-I"; options[current++] = "" + m_maxIterations; options[current++] = "-B"; options[current++] = "" + m_maxBlankIterations; options[current++] = "-O"; options[current++] = "" + m_ObjFunConvergenceDifference; options[current++] = "-V"; options[current++] = "" + m_useTransitiveConstraints; while (current < options.length) { options[current++] = ""; } return options; } /** * Parses a given list of options. * @param options the list of options as an array of strings * @exception Exception if an option is not supported * **/ public void setOptions (String[] options) throws Exception { if (Utils.getFlag('S', options)) { setSeedable(true); } String optionString = Utils.getOption('T', options); if (optionString.length() != 0) { setTrainable(new SelectedTag(Integer.parseInt(optionString), TAGS_TRAINING)); } optionString = Utils.getOption('M', options); if (optionString.length() != 0) { String[] metricSpec = Utils.splitOptions(optionString); String metricName = metricSpec[0]; metricSpec[0] = ""; setMetric((LearnableMetric) Utils.forName(LearnableMetric.class, metricName, metricSpec)); } optionString = Utils.getOption('L', options); if (optionString.length() != 0) { String[] learnerSpec = Utils.splitOptions(optionString); String learnerName = learnerSpec[0]; learnerSpec[0] = ""; setMetricLearner((MPCKMeansMetricLearner) Utils.forName(MPCKMeansMetricLearner.class, learnerName, learnerSpec)); } optionString = Utils.getOption('G', options); if (optionString.length() != 0) { String[] regularizerSpec = Utils.splitOptions(optionString); String regularizerName = regularizerSpec[0]; regularizerSpec[0] = ""; m_metric.setRegularizer((Regularizer) Utils.forName(Regularizer.class, regularizerName, regularizerSpec)); } optionString = Utils.getOption('A', options); if (optionString.length() != 0) { String[] assignerSpec = Utils.splitOptions(optionString); String assignerName = assignerSpec[0]; assignerSpec[0] = ""; setAssigner((MPCKMeansAssigner) Utils.forName(MPCKMeansAssigner.class, assignerName, assignerSpec)); } optionString = Utils.getOption('I', options); if (optionString.length() != 0) { String[] initializerSpec = Utils.splitOptions(optionString); String initializerName = initializerSpec[0]; initializerSpec[0] = ""; setInitializer((MPCKMeansInitializer) Utils.forName(MPCKMeansInitializer.class, initializerName, initializerSpec)); } if (Utils.getFlag('U', options)) { setUseMultipleMetrics(true); } optionString = Utils.getOption('N', options); if (optionString.length() != 0) { setNumClusters(Integer.parseInt(optionString)); } optionString = Utils.getOption('R', options); if (optionString.length() != 0) { setRandomSeed(Integer.parseInt(optionString)); } optionString = Utils.getOption('l', options); if (optionString.length() != 0) { setLogTermWeight(Double.parseDouble(optionString)); } optionString = Utils.getOption('r', options); if (optionString.length() != 0) { setRegularizerTermWeight(Double.parseDouble(optionString)); } optionString = Utils.getOption('m', options); if (optionString.length() != 0) { setMustLinkWeight(Double.parseDouble(optionString)); } optionString = Utils.getOption('c', options); if (optionString.length() != 0) { setCannotLinkWeight(Double.parseDouble(optionString)); } optionString = Utils.getOption('I', options); if (optionString.length() != 0) { setMaxIterations(Integer.parseInt(optionString)); } optionString = Utils.getOption('B', options); if (optionString.length() != 0) { setMaxBlankIterations(Integer.parseInt(optionString)); } optionString = Utils.getOption('O', options); if (optionString.length() != 0) { setObjFunConvergenceDifference(Double.parseDouble(optionString)); } if (Utils.getFlag('V', options)) { setUseTransitiveConstraints(true); } } /** * return a string describing this clusterer * * @return a description of the clusterer as a string */ public String toString() { StringBuffer temp = new StringBuffer(); return temp.toString(); } /** * set the verbosity level of the clusterer * @param verbose messages on(true) or off (false) */ public void setVerbose (boolean verbose) { m_verbose = verbose; } /** * get the verbosity level of the clusterer * @return messages on(true) or off (false) */ public boolean getVerbose () { return m_verbose; } /** Set/get the use of transitive closure */ public void setUseTransitiveConstraints(boolean useTransitiveConstraints) { m_useTransitiveConstraints = useTransitiveConstraints; } public boolean getUseTransitiveConstraints() { return m_useTransitiveConstraints; } /** * Turn on/off the use of per-cluster metrics * @param useMultipleMetrics if true, individual metrics will be used for each cluster */ public void setUseMultipleMetrics (boolean useMultipleMetrics) { m_useMultipleMetrics = useMultipleMetrics; } /** * See if individual per-cluster metrics are used * @return true if individual metrics are used for each cluster */ public boolean getUseMultipleMetrics () { return m_useMultipleMetrics; } /** * Turn on/off the use of regularization of weights * @param regularize, if true weights will be regularized */ public void setRegularize (boolean regularize) { m_regularize = regularize; } /** * See if weights are regularized * @return true if weights are regularized */ public boolean getRegularize () { return m_regularize; } /** * Get the value of the weight assigned to log term in the objective function * @return value of the weight assigned to log term in the objective function */ public double getLogTermWeight() { return m_logTermWeight; } /** * Set the value of the weight assigned to log term in the objective function * @param logTermWeight weight assigned to log term in the objective function */ public void setLogTermWeight(double logTermWeight) { this.m_logTermWeight = logTermWeight; } /** * Get the value of the weight assigned to regularizer term in the objective function * @return value of the weight assigned to regularizer term in the objective function */ public double getRegularizerTermWeight() { return m_regularizerTermWeight; } /** * Set the value of the weight assigned to regularizer term in the objective function * @param regularizerTermWeight weight assigned to regularizer term in the objective function */ public void setRegularizerTermWeight(double regularizerTermWeight) { this.m_regularizerTermWeight = regularizerTermWeight; } /** * Train the clusterer using specified parameters * * @param instances Instances to be used for training */ public void trainClusterer (Instances instances) throws Exception { if (m_metric instanceof LearnableMetric) { if (((LearnableMetric)m_metric).getTrainable()) { ((LearnableMetric)m_metric).learnMetric(instances); } else { throw new Exception ("Metric is not trainable"); } } else { throw new Exception ("Metric is not trainable"); } } /** * Main method for testing this class. * */ public static void main (String[] args) { //testCase(); runFromCommandLine(args); } public static void runFromCommandLine(String[] args) { MPCKMeans mpckmeans = new MPCKMeans(); Instances data = null, clusterData; ArrayList labeledPairs = null; try { String optionString = Utils.getOption('D', args); if (optionString.length() != 0) { FileReader reader = new FileReader (optionString); data = new Instances (reader); } optionString = Utils.getOption('K', args); if (optionString.length() != 0) { int classIndex = Integer.parseInt(optionString); data.setClassIndex(classIndex); // starts with 0 // Remove the class labels before clustering clusterData = new Instances(data); clusterData.deleteClassAttribute(); } else { int classIndex = data.numAttributes()-1; data.setClassIndex(classIndex); // starts with 0 // Remove the last attribute as class index clusterData = new Instances(data); clusterData.deleteClassAttribute(); } optionStr
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -