📄 makedensitybasedclusterer.java
字号:
return n; } /** * Computes the log of the conditional density (per cluster) for a given instance. * * @param inst the instance to compute the density for * @return an array containing the estimated densities * @throws Exception if the density could not be computed * successfully */ public double[] logDensityPerClusterForInstance(Instance inst) throws Exception { int i, j; double logprob; double[] wghts = new double[m_wrappedClusterer.numberOfClusters()]; m_replaceMissing.input(inst); inst = m_replaceMissing.output(); for (i = 0; i < m_wrappedClusterer.numberOfClusters(); i++) { logprob = 0; for (j = 0; j < inst.numAttributes(); j++) { if (!inst.isMissing(j)) { if (inst.attribute(j).isNominal()) { logprob += Math.log(m_model[i][j].getProbability(inst.value(j))); } else { // numeric attribute logprob += logNormalDens(inst.value(j), m_modelNormal[i][j][0], m_modelNormal[i][j][1]); } } } wghts[i] = logprob; } return wghts; } /** Constant for normal distribution. */ private static double m_normConst = 0.5 * Math.log(2 * Math.PI); /** * Density function of normal distribution. * @param x input value * @param mean mean of distribution * @param stdDev standard deviation of distribution * @return the density */ private double logNormalDens (double x, double mean, double stdDev) { double diff = x - mean; return - (diff * diff / (2 * stdDev * stdDev)) - m_normConst - Math.log(stdDev); } /** * Returns the number of clusters. * * @return the number of clusters generated for a training dataset. * @throws Exception if number of clusters could not be returned successfully */ public int numberOfClusters() throws Exception { return m_wrappedClusterer.numberOfClusters(); } /** * Returns a description of the clusterer. * * @return a string containing a description of the clusterer */ public String toString() { StringBuffer text = new StringBuffer(); text.append("MakeDensityBasedClusterer: \n\nWrapped clusterer: " + m_wrappedClusterer.toString()); text.append("\nFitted estimators (with ML estimates of variance):\n"); for (int j = 0; j < m_priors.length; j++) { text.append("\nCluster: " + j + " Prior probability: " + Utils.doubleToString(m_priors[j], 4) + "\n\n"); for (int i = 0; i < m_model[0].length; i++) { text.append("Attribute: " + m_theInstances.attribute(i).name() + "\n"); if (m_theInstances.attribute(i).isNominal()) { if (m_model[j][i] != null) { text.append(m_model[j][i].toString()); } } else { text.append("Normal Distribution. Mean = " + Utils.doubleToString(m_modelNormal[j][i][0], 4) + " StdDev = " + Utils.doubleToString(m_modelNormal[j][i][1], 4) + "\n"); } } } return text.toString(); } /** * 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 wrap"; } /** * Sets the clusterer to wrap. * * @param toWrap the clusterer */ public void setClusterer(Clusterer toWrap) { m_wrappedClusterer = toWrap; } /** * Gets the clusterer being wrapped. * * @return the clusterer */ public Clusterer getClusterer() { return m_wrappedClusterer; } /** * Returns the tip text for this property * @return tip text for this property suitable for * displaying in the explorer/experimenter gui */ public String minStdDevTipText() { return "set minimum allowable standard deviation"; } /** * Set the minimum value for standard deviation when calculating * normal density. Reducing this value can help prevent arithmetic * overflow resulting from multiplying large densities (arising from small * standard deviations) when there are many singleton or near singleton * values. * @param m minimum value for standard deviation */ public void setMinStdDev(double m) { m_minStdDev = m; } /** * Get the minimum allowable standard deviation. * @return the minumum allowable standard deviation */ public double getMinStdDev() { return m_minStdDev; } /** * Returns an enumeration describing the available options.. * * @return an enumeration of all the available options. */ public Enumeration listOptions() { Vector result = new Vector(); result.addElement(new Option( "\tminimum allowable standard deviation for normal density computation " +"\n\t(default 1e-6)" ,"M",1,"-M <num>")); result.addElement(new Option( "\tClusterer to wrap.\n" + "\t(default " + defaultClustererString() + ")", "W", 1,"-W <clusterer name>")); if ((m_wrappedClusterer != null) && (m_wrappedClusterer instanceof OptionHandler)) { result.addElement(new Option( "", "", 0, "\nOptions specific to clusterer " + m_wrappedClusterer.getClass().getName() + ":")); Enumeration enu = ((OptionHandler)m_wrappedClusterer).listOptions(); while (enu.hasMoreElements()) { result.addElement(enu.nextElement()); } } return result.elements(); } /** * Parses a given list of options. <p/> * <!-- options-start --> * Valid options are: <p/> * * <pre> -M <num> * minimum allowable standard deviation for normal density computation * (default 1e-6)</pre> * * <pre> -W <clusterer name> * Clusterer to wrap. * (default weka.clusterers.SimpleKMeans)</pre> * * <pre> * Options specific to clusterer weka.clusterers.SimpleKMeans: * </pre> * * <pre> -N <num> * number of clusters. (default = 2).</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('M', options); if (optionString.length() != 0) setMinStdDev((new Double(optionString)).doubleValue()); else setMinStdDev(1e-6); String wString = Utils.getOption('W', options); if (wString.length() == 0) wString = defaultClustererString(); setClusterer(Clusterer.forName(wString, Utils.partitionOptions(options))); } /** * Gets the current settings of the clusterer. * * @return an array of strings suitable for passing to setOptions() */ public String[] getOptions() { String [] clustererOptions = new String [0]; if ((m_wrappedClusterer != null) && (m_wrappedClusterer instanceof OptionHandler)) { clustererOptions = ((OptionHandler)m_wrappedClusterer).getOptions(); } String [] options = new String [clustererOptions.length + 5]; int current = 0; options[current++] = "-M"; options[current++] = ""+getMinStdDev(); if (getClusterer() != null) { options[current++] = "-W"; options[current++] = getClusterer().getClass().getName(); } options[current++] = "--"; System.arraycopy(clustererOptions, 0, options, current, clustererOptions.length); current += clustererOptions.length; while (current < options.length) { options[current++] = ""; } return options; } /** * Main method for testing this class. * * @param argv the options */ public static void main(String [] argv) { runClusterer(new MakeDensityBasedClusterer(), argv); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -