📄 xmeans.java
字号:
/** * Gets the distance function specification string, which contains the * class name of the distance function class and any options to it * * @return the distance function specification string */ protected String getDistanceFSpec() { DistanceFunction d = getDistanceF(); if (d instanceof OptionHandler) { return d.getClass().getName() + " " + Utils.joinOptions(((OptionHandler) d).getOptions()); } return d.getClass().getName(); } /** * Sets a file name for a file that has the random vektors stored. * Just used for debugging reasons. * @param fileName file name for the file to read the random vektors from */ public void setDebugVektorsFile(String fileName) { m_DebugVektorsFile = fileName; } /** * Initialises the debug vektor input. */ public void initDebugVektorsInput() throws Exception { m_DebugVektorsInput = new BufferedReader(new FileReader(m_DebugVektorsFile)); m_DebugVektors = new Instances(m_DebugVektorsInput); m_DebugVektorsIndex = 0; } /** * Read an instance from debug vektors file. * @param model the data model for the instance */ public Instance getNextDebugVektorsInstance(Instances model) throws Exception { if (m_DebugVektorsIndex >= m_DebugVektors.numInstances()) throw new Exception("no more prefabricated Vektors"); Instance nex = m_DebugVektors.instance(m_DebugVektorsIndex); nex.setDataset(model); m_DebugVektorsIndex++; return nex; } /** * Sets the name of the file to read the list of centers from. * * @param fileName file name of file to read centers from */ public void setInputCenterFile(String fileName) { m_InputCenterFile = fileName; } /** * 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. * @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 { 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((DistanceFunction) Utils.forName(DistanceFunction.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 D"); } } 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(); } options[current++] = "-N"; options[current++] = "" + getInputCenterFile(); 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(); } /** * Used for debug println's. * @param output string that is printed */ private void OOPS(int debugLevel, String output) { if (debugLevel == m_DebugLevel) System.out.println(output); } private void OOPS(String output) { System.out.println(output); } /** * 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)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -