📄 xmeans.java
字号:
/** * Sets the name of the file to write the list of centers to. * * @param fileName file to write centers to */ public void setOutputCenterFile(String fileName) { m_OutputCenterFile = fileName; } /** * Gets the name of the file to read the list of centers from. * * @return filename of the file to read the centers from */ public String getInputCenterFile() { return m_InputCenterFile; } /** * Gets the name of the file to write the list of centers to. * @return filename of the file to write centers to */ public String getOutputCenterFile() { return m_OutputCenterFile; } /** * Sets the KDTree class. * @param k a KDTree object with all options set */ public void setKDTree(KDTree k) { m_KDTree = k; } /** * Gets the KDTree class. * @return flag if KDTrees are used */ public KDTree getKDTree() { return m_KDTree; } /** * Gets the KDTree specification string, which contains the class name of * the KDTree class and any options to the KDTree * * @return the KDTree string. */ protected String getKDTreeSpec() { KDTree c = getKDTree(); if (c instanceof OptionHandler) { return c.getClass().getName() + " " + Utils.joinOptions(((OptionHandler)c).getOptions()); } return c.getClass().getName(); } /** * Sets the debug level. * debug level = 0, means no output * @param d debuglevel */ public void setDebugLevel(int d) { m_DebugLevel = d; } /** * Gets the debug level. * @return debug level */ public int getDebugLevel() { return m_DebugLevel; } /** * Gets the minimum number of clusters to generate. * @return the minimum number of clusters to generate */ public int getMinNumClusters() { return m_MinNumClusters; } /** * Gets the maximum number of clusters to generate. * @return the maximum number of clusters to generate */ public int getMaxNumClusters() { return m_MaxNumClusters; } /** * Returns the tip text for this property. * @return tip text for this property */ public String seedTipText() { return "random number seed"; } /** * Sets the random number seed. * @param s the seed */ public void setSeed(int s) { m_Seed = s; } /** * Gets the random number seed. * @return the seed */ public int getSeed() { return m_Seed; } /** * Checks the instances. * No checks in this KDTree but it calls the check of the distance function. */ private void checkInstances () throws Exception { // m_DistanceF.checkInstances(); } /** * Parses a given list of options. <p/> * <!-- options-start --> * Valid options are: <p/> * * <pre> -I <num> * maximum number of overall iterations * (default = 1).</pre> * * <pre> -M <num> * maximum number of iterations in the kMeans loop in * the Improve-Parameter part * (default = 1000).</pre> * * <pre> -J <num> * maximum number of iterations in the kMeans loop * for the splitted centroids in the Improve-Structure part * (default = 1000).</pre> * * <pre> -L <num> * minimum number of clusters * (default = 2).</pre> * * <pre> -H <num> * maximum number of clusters * (default = 4).</pre> * * <pre> -V <value> * distance value for binary attributes * (default = 1.0).</pre> * * <pre> -K <KDTree class specification> * Full class name of KDTree class to use, followed * by scheme options. * eg: "weka.core.KDTree -P" * (default = no KDTree class used).</pre> * * <pre> -C <value> * cutoff factor, takes the given percentage of the splitted * centroids if none of the children win * (default = 0.0).</pre> * * <pre> -K <distance function class specification> * Full class name of Distance function class to use, followed * by scheme options. * eg: "weka.core.MahalanobisDistance" * (default = weka.core.EuclideanDistance).</pre> * * <pre> -N <file name> * file to read starting centers from (ARFF format).</pre> * * <pre> -O <file name> * file to write centers to (ARFF format).</pre> * * <pre> -S <num> * random number seed (default 10).</pre> * <!-- options-end --> * * @param options the list of options as an array of strings * @throws Exception if an option is not supported */ public void setOptions(String[] options) throws Exception { String optionString = Utils.getOption('I', options); if (optionString.length() != 0) { setMaxIterations(Integer.parseInt(optionString)); } optionString = Utils.getOption('M', options); if (optionString.length() != 0) { setMaxKMeans(Integer.parseInt(optionString)); } optionString = Utils.getOption('J', options); if (optionString.length() != 0) { setMaxKMeansForChildren(Integer.parseInt(optionString)); } optionString = Utils.getOption('L', options); if (optionString.length() != 0) { setMinNumClusters(Integer.parseInt(optionString)); } optionString = Utils.getOption('H', options); if (optionString.length() != 0) { setMaxNumClusters(Integer.parseInt(optionString)); } optionString = Utils.getOption('B', options); if (optionString.length() != 0) { setBinValue(Double.parseDouble(optionString)); } String funcString = Utils.getOption('K', options); if (funcString.length() != 0) { String [] funcSpec = Utils.splitOptions(funcString); if (funcSpec.length == 0) { throw new Exception("Invalid function specification string"); } String funcName = funcSpec[0]; funcSpec[0] = ""; setKDTree((KDTree) Utils.forName(KDTree.class, funcName, funcSpec)); } optionString = Utils.getOption('C', options); if (optionString.length() != 0) { setCutOffFactor(Double.parseDouble(optionString)); } funcString = Utils.getOption('D', options); if (funcString.length() != 0) { String [] funcSpec = Utils.splitOptions(funcString); if (funcSpec.length == 0) { throw new Exception("Invalid function specification string"); } String funcName = funcSpec[0]; funcSpec[0] = ""; setDistanceF((EuclideanDistance) Utils.forName(EuclideanDistance.class, funcName, funcSpec)); } optionString = Utils.getOption('N', options); if (optionString.length() != 0) { setInputCenterFile(optionString); m_CenterInput = new BufferedReader(new FileReader(optionString)); } optionString = Utils.getOption('O', options); if (optionString.length() != 0) { setOutputCenterFile(optionString); m_CenterOutput = new PrintWriter(new FileOutputStream(optionString)); } optionString = Utils.getOption('S', options); if (optionString.length() != 0) { setSeed(Integer.parseInt(optionString)); } optionString = Utils.getOption('U', options); int debugLevel = 0; if (optionString.length() != 0) { try { debugLevel = Integer.parseInt(optionString); } catch (NumberFormatException e) { throw new Exception(optionString + "is an illegal value for option -U"); } } setDebugLevel(debugLevel); optionString = Utils.getOption('Y', options); if (optionString.length() != 0) { setDebugVektorsFile(optionString); } } /** * Gets the current settings of SimpleKMeans. * @return an array of strings suitable for passing to setOptions */ public String[] getOptions() { String[] options = new String[27]; int current = 0; options[current++] = "-I"; options[current++] = "" + getMaxIterations(); options[current++] = "-M"; options[current++] = "" + getMaxKMeans(); options[current++] = "-J"; options[current++] = "" + getMaxKMeansForChildren(); options[current++] = "-L"; options[current++] = "" + getMinNumClusters(); options[current++] = "-H"; options[current++] = "" + getMaxNumClusters(); options[current++] = "-B"; options[current++] = "" + getBinValue(); if (getKDTree() != null) { options[current++] = "-K"; options[current++] = "" + getKDTreeSpec(); } options[current++] = "-C"; options[current++] = "" + getCutOffFactor(); if (getDistanceF() != null) { options[current++] = "-D"; options[current++] = "" + getDistanceFSpec(); } if (getInputCenterFile() != null) { options[current++] = "-N"; options[current++] = "" + getInputCenterFile(); } if (getOutputCenterFile() != null) { options[current++] = "-O"; options[current++] = "" + getOutputCenterFile(); } options[current++] = "-S"; options[current++] = "" + getSeed(); int dL = getDebugLevel(); if (dL > 0) { options[current++] = "-U"; options[current++] = "" + getDebugLevel(); } while (current < options.length) { options[current++] = ""; } return options; } /** * Return a string describing this clusterer. * @return a description of the clusterer as a string */ public String toString() { StringBuffer temp = new StringBuffer(); temp.append("\nkMeans\n======\n"); temp.append("Requested iterations : " + m_MaxIterations + "\n"); temp.append("Iterations performed : " + m_IterationCount+ "\n"); temp.append("kMeans did not converge\n"); temp.append(" but was stopped by max-loops " + m_KMeansStopped + " times (max kMeans-iter) = \n\n"); temp.append("Splits prepared : " + m_NumSplits + "\n"); temp.append("Splits performed : " + m_NumSplitsDone + "\n"); temp.append("Cutoff factor : " + m_CutOffFactor + "\n"); double perc; if (m_NumSplitsDone > 0) perc = (((double)m_NumSplitsStillDone)/((double) m_NumSplitsDone)) * 100.0; else perc = 0.0; temp.append("Percentage of splits accepted \n" + "by cutoff factor : " + Utils.doubleToString(perc,2) + " %\n"); temp.append("------\n"); temp.append("Cutoff factor : " + m_CutOffFactor + "\n"); temp.append("------\n"); temp.append("\nCluster centers : " + m_NumClusters + " centers\n"); for (int i = 0; i < m_NumClusters; i++) { temp.append("\nCluster "+i+"\n "); for (int j = 0; j < m_ClusterCenters.numAttributes(); j++) { if (m_ClusterCenters.attribute(j).isNominal()) { temp.append(" "+m_ClusterCenters.attribute(j). value((int)m_ClusterCenters.instance(i).value(j))); } else { temp.append(" "+m_ClusterCenters.instance(i).value(j)); } } } if (m_Mle != null) temp.append("\n\nDistortion: " + Utils.doubleToString(Utils.sum(m_Mle),6) + "\n"); temp.append("BIC-Value : " + Utils.doubleToString(m_Bic,6) + "\n"); return temp.toString(); } /** * Print centers for debug. * @param debugLevel level that gives according messages * @return true if debug level is set */ private void PrCentersFD(int debugLevel) { if (debugLevel == m_DebugLevel) { for (int i = 0; i < m_ClusterCenters.numInstances(); i++) { System.out.println(m_ClusterCenters.instance(i)); } } } /** * Tests on debug status. * @param debugLevel level that gives according messages * @return true if debug level is set */ private boolean TFD(int debugLevel) { return (debugLevel == m_DebugLevel); } /** * Does debug printouts. * @param debugLevel level that gives according messages * @param output string that is printed */ private void PFD(int debugLevel, String output) { if (debugLevel == m_DebugLevel) System.out.println(output); } /** * Does debug printouts. * @param debugLevel level that gives according messages * @param output string that is printed */ private void PFD_CURR(String output) { if (m_CurrDebugFlag) System.out.println(output); } /** * Main method for testing this class. * @param argv should contain options */ public static void main(String[] argv) { try { System.out.println(ClusterEvaluation. evaluateClusterer(ne
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -