protected void adjustCanopies(double[] densities){
  if (m_numClustersRequested < 0) {
    assignCanopiesToCanopyCenters();
    m_trainingData=new Instances(m_canopies,0);
    return;
  }
  if (m_canopies.numInstances() > m_numClustersRequested) {
    int[] sortedIndexes=Utils.stableSort(densities);
    Instances finalCanopies=new Instances(m_canopies,0);
    int count=0;
    for (int i=sortedIndexes.length - 1; count < m_numClustersRequested; i--) {
      finalCanopies.add(m_canopies.instance(sortedIndexes[i]));
      count++;
    }
    m_canopies=finalCanopies;
    List<double[][]> tempCanopyCenters=new ArrayList<double[][]>();
    List<double[]> tempT2Dists=new ArrayList<double[]>();
    List<double[]> tempMissings=new ArrayList<double[]>();
    count=0;
    for (int i=sortedIndexes.length - 1; count < finalCanopies.numInstances(); i--) {
      tempCanopyCenters.add(m_canopyCenters.get(sortedIndexes[i]));
      tempT2Dists.add(m_canopyT2Density.get(sortedIndexes[i]));
      tempMissings.add(m_canopyNumMissingForNumerics.get(sortedIndexes[i]));
      count++;
    }
    m_canopyCenters=tempCanopyCenters;
    m_canopyT2Density=tempT2Dists;
    m_canopyNumMissingForNumerics=tempMissings;
  }
 else   if (m_canopies.numInstances() < m_numClustersRequested && m_trainingData != null && m_trainingData.numInstances() > 0) {
    Random r=new Random(getSeed());
    for (int i=0; i < 10; i++) {
      r.nextInt();
    }
    HashMap<DecisionTableHashKey,Integer> initC=new HashMap<DecisionTableHashKey,Integer>();
    DecisionTableHashKey hk=null;
    for (int i=0; i < m_canopies.numInstances(); i++) {
      try {
        hk=new DecisionTableHashKey(m_canopies.instance(i),m_canopies.numAttributes(),true);
        initC.put(hk,null);
      }
 catch (      Exception e) {
        e.printStackTrace();
      }
    }
    for (int j=m_trainingData.numInstances() - 1; j >= 0; j--) {
      int instIndex=r.nextInt(j + 1);
      try {
        hk=new DecisionTableHashKey(m_trainingData.instance(instIndex),m_trainingData.numAttributes(),true);
      }
 catch (      Exception e) {
        e.printStackTrace();
      }
      if (!initC.containsKey(hk)) {
        Instance newInstance=m_trainingData.instance(instIndex);
        m_canopies.add(newInstance);
        double[] density=new double[1];
        density[0]=1.0;
        m_canopyT2Density.add(density);
        double[][] center=new double[newInstance.numAttributes()][0];
        double[] numMissingNumerics=new double[newInstance.numAttributes()];
        updateCanopyCenter(newInstance,center,numMissingNumerics);
        m_canopyCenters.add(center);
        m_canopyNumMissingForNumerics.add(numMissingNumerics);
        initC.put(hk,null);
      }
      m_trainingData.swap(j,instIndex);
      if (m_canopies.numInstances() == m_numClustersRequested) {
        break;
      }
    }
  }
  assignCanopiesToCanopyCenters();
  m_trainingData=new Instances(m_canopies,0);
}

protected void adjustCanopies ( double [ ] densities ) { if ( m_numClustersRequested < 0 ) { assignCanopiesToCanopyCenters ( ) ; m_trainingData = new Instances ( m_canopies , 0 ) ; return ; } if ( m_canopies . numInstances ( ) > m_numClustersRequested ) { int [ ] sortedIndexes = Utils . stableSort ( densities ) ; Instances finalCanopies = new Instances ( m_canopies , 0 ) ; int count = 0 ; for ( int i = sortedIndexes . length - 1 ; count < m_numClustersRequested ; i -- ) { finalCanopies . add ( m_canopies . instance ( sortedIndexes [ i ] ) ) ; count ++ ; } m_canopies = finalCanopies ; List < double [ ] [ ] > tempCanopyCenters = new ArrayList < double [ ] [ ] > ( ) ; List < double [ ] > tempT2Dists = new ArrayList < double [ ] > ( ) ; List < double [ ] > tempMissings = new ArrayList < double [ ] > ( ) ; count = 0 ; for ( int i = sortedIndexes . length - 1 ; count < finalCanopies . numInstances ( ) ; i -- ) { tempCanopyCenters . add ( m_canopyCenters . get ( sortedIndexes [ i ] ) ) ; tempT2Dists . add ( m_canopyT2Density . get ( sortedIndexes [ i ] ) ) ; tempMissings . add ( m_canopyNumMissingForNumerics . get ( sortedIndexes [ i ] ) ) ; count ++ ; } m_canopyCenters = tempCanopyCenters ; m_canopyT2Density = tempT2Dists ; m_canopyNumMissingForNumerics = tempMissings ; } else if ( m_canopies . numInstances ( ) < m_numClustersRequested && m_trainingData != null && m_trainingData . numInstances ( ) > 0 ) { Random r = new Random ( getSeed ( ) ) ; for ( int i = 0 ; i < 10 ; i ++ ) { r . nextInt ( ) ; } HashMap < DecisionTableHashKey , Integer > initC = new HashMap < DecisionTableHashKey , Integer > ( ) ; DecisionTableHashKey hk = null ; for ( int i = 0 ; i < m_canopies . numInstances ( ) ; i ++ ) { try { hk = new DecisionTableHashKey ( m_canopies . instance ( i ) , m_canopies . numAttributes ( ) , true ) ; initC . put ( hk , null ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; } } for ( int j = m_trainingData . numInstances ( ) - 1 ; j >= 0 ; j -- ) { int instIndex = r . nextInt ( j + 1 ) ; try { hk = new DecisionTableHashKey ( m_trainingData . instance ( instIndex ) , m_trainingData . numAttributes ( ) , true ) ; } catch ( Exception e ) { e . printStackTrace ( ) ; } if ( ! initC . containsKey ( hk ) ) { Instance newInstance = m_trainingData . instance ( instIndex ) ; m_canopies . add ( newInstance ) ; double [ ] density = new double [ 1 ] ; density [ 0 ] = 1.0 ; m_canopyT2Density . add ( density ) ; double [ ] [ ] center = new double [ newInstance . numAttributes ( ) ] [ 0 ] ; double [ ] numMissingNumerics = new double [ newInstance . numAttributes ( ) ] ; updateCanopyCenter ( newInstance , center , numMissingNumerics ) ; m_canopyCenters . add ( center ) ; m_canopyNumMissingForNumerics . add ( numMissingNumerics ) ; initC . put ( hk , null ) ; } m_trainingData . swap ( j , instIndex ) ; if ( m_canopies . numInstances ( ) == m_numClustersRequested ) { break ; } } } assignCanopiesToCanopyCenters ( ) ; m_trainingData = new Instances ( m_canopies , 0 ) ; }
adjust the final number of canopies to match the user - requested number ( if possible )
