⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 .#mpckmeans.java.1.106

📁 wekaUT是 university texas austin 开发的基于weka的半指导学习(semi supervised learning)的分类器
💻 106
📖 第 1 页 / 共 5 页
字号:
							   m_maxCLPoints[j][1]);	  m_maxCLDiffInstances[j] = m_metrics[0].createDiffInstance(m_maxCLPoints[j][0], 								    m_maxCLPoints[j][1]);	}      }    }    return maxPenalties;  }   /**   * Checks if instance has to be normalized and classifies the   * instance using the current clustering   *   * @param instance the instance to be assigned to a cluster   * @return the number of the assigned cluster as an integer   * if the class is enumerated, otherwise the predicted value   * @exception Exception if instance could not be classified   * successfully */  public int clusterInstance(Instance instance) throws Exception {    return assignInstanceToCluster(instance);  }  /** lookup the instance in the checksum hash, assuming transductive clustering   * @param instance instance to be looked up   * @return the index of the cluster to which the instance was assigned, -1 if the instance has not bee clustered   */  protected int lookupInstanceCluster(Instance instance) throws Exception {    int classIdx = instance.classIndex();    double checksum = 0;    // need to normalize using original metric, since cluster data is normalized similarly    if (m_metric.doesNormalizeData()) {      if (m_Trainable == TRAINING_INTERNAL) {	m_metric.resetMetric();      }      m_metric.normalizeInstanceWeighted(instance);    }    double[] values1 = instance.toDoubleArray();    for (int i = 0; i < values1.length; i++) {      if (i != classIdx) {	checksum += m_checksumCoeffs[i] * values1[i];       }     }    Object list = m_checksumHash.get(new Double((float)checksum));    if (list != null) {      // go through the list of instances with the same checksum and find the one that is equivalent      ArrayList checksumList = (ArrayList) list;      for (int i = 0; i < checksumList.size(); i++) {	int instanceIdx = ((Integer) checksumList.get(i)).intValue();	Instance listInstance = m_Instances.instance(instanceIdx);	double[] values2 = listInstance.toDoubleArray();	boolean equal = true; 	for (int j = 0; j < values1.length && equal == true; j++) {	  if (j != classIdx) {	    if ((float)values1[j] != (float)values2[j]) {	      equal = false;	    }	  } 	}	if (equal == true) {	  return m_ClusterAssignments[instanceIdx]; 	}      }     }    return -1;   }  /**   * Classifies the instances using the current clustering, moves   * must-linked points together (Xing's approach)   *   * @param instIdx the instance index to be assigned to a cluster   * @return the number of the assigned cluster as an integer   * if the class is enumerated, otherwise the predicted value   * @exception Exception if instance could not be classified   * successfully */  public int assignAllInstancesToClusters() throws Exception {    int numInstances = m_Instances.numInstances();    boolean [] instanceAlreadyAssigned = new boolean[numInstances];    int moved = 0;    if (!m_isOfflineMetric) {      System.err.println("WARNING!!!\n\nThis code should not be called if metric is not a BarHillelMetric or XingMetric!!!!\n\n");    }    for (int i=0; i<numInstances; i++) {      instanceAlreadyAssigned[i] = false;    }    // now process points not in ML meighborhood sets    for (int instIdx = 0; instIdx < numInstances; instIdx++) {      if (instanceAlreadyAssigned[instIdx]) { 	continue; // was already in some ML neighborhood      }      int bestCluster = 0;      double bestDistance = Double.POSITIVE_INFINITY;      for (int centroidIdx = 0; centroidIdx < m_NumClusters; centroidIdx++) {	double sqDistance = m_metric.distance(m_Instances.instance(instIdx), m_ClusterCentroids.instance(centroidIdx));	if (sqDistance < bestDistance) {	  bestDistance = sqDistance;	  bestCluster = centroidIdx;	}      }      // accumulate objective function value      //      m_Objective += bestDistance;      // do we need to reassign the point?      if (m_ClusterAssignments[instIdx] != bestCluster) {	m_ClusterAssignments[instIdx] = bestCluster;	instanceAlreadyAssigned[instIdx] = true;	moved++;      }    }    return moved;  }  /**   * Classifies the instance using the current clustering, without considering constraints   *   * @param instance the instance to be assigned to a cluster   * @return the number of the assigned cluster as an integer   * if the class is enumerated, otherwise the predicted value   * @exception Exception if instance could not be classified   * successfully */  public int assignInstanceToCluster(Instance instance) throws Exception {    int bestCluster = 0;    double bestDistance = Double.POSITIVE_INFINITY;    double bestSimilarity = Double.NEGATIVE_INFINITY;    int lookupCluster;    if (m_metric instanceof InstanceConverter) {      Instance newInstance = ((InstanceConverter)m_metric).convertInstance(instance);      lookupCluster = lookupInstanceCluster(newInstance);    } else {      lookupCluster = lookupInstanceCluster(instance);    }    if (lookupCluster >= 0) {      return lookupCluster;    }    throw new Exception ("ACHTUNG!!!\n\nCouldn't lookup the instance!!! Size of hash = " + m_checksumHash.size());  }    /** Set the cannot link constraint weight */  public void setCannotLinkWeight(double w) {    m_CLweight = w;  }  /** Return the cannot link constraint weight */  public double getCannotLinkWeight() {    return m_CLweight;  }  /** Set the must link constraint weight */  public void setMustLinkWeight(double w) {    m_MLweight = w;  }  /** Return the must link constraint weight */  public double getMustLinkWeight() {    return m_MLweight;  }  /** Return the number of clusters */  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();  }     /** Set the m_SeedHash */  public void setSeedHash(HashMap seedhash) {    System.err.println("Not implemented here");  }      /**   * 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 maximum number of iterations */  public void setMaxIterations(int maxIterations) {    m_maxIterations = maxIterations;  }  /** Get the maximum number of iterations */  public int getMaxIterations() {    return m_maxIterations;  }  /** Set the maximum number of blank iterations (those where no points are moved) */  public void setMaxBlankIterations(int maxBlankIterations) {    m_maxBlankIterations = maxBlankIterations;  }  /** Get the maximum number of blank iterations */  public int getMaxBlankIterations() {    return m_maxBlankIterations;  }    /**   * 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;    // create the checksum coefficients    m_checksumCoeffs = new double[instances.numAttributes()];    for (int i = 0; i < m_checksumCoeffs.length; i++) {      m_checksumCoeffs[i] = m_RandomNumberGenerator.nextDouble();    }    // hash the instance checksums    m_checksumHash = new HashMap(instances.numInstances());    int classIdx = instances.classIndex();    for (int i = 0; i < instances.numInstances(); i++) {      Instance instance = instances.instance(i);      double[] values = instance.toDoubleArray();      double checksum = 0;      for (int j = 0; j < values.length; j++) {	if (j != classIdx) {	  checksum += m_checksumCoeffs[j] * values[j]; 	}       }      // take care of chaining      Object list = m_checksumHash.get(new Double((float)checksum));      ArrayList idxList = null;       if (list == null) {	idxList = new ArrayList();	m_checksumHash.put(new Double((float)checksum), idxList);      } else { // chaining	idxList = (ArrayList) list;      }      idxList.add(new Integer(i));    }   }  /** 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);    }  }  /** Is the objective function decreasing or increasing? */  public boolean isObjFunDecreasing() {    return m_objFunDecreasing;  }   /**   * Set the distance metric   *   * @param s the metric   */  public void setMetric (LearnableMetric m) {    String metricName = m.getClass().getName();    System.out.println("Using metric " + metricName);    m_metric = m;    m_metricLearner.setMetric(m_metric);    m_metricLearner.setClusterer(this);  }  /**   * get the distance metric   * @returns the distance metric used   */  public LearnableMetric getMetric () {    return m_metric;  }  /**   * get the array of metrics   */  public LearnableMetric[] getMetrics () {    return m_metrics;  }  /** Set/get the metric learner */  public void setMetricLearner (MPCKMeansMetricLearner ml) {    m_metricLearner = ml;    m_metricLearner.setMetric(m_metric);    m_metricLearner.setClusterer(this);  }  public MPCKMeansMetricLearner getMetricLearner () {    return m_metricLearner;  }  /** Set/get the assigner */  public MPCKMeansAssigner getAssigner() {    return m_Assigner;  }  public void setAssigner(MPCKMeansAssigner assigner) {    assigner.setClusterer(this);    this.m_Assigner = assigner;  }  /** Set/get the initializer */  public MPCKMeansInitializer getInitializer() {    return m_Initializer;  }  public void setInitializer(MPCKMeansInitializer initializer) {    initializer.setClusterer(this);    this.m_Initializer = initializer;  }  /** 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) {    System.err.println("Not implemented here");  }   /** 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 clusters from the cluster assignments, for external access   *    * @exception Exception if clusters could not be computed successfully   */      public ArrayList getClusters() throws Exception {    m_Clusters = 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_Clusters.add(clusterArray[j]);

⌨️ 快捷键说明

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