seededkmeans.java

来自「wekaUT是 university texas austin 开发的基于wek」· Java 代码 · 共 2,032 行 · 第 1/5 页

JAVA
2,032
字号
  public int getNumClusters() {    return m_NumClusters;  }  /** A duplicate function to conform to Clusterer abstract class.   * @returns the number of clusters   */  public int numberOfClusters() {    return getNumClusters();  }   /** Return the number of extra phase1 runs */  public double getExtraPhase1RunFraction() {    return m_ExtraPhase1RunFraction;  }  /** Set the number of extra phase1 runs */  public void setExtraPhase1RunFraction(double w) {    m_ExtraPhase1RunFraction = w;  }  /** Return the concentration */  public double getConcentration() {    return m_Concentration;  }  /** Set the concentration */  public void setConcentration(double w) {    m_Concentration = w;  }  /** Set the m_SeedHash */  public void setSeedHash(HashMap seedhash) {    m_SeedHash = seedhash;  }     /**   * Set the random number seed   * @param s the seed   */  public void setRandomSeed (int s) {    m_randomSeed = s;  }      /** Return the random number seed */  public int getRandomSeed () {    return  m_randomSeed;  }  /**   * Set the minimum value of the objective function difference required for convergence   * @param objFunConvergenceDifference the minimum value of the objective function difference required for convergence   */  public void setObjFunConvergenceDifference(double objFunConvergenceDifference) {    m_ObjFunConvergenceDifference = objFunConvergenceDifference;  }    /**   * Get the minimum value of the objective function difference required for convergence   * @returns the minimum value of the objective function difference required for convergence   */  public double getObjFunConvergenceDifference() {    return m_ObjFunConvergenceDifference;  }     /** Sets training instances */  public void setInstances(Instances instances) {    m_Instances = instances;  }  /** Return training instances */  public Instances getInstances() {    return m_Instances;  }  /**   * Set the number of clusters to generate   *   * @param n the number of clusters to generate   */  public void setNumClusters(int n) {    m_NumClusters = n;    if (m_Verbose) {      System.out.println("Number of clusters: " + n);    }  }  /**   * Set the distance metric   *   * @param s the metric   */  public void setMetric (LearnableMetric m) {    m_metric = m;    m_metricName = m_metric.getClass().getName();    m_objFunDecreasing = m.isDistanceBased();  }  /**   * Get the distance metric   *   * @returns the distance metric used   */  public Metric getMetric () {    return m_metric;  }  /**   * Get the distance metric name   *   * @returns the name of the distance metric used   */  public String metricName () {    return m_metricName;  }  /**   * Set the seeding method.  Values other than   * SEEDING_CONSTRAINED, or SEEDING_SEEDED will be ignored   *   * @param seedingMethod the seeding method to use   */  public void setSeedingMethod (SelectedTag seedingMethod)  {    if (seedingMethod.getTags() == TAGS_SEEDING) {      if (m_Verbose) {	System.out.println("Seeding method: " + seedingMethod.getSelectedTag().getReadable());      }      m_SeedingMethod = seedingMethod.getSelectedTag().getID();    }  }  /**   * Get the seeding method used.    *   * @returns the seeding method    */  public SelectedTag getSeedingMethod ()  {      return new SelectedTag(m_SeedingMethod, TAGS_SEEDING);  }  /**   * Set the KMeans algorithm.  Values other than   * ALGORITHM_SIMPLE or ALGORITHM_SPHERICAL will be ignored   *   * @param algo algorithm type   */  public void setAlgorithm (SelectedTag algo)  {    if (algo.getTags() == TAGS_ALGORITHM) {      if (m_Verbose) {	System.out.println("Algorithm: " + algo.getSelectedTag().getReadable());      }      m_Algorithm = algo.getSelectedTag().getID();    }  }  /**   * Get the KMeans algorithm type. Will be one of   * ALGORITHM_SIMPLE or ALGORITHM_SPHERICAL   *   * @returns algorithm type   */  public SelectedTag getAlgorithm ()  {    return new SelectedTag(m_Algorithm, TAGS_ALGORITHM);  }  /**   * Set the distance metric   *   * @param met the distance metric that should be used   */  public void setMetricName (String metricName) {    try {      m_metricName = metricName;      m_metric = (Metric) Class.forName(metricName).newInstance();      m_objFunDecreasing = m_metric.isDistanceBased();    }     catch (Exception e) {      System.err.println("Error instantiating metric " + metricName);    }  }  /** Set default perturbation value   * @param p perturbation fraction   */  public void setDefaultPerturb(double p) {    m_DefaultPerturb = p;  }  /** Get default perturbation value   * @return perturbation fraction   */  public double getDefaultPerturb(){    return m_DefaultPerturb;  }      /** Turn seeding on and off   * @param seedable should seeding be done?   */  public void setSeedable(boolean seedable) {    m_Seedable = seedable;  }  /** Turn seeding on and off   * @param seedable should seeding be done?   */  public boolean getSeedable() {      return m_Seedable;  }      /** Read the seeds from a hastable, where every key is an instance and every value is:   * the cluster assignment of that instance    * seedVector vector containing seeds   */    public void seedClusterer(HashMap seedHash) {    if(m_Seedable) {      setSeedHash(seedHash);    }  }    /**   * Computes the clusters from the cluster assignments, for external access   *    * @exception Exception if clusters could not be computed successfully   */        public ArrayList getIndexClusters() throws Exception {    m_IndexClusters = new ArrayList();    Cluster [] clusterArray = new Cluster[m_Instances.numInstances()];    for (int i=0; i < m_Instances.numInstances(); i++) {      if (m_ClusterAssignments[i]!=-1) {	if (clusterArray[m_ClusterAssignments[i]] == null) {	  clusterArray[m_ClusterAssignments[i]] = new Cluster();	}	clusterArray[m_ClusterAssignments[i]].add(new Integer(i), 1);      }    }        for (int j =0; j< m_Instances.numInstances(); j++)       m_IndexClusters.add(clusterArray[j]);        return m_IndexClusters;  }  /** Outputs the current clustering   *   * @exception Exception if something goes wrong   */  public void printIndexClusters() throws Exception {    if (m_IndexClusters == null)      throw new Exception ("Clusters were not created");    for (int i = 0; i < m_IndexClusters.size(); i++) {      Cluster cluster = (Cluster) m_IndexClusters.get(i);      if (cluster == null) {	//	System.out.println("Cluster " + i + " is null");      }      else {	System.out.println ("Cluster " + i + " consists of " + cluster.size() + " elements");	for (int j = 0; j < cluster.size(); j++) {	  int idx = ((Integer) cluster.get(j)).intValue();	  System.out.println("\t\t" + idx);	}      }    }  }  /** Prints clusters */  public void printClusters () throws Exception{    ArrayList clusters = getClusters();    for (int i=0; i<clusters.size(); i++) {      Cluster currentCluster = (Cluster) clusters.get(i);      System.out.println("\nCluster " + i + ": " + currentCluster.size() + " instances");      if (currentCluster == null) {	System.out.println("(empty)");      }      else {	for (int j=0; j<currentCluster.size(); j++) {	  Instance instance = (Instance) currentCluster.get(j);		  System.out.println("Instance: " + instance);	}      }    }  }  /**   * Computes the final clusters from the cluster assignments, for external access   *    * @exception Exception if clusters could not be computed successfully   */      public ArrayList getClusters() throws Exception {    m_FinalClusters = new ArrayList();    Cluster [] clusterArray = new Cluster[m_NumClusters];    for (int i=0; i < m_Instances.numInstances(); i++) {	Instance inst = m_Instances.instance(i);	if(clusterArray[m_ClusterAssignments[i]] == null)	   clusterArray[m_ClusterAssignments[i]] = new Cluster();	clusterArray[m_ClusterAssignments[i]].add(inst, 1);    }    for (int j =0; j< m_NumClusters; j++)       m_FinalClusters.add(clusterArray[j]);    return m_FinalClusters;  }  public Enumeration listOptions () {    return null;  }  /**   * Gets the classifier specification string, which contains the class name of   * the classifier and any options to the classifier   *   * @return the classifier string.   */  protected String getMetricSpec() {    if (m_metric instanceof OptionHandler) {      return m_metric.getClass().getName() + " "	+ Utils.joinOptions(((OptionHandler)m_metric).getOptions());    }    return m_metric.getClass().getName();  }    public String [] getOptions ()  {    String[] options = new String[80];    int current = 0;    options[current++] = "-N";    options[current++] = "" + getNumClusters();    options[current++] = "-R";    options[current++] = "" + getRandomSeed();    if (getSeedable()) {      options[current++] = "-S";      options[current++] = "" + getSeedingMethod().getSelectedTag().getID();    }    options[current++] = "-A";    options[current++] = "" + getAlgorithm().getSelectedTag().getID();    options[current++] = "-M";    options[current++] = m_metric.getClass().getName();    if (m_metric instanceof OptionHandler) {      String[] metricOptions = ((OptionHandler)m_metric).getOptions();      for (int i = 0; i < metricOptions.length; i++) {	options[current++] = metricOptions[i];      }    }     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 {    String 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('S', options);    if (optionString.length() != 0) {      setSeedingMethod(new SelectedTag(Integer.parseInt(optionString), TAGS_SEEDING));    }    else {      setSeedable(false);    }    optionString = Utils.getOption('A', options);    if (optionString.length() != 0) {      setAlgorithm(new SelectedTag(Integer.parseInt(optionString), TAGS_ALGORITHM));    }    optionString = Utils.getOption('M', options);    if (optionString.length() != 0) {      String[] metricSpec = Utils.splitOptions(optionString);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?